Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: delete sf::Window  (Read 10371 times)

0 Members and 3 Guests are viewing this topic.

Anasky

  • Newbie
  • *
  • Posts: 20
    • View Profile
delete sf::Window
« on: May 02, 2016, 02:37:13 am »
Hey all,

For memory purposes I am trying to delete the sf::Window variable I have. However, when I try to do so, I get the following error:

Quote
Debug Assertion Failed!

Program: C:\Windows\system32\MSVCP140D.dll
File: C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\xtree
Line: 237

Expression: map/set iterator not dereferencable

The code I use is the following:

Quote
int main(int argc, char *argv[]){
m_WindowMode = sf::VideoMode(1024, 768);
m_Title = "Brawler";
m_ContextSettings = sf::ContextSettings(24U, 8U, 4U, 3U, 1U, 0U);
m_Style = sf::Style::None;
m_Style |= sf::Style::Titlebar;
m_Style |= sf::Style::Resize;
m_Style |= sf::Style::Close;
//m_Style |= sf::Style::Fullscreen;
Window = new sf::Window();

delete Window;
return 0;
}

Does anyone happen to know why this is happening, and if there is a solution to it?

Thanks in advance,
Anasky
« Last Edit: May 02, 2016, 02:48:44 am by Anasky »

Erdrick

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
    • Email
Re: delete sf::Window
« Reply #1 on: May 02, 2016, 03:25:54 am »
I see two things here.

It looks like the code you put in your message isn't complete, what object does m_Title etc. belong to?  Is there more code you should have included to help make sense of this example?

Secondly, why are using the new operator anyway instead of just declaring an object of type sf::Window?


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: delete sf::Window
« Reply #2 on: May 02, 2016, 07:59:32 am »
Quote
Expression: map/set iterator not dereferencable
... which involves you're using a std::map or std::set. So please show us your real code, it's just a waste of time otherwise.
Laurent Gomila - SFML developer

Anasky

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: delete sf::Window
« Reply #3 on: May 02, 2016, 02:02:11 pm »
Sure. Didn't wanna pollute the post with unnecessary data, but apparently I left out critical information.

My class:
Quote
namespace OpenGLNamespace
{
   class clsOpenGL
   {
   public:
      clsOpenGL();
      ~clsOpenGL();
      void Init(int ScreenWidth, int ScreenHeight, std::string Filepath);
      void LoadResources();
      void Resize();
      static void CheckOGLerror();
   public:
      std::string                        Filepath;
      sf::Window*                        Window;
   public:
      int                              m_ScreenWidth;
      int                              m_ScreenHeight;
      std::string                        m_ShaderPath;
      sf::VideoMode                     m_WindowMode;
      std::string                        m_Title;
      sf::ContextSettings                  m_ContextSettings;
      sf::Uint32                        m_Style;
      sf::WindowHandle                  m_WindowHandle;
      std::map<std::string, clsShader*>      m_Shaders;
      RECT                           m_WindowBorder;
      clsCamera*                        m_Camera;
   };
};

Its Init function:
Quote
void clsOpenGL::Init(int a_ScreenWidth, int a_ScreenHeight, std::string a_Filepath)
   {
      m_ScreenWidth = a_ScreenWidth;
      m_ScreenHeight = a_ScreenHeight;
      Filepath = a_Filepath;
      // Initialize window, ~OpenGL deletes it
      m_WindowMode = sf::VideoMode(1024, 768);
      m_Title = "Brawler";
      m_ContextSettings = sf::ContextSettings(24U, 8U, 4U, 3U, 1U, 0U);
      m_Style = sf::Style::None;
      m_Style |= sf::Style::Titlebar;
      m_Style |= sf::Style::Resize;
      m_Style |= sf::Style::Close;
      //m_Style |= sf::Style::Fullscreen;
      Window = new sf::Window();
   }

Its destructor:
Quote
   clsOpenGL::~clsOpenGL()
   {
      delete Window;
   }

And the int main function:
Quote
int main(int argc, char *argv[])
{
   // OpenGL
   OpenGL.Init(Main.m_ScreenResolutionWidth, Main.m_ScreenResolutionHeight, Main.Filepath);

   // Clean up
   return 0;
}

The only map I can see is the m_Shaders, which isn't even being used in the current code.
Hopefully this is enough information to help out :)

Thanks in advance,
Anasky

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: delete sf::Window
« Reply #4 on: May 02, 2016, 02:24:37 pm »
What's the stack track/call stack?

Why exactly are you manually allocating/deallocating the window?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: delete sf::Window
« Reply #5 on: May 02, 2016, 02:31:55 pm »
This code is still not complete.

For example, where is the creation of the OpenGL instance?

It might be wise for you to read this article (in particular, this section) to more easily allow people to help you.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Anasky

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: delete sf::Window
« Reply #6 on: May 02, 2016, 02:54:51 pm »
I must say, I look like a real fool now, lol.
Here is the call stack (crashes on ~Window(void)):
Code: [Select]
> msvcp140d.dll!std::_Debug_message(const wchar_t * message, const wchar_t * file, unsigned int line) Line 17 C++
  BrawlerEngine.exe!std::_Tree_const_iterator<class std::_Tree_val<struct std::_Tree_simple_types<class sf::priv::GlContext *> > >::operator*(void) Unknown
  BrawlerEngine.exe!sf::priv::GlContext::globalCleanup(void) Unknown
  BrawlerEngine.exe!sf::GlResource::~GlResource(void) Unknown
  BrawlerEngine.exe!sf::Window::~Window(void) Unknown
  BrawlerEngine.exe!sf::Window::`vector deleting destructor'(unsigned int) Unknown
  BrawlerEngine.exe!OpenGLNamespace::clsOpenGL::~clsOpenGL() Line 14 C++
  BrawlerEngine.exe!`dynamic atexit destructor for 'OpenGL''() C++
  ucrtbased.dll!__crt_scoped_stack_ptr<char>::__crt_scoped_stack_ptr<char>(struct __crt_scoped_stack_ptr_tag<char>) Unknown
  ucrtbased.dll!__crt_seh_guarded_call<int>::operator()<class <lambda_d22e02de4d14401f49897516b29a2b7c>,class <lambda_e971338317bfa523bb8920b43d823727> &,class <lambda_b0b8599c9027f5b14541fbbd6863d1f1> >(class <lambda_d22e02de4d14401f49897516b29a2b7c> &&,class <lambda_e971338317bfa523bb8920b43d823727> &,class <lambda_b0b8599c9027f5b14541fbbd6863d1f1> &&) Unknown
  ucrtbased.dll!__acrt_lock_and_call<class <lambda_e971338317bfa523bb8920b43d823727> >(enum __acrt_lock_id,class <lambda_e971338317bfa523bb8920b43d823727> &&) Unknown
  ucrtbased.dll!_execute_onexit_table() Unknown
  ucrtbased.dll!__crt_hmodule_traits::close(struct HINSTANCE__ *) Unknown
  ucrtbased.dll!exit() Unknown
  BrawlerEngine.exe!__scrt_common_main_seh() Line 266 C++
  BrawlerEngine.exe!__scrt_common_main() Line 300 C++
  BrawlerEngine.exe!mainCRTStartup() Line 17 C++
  kernel32.dll!BaseThreadInitThunk() Unknown
  ntdll.dll!RtlUserThreadStart() Unknown

If it's still not complete, I'm not sure what else to add. This is all the code that is being called, and the stacktrace is above.

As for creating the OpenGL instance, I haven't even reached that point yet. So far, I'm only trying to create and destroy a window.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: delete sf::Window
« Reply #7 on: May 02, 2016, 03:00:39 pm »
As for creating the OpenGL instance, I haven't even reached that point yet. So far, I'm only trying to create and destroy a window.
By OpenGL instance, I mean the OpenGL instance of your clsOpenGL object - the one on which you call Init.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Anasky

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: delete sf::Window
« Reply #8 on: May 02, 2016, 04:10:00 pm »
As for creating the OpenGL instance, I haven't even reached that point yet. So far, I'm only trying to create and destroy a window.
By OpenGL instance, I mean the OpenGL instance of your clsOpenGL object - the one on which you call Init.

My bad. It's a global instance.
Header:
extern OpenGLNamespace::clsOpenGL OpenGL;
CPP:
OpenGLNamespace::clsOpenGL OpenGL;

Its constructor only states:
        clsOpenGL::clsOpenGL()
        {
                Window = 0;
        }

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: delete sf::Window
« Reply #9 on: May 02, 2016, 04:45:55 pm »
Don't construct SFML resources at global scope.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Anasky

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: delete sf::Window
« Reply #10 on: May 02, 2016, 04:49:44 pm »
Don't construct SFML resources at global scope.

Which is why it's a pointer. That way it'll be constructed inside int main(), at the call of Window = new sf::Window();

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: delete sf::Window
« Reply #11 on: May 02, 2016, 05:06:05 pm »
When will it be destroyed?

The other SFML objects inside that class will be created as globals, including your map of shaders.
« Last Edit: May 02, 2016, 05:08:28 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Anasky

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: delete sf::Window
« Reply #12 on: May 02, 2016, 05:12:37 pm »
When will it be destroyed?

The other SFML objects inside that class will be created as globals, including your map of shaders.

My shader class contains no SFML objects, only GLuint, ints, strings, and ints.
It'll be destroyed at the end of the application, when clsOpenGL's destructor is called, at which point 'delete Window;' is executed, and crashes.

EDIT: Found the issue. I moved the delete function to somewhere inside my own code (instead of the destructor of a global class), and it works.
Thanks hapax :)

 

anything