Hi, I seem to be getting myself into a lot of segmentation fault errors lately. The that currently has me stuck is one that shows up after I exit my program (an SDL App). When I need to debug I usually use the debugger or a Logger class I found and then modified, but none of them work here. The Logger doesn't work because it writes files to a folder, but since the program doesn't close cleanly the file doesn't get written. My debugger doesn't do a traceback and I don't know why. I get the segmentation fault error, the windows close, but the debugger just hangs there with no windows open. Any help finding the Segmentation fault or some reason as to why a segmentation fault would get called after everything closes would be appreciated.

Recommended Answers

erm..... none of us are clairvoyants...

Would you like to share your code?

Jump to Post

Wow, what did I get into?!?

//from CApp.h
        bool OnInit();

        void OnEvent(SDL_Event* Event);

            void OnExit();

        void OnLoop();

        void OnRender();

        void OnCleanup();

Where are these implemented? I can't find them, I looked in CApp.cpp because that made the most sense, they weren't there. I suspect the issue may …

Jump to Post

All 8 Replies

erm..... none of us are clairvoyants...

Would you like to share your code?

I included my code in this post, but the forum is more for general help in finding segmentation faults. You may help me solve this problem, but if I get another I want to be better armed for dealing with it. Thank you for your quick reply.
Since you offered to take a look at my code, I'll give a quick rundown. I'm trying to make a game to help students learn how to synthesize organic compounds. I'm in the very early stages of developments and I'm just trying to get a hold on how a button and gamestates work. Most of this code is modified from Lazy Foo and Tim Jones tutorials. Like i said, I get the segmentation fault only when I try to close the window and the debugger doesn't do a traceback.
Any help regarding my segmentation fault error, my coding style (which I know is severely lacking) or general SDL tips are appreciated. Thank you

Wow, what did I get into?!?

//from CApp.h
        bool OnInit();

        void OnEvent(SDL_Event* Event);

            void OnExit();

        void OnLoop();

        void OnRender();

        void OnCleanup();

Where are these implemented? I can't find them, I looked in CApp.cpp because that made the most sense, they weren't there. I suspect the issue may be in your OnExit() . I'd suggest starting there. If you're using dynamic allocation, you may be overrunning the limits of an array when de-allocating.

Haha, I know my code is big and too spread out, that's kind of why I didn't include it in my first post. The functions you talk about are implemented in CApp::OnExecute. I also think it must have something to do with OnCleanup, but I don't know which. I'm not sure if I'm not freeing the button, the state or the whole App. I would normally use the debugger to see, but it just hangs there indefinitely. I think the problems might be that when I call SDL_QUIT there's still a loaded Image on memory. The problem is that I free all the images in all of my classes's OnCleanup function. It could be that the main loop rund for a while longer and initializes something, but as far as I've checked with the debugger that doesn't happen. By the way, I don't use arrays so that can't be the problem, you don't have to take an in-depth look at my code, some more general advice is also appreciated.

Something that looks suspicious ...

TitleState::~TitleState()
{
    isCurrentState=false;
    StartButton.OnCleanup();
    SDL_FreeSurface(background);
}
//------------------------------------------------------------------------------
void TitleState::OnExit()
{
     StartButton.OnCleanup();
     isCurrentState=false;
     SDL_FreeSurface(background);
}

I'd say that be sure that you don't free anything twice i.e. don't do SDL_FreeSurface(background) , if the 'background' has already been freed.

Then change

void Out(char* words)
{
     printf(words);
}

to

void Out(char* words)
{
     printf("%s", words);
}

Remember that printf() treats its first argument as a format string. And if that string happens to 'resemble' a format string, printf() expects one or more arguments (which are not present), so you'd likely be heading for a nasty surprise.

O.k., followed the suggestions but I'm still getting the fault. I think that I used to be able to compile the program if I edited out the part in OnEvent.cpp that caused the Titlestate to get control of the events. Maybe there's something wrong there? Any other ideas or general advice?

O.k. I switched IDE, CodeBlocks outputs texts to the console instead of to a file so I should be able to get a better idea of where the problem lies. Just in case, I included a traceback that the console windows shows when I close my program. They only thing I can make of it is that there's something from when I free a surface. If you someone can make something more of it or if they come up with more suggestions please let me know. Thanks in advance.

I owe an apology to mitrmkar, the problem had something to do with what he mentioned in his post. It turns out that Code::Block's debugger outputs to the console so I could directly see where the program crashed. The problem was that I the deconstructor of the Button class was freeing an image that had already been freed. Thank you all for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.