C++ delete question!

Started by
6 comments, last by MajinMusashi 19 years, 4 months ago
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...
Advertisement
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
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.
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 darookie
Quote:
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().
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
Quote:Original post by joanusdmentia
Also, since SDL is in C, and not C++, you wouldn't use delete anyway, you would use free().


Oh, I've almost forgot that :)
And nice joke in your sig.

This topic is closed to new replies.

Advertisement