SDL inside a class

This topic is 4584 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Noticed something weird. If I put SDL variables in a class as member variables it will cause some internal memory bugs. First instance of the class was working ok, but the second didn't. When I changed their order the second one began to work. Further if I placed something between them both worked. Then I removed SDL related variables from the class (member variables) and then everything was fine. So, I guess SDL is incompatible with C++?:)

Share on other sites
Did you make the variable private. I also tried to encapsulate some SDL functions. I tried to return SDL_Surface but it gave me an error. Is this familiar to you?

Share on other sites
Hmm, the order in which you declared the variables mattered? Are you doing any pointer voodoo?

edit: No, that isn't what you said.

You are probably trying to free the same surface twice, or something like that.

Share on other sites
Show us your code. I've written many classes that use SDL for various things and they've all worked fine.

Share on other sites
The SDL part was:

class G_Tile {SDL_Rect so;SDL_Rect de;SDL_Surface *s_src;...

First I tried to move SDL_Surface out, but finally had to remove those Rects too to make it work. I have no idea why it failed and because of that my code is partially procedural which isn't that nice...
In the constructor I made a surface for s_src and the code is working ok when removed from class, so I don't think there are bugs in that implementation. Just putting them in a class makes the thing go nuts.

Share on other sites
I have code in my game that looks similar to yours, and all is working fine. I'm afraid it's not the class membership that screws thing up...

Share on other sites
Quote:
 Original post by Daniel MillerYou are probably trying to free the same surface twice, or something like that.

I don't think so, because the surface for G_Tile class was freed in the destructor.

Share on other sites
Quote:
 Original post by DaBonoI have code in my game that looks similar to yours, and all is working fine. I'm afraid it's not the class membership that screws thing up...

In this case it seems to be. Did you ever try to make two or more instances from your class in a row? The first instance was working.

Share on other sites
There was a bug after all. When I copied the old procedural routine I had left the local declaration of SDL_Surface *s_src; there. It was silently overriding the member variable and that generated no warning messages from the compiler. Still I think it's odd id didn't crash directly. But the problem is now solved:)