C++ delete question!
I'm using Visual C++ 6.0 with SDL, and I'm in doubt about the following situation:
Uint8 *keys;
keys = SDL_GetKeyState(NULL);
delete keys;
When the application ends, and I'm in debugging mode, it throws me a "User breakpoint called from code at 0x7c901230" in the moment I try to delete "keys". If I've allocated memory, I must deallocate it, right? Why is this happening?
Thanks!!
PS.: this is just curiosity. Not deleting "keys" will make the app ends fine...
My guess would be that you haven't allocated any memory. This confirms it.
Any time you are given a pointer, check the documentation carefully to see who is responsible for the memory it points to. In this case, the SDL maintains ownership of the memory, so it will free it when the time comes to do so. If you're getting a breakpoint, there's a pretty good chance that memory wasn't dynamically allocated in the first place, but other times you won't be so lucky.
CM
Any time you are given a pointer, check the documentation carefully to see who is responsible for the memory it points to. In this case, the SDL maintains ownership of the memory, so it will free it when the time comes to do so. If you're getting a breakpoint, there's a pretty good chance that memory wasn't dynamically allocated in the first place, but other times you won't be so lucky.
CM
SDL_GetKeyState() returns a pointer to an internal array on stack, which you must not delete.
Rule of thumb: Only 'delete' pointers you created with 'new'. When in doubt (e.g. using a 3rd party library like SDL), consult the documentation.
Rule of thumb: Only 'delete' pointers you created with 'new'. When in doubt (e.g. using a 3rd party library like SDL), consult the documentation.
Quote:Straight from the docs:
Gets a snapshot of the current keyboard state. The current state is returned as a pointer to an array. The size of this array is stored in numkeys. The array is indexed by the SDLK_* symbols (see SDLKey). A value of 1 means the key is pressed and a value of 0 means its not. The pointer returned is a pointer to an internal SDL array and should not be freed by the caller.
I'm kind today so I won't shout rtfm at you [wink].
Regards,
Pat.
Original post by darookieQuote:
SDL_GetKeyState() returns a pointer to an internal array on stack, which you must not delete.
Rule of thumb: Only 'delete' pointers you created with 'new'. When in doubt (e.g. using a 3rd party library like SDL), consult the documentation.
I'm kind today so I won't shout rtfm at you [wink].
Regards,
Pat.
It can look very strange, but this function is not in my SDL docs, so I thought it could be an undocumented one (it happens [wink]).
But thanks!
PS.: RTFM? What is that? ;) Hehe.
Quote:Original post by Conner McCloud
My guess would be that you haven't allocated any memory. This confirms it.
Any time you are given a pointer, check the documentation carefully to see who is responsible for the memory it points to. In this case, the SDL maintains ownership of the memory, so it will free it when the time comes to do so. If you're getting a breakpoint, there's a pretty good chance that memory wasn't dynamically allocated in the first place, but other times you won't be so lucky.
CM
Thanks, now I completely understand what was going there!
I love you all [wink]
Also, since SDL is in C, and not C++, you wouldn't use delete anyway, you would use free().
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement