Domanda

I'm currently learning to code on SDL while using C++ as my base programming language.

So basically, what happens is, I have a piece of code with a loop in it, which will display an image till the loop starts again. The loop is 5 seconds long and the program has an iterator i set to 0 which increases with each loop until it gets to five, when it exits the loop via break. Then SDL quits as expected and the program is done running.

There are no compilation errors, just FYI. When I run the program, the main loop is run one single time and then the programs appears to close it self, printing on my Terminal window a message that says "Segmentation fault (core dumped)".

What does this mean, and what can be done to not make it happen? Thanks in advanced.

EDIT: The code is this one, sorry for not having written before. I'm writing it by memory as I'm currently not at home.

#include <iostream>
#include <SDL.h>

int main( int argc, char** argv )
{
    SDL_Surface* media;
    SDL_Surface* window;

    int i = 0;

    SDL_Init( SDL_INIT_EVERYTHING );
    window = SDL_SetVideoMode( 1920, 1080, 32, SDL_SWSURFACE );
    media = SDL_LoadBMP( "xD.bmp" );

    while( true )
    {
        SDL_BlitSurface( media, NULL, window, NULL );
        SDL_Delay( 5000 );

        if( i == 5 )
        {
            break;
        }

        i++;
    }

    SDL_FreeSurface( media );
    SDL_Quit( );

    return 0;
}

EDIT 2: The previous if (i=5) was a typographical and has been corrected.

EDIT 3: I've arrived home and checked my code. I didn't type if (i=5) , so I'm happy cause that's quite a stupid error. I've also tried lowering the window's size with no success, so we are back to the main problem. Also, I've tried removing the loop, and it the program is run and at the time of closing gives a segfault, so it's not the loop's fault at least. But this is a bigger problem now. BTW, I'll post the real compiled code, because the one before was written by memory.

#include <iostream>
#include <SDL/SDL.h>

int main (int argc, char** argv)
{
    SDL_Surface*  window;
    SDL_Surface* media;


    int i;

    SDL_Init (SDL_INIT_EVERYTHING);
    window = SDL_SetVideoMode (2058, 1152, 32, SDL_SWSURFACE);
    media = SDL_LoadBMP("xD.bmp");
    while (true)
    {
        i++;
        SDL_BlitSurface(media, NULL, window, NULL);
        SDL_Flip(window);
        SDL_Delay(5000);
        SDL_FreeSurface(media);

    if (i==5)
    {
        break;
    }

    }
    SDL_FreeSurface(media);
    SDL_Quit();
    return 0;
}

I just hope this is only happenning to me cause it's a pretty messed up thing to fix. Peace, fellows.

È stato utile?

Soluzione 2

SDL_FreeSurface(media); on every loop iteration, and then once again after loop is broken. This is wrong, after free media is dangling pointer and no longer points to valid location. Remove this from loop, there is absolutely no reasons to keep it there.

Also, as was stated in comment to question, check values of window and media. There is no guarantee that image exists and could be loaded, so it could be NULL.

Altri suggerimenti

the main loop is run one single time and then the programs appears to close it self

Unless it is a typographical error, then change

if( i = 5 )

to

if( i == 5 )

The i = 5 operation will always evaluate to TRUE and thus cause the break. Other than that, the program runs correctly on my computer with no segmentation fault.

Segmentation faults happen when you are trying to access memory that you shouldn't. For example below:

int array[5];
array[20] = 0;

Because you are accessing memory you are not allowed to access (it hasn't been declared), you will get a segmentation fault. This can also happen with memory leaks if you are trying to declare objects dynamically.

What exactly is inside of the loop? I'm not quite sure what would cause it unless you are accessing some part of memory that you're not allowed to. Try running a debugger and seeing what line of code exactly fails.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top