Freeing a pointer doesn’t set the pointer to NULL.
In C++, deleting allocated memory pointer to by *p, sets *p
to 0.
if ( ptr ) {
free(ptr);
ptr = NULL;
}
Is NULL and 0 the same when talking pointers?
Anyway, the problem wasn’t related to the freeing-code.
I have a function that loads some surfaces, display them in
an window, frees the surfaces and calles SDL_Quit() and
returns into a main()-loop.
When the function is call again, the surfaces are displayed
correctly and everything works perfectly right until I close
the window and SDL_Quit() is called. Then it hangs.
I can of course ^C my way out of it, but then SDL
segfaults.
What can possibly be the cause of this?On Sun, 19 Dec 1999, Sam Lantinga wrote:
Freeing a pointer doesn’t set the pointer to NULL.
In C++, deleting allocated memory pointer to by *p, sets *p
to 0.
Most definitely not.
if ( ptr ) {
free(ptr);
ptr = NULL;
}
Is NULL and 0 the same when talking pointers?
In C++, the old NULL macro is deprecated and using just plain 0 is
recommended. The null pointer is not necessarily all-bits-zero, but when
0 is used as a literal pointer constant, it means the null pointer.
Anyway, the problem wasn’t related to the freeing-code.
I have a function that loads some surfaces, display them in
an window, frees the surfaces and calles SDL_Quit() and
returns into a main()-loop.
When the function is call again, the surfaces are displayed
correctly and everything works perfectly right until I close
the window and SDL_Quit() is called. Then it hangs.
I can of course ^C my way out of it, but then SDL
segfaults.
What can possibly be the cause of this?
Just be aware that memory problems such as deleting a pointer twice, or
using freed memory, can have very weird symptoms that vary across runs,
compilations, and architectures. What appears to work may not actually
work.> On Sun, 19 Dec 1999, Sam Lantinga wrote:
Anyway, the problem wasn’t related to the freeing-code.
I have a function that loads some surfaces, display them in
an window, frees the surfaces and calles SDL_Quit() and
returns into a main()-loop.
When the function is call again, the surfaces are displayed
correctly and everything works perfectly right until I close
the window and SDL_Quit() is called. Then it hangs.
I can of course ^C my way out of it, but then SDL
segfaults.
What can possibly be the cause of this?
Just be aware that memory problems such as deleting a pointer twice, or
using freed memory, can have very weird symptoms that vary across runs,
compilations, and architectures. What appears to work may not actually
work.
The crash has the same pattern. The first time the window is opened
and closed it works well. The second time the windows is opened and
then closed it hangs.
I’ve tried not calling SDL_Quit(), and then it works fine – except
the window isn’t closed and stays on the screen. But further calls to
the function updates the window and activates it (makes it accept
events as usual).
The SDL_Surface *screen usind in many of the SDL-demos as the
screen, on which all the graphics are draw… is it supposed to be
freed as well? (I’ve tried, but still no success).
I am not freeing pointers twice. Besides isn’t not the
SDL_FreeSurface that hangs.
Btw, what does the atexit(SDL_Quit) do? I implemented it because I
saw it in a demo and though it was useful, but uncommenting it doesn’t
change anything. Is it event-related somehow?–