2

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.

Martin G
  • 17,357
  • 9
  • 82
  • 98
ChemiCalChems
  • 612
  • 12
  • 31
  • This is likely unrelated to the fact you're using SDL or on an iPhone - if you could show us the loop it would really help. Otherwise we're just guessing. – crimson_penguin Apr 23 '14 at 05:52
  • possible duplicate of [What is segmentation fault?](http://stackoverflow.com/questions/2346806/what-is-segmentation-fault) – razlebe Apr 23 '14 at 05:52
  • It's partially a duplicate of that, but the OP also wants to know why their program in particular segfaults. – crimson_penguin Apr 23 '14 at 05:53
  • 1
    "There are no compilation errors, just FYI " - We somewhat figured there were no compilation error, otherwise a *run-time* crash would be rather unlikely. Crashes mean you're likely invoking *undefined behavior* somewhere. A debugger and/or Valgrind may assist in tracking down *why*. – WhozCraig Apr 23 '14 at 05:54
  • Presented loop have only one iteration (because `if` condition is an assignment), but aside from that looks ok. You should check values of `window` and `media`. – keltar Apr 23 '14 at 06:46
  • 1
    Perhaps, insert this code before the loop `if (window == nullptr || media == nullptr) return -1;` – mr5 Apr 23 '14 at 07:56
  • No, @mr5, if I dont set the loop it works perfectly fine. And if I had said if (i=5), the loop would repeated forever AND a compilation error would have ocurred. – ChemiCalChems Apr 23 '14 at 14:58
  • Just check those variables, its harmless or you could just reduce the size of your `window` instead. – mr5 Apr 23 '14 at 15:46
  • `if(i=5)` is perfectly valid code which will result in breaking loop after first iteration. Value of `i=5` is `5`, and `(bool)5` is `true`. There could be no compilation error, however modern compilers issues a warning for this code, just in case (which could be suppressed with `if((i=5))`, if it really what programmer have meant). – keltar Apr 24 '14 at 03:01

3 Answers3

2

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.

ssell
  • 6,429
  • 2
  • 34
  • 49
  • @user3563094 ***typographical**. Just nitpicking (I can't somewhat handle it) – mr5 Apr 23 '14 at 07:58
  • dont worry mr 5 and @ssell if this has happened then i consider my self a complete idiot... ill check my code when i get home ,and if not ill make an edit. thanks for everything – ChemiCalChems Apr 23 '14 at 17:27
1

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.

keltar
  • 17,711
  • 2
  • 37
  • 42
  • The image is shown on screen both with the loop written and with the loop not written. So that isn't the problem. – ChemiCalChems Apr 24 '14 at 05:24
  • And also, if it never exists the loop, because i !=5 , it shouldn' deal with the dangling pointer problem. And also, freeing the surface in every loop is a way in much bigger programs of avoiding memory overload. But anyway, I'll try taking away the SDL_FreeSurface (media) which is outside the loop. – ChemiCalChems Apr 24 '14 at 05:38
  • I just got what you were saying, so if I don't free that surface it is still used with value xD.bmp. If I do, it's value is NULL I would have to declare it again, cause if not segfault comes in. Lol. It was all my understanding of what FreeSurface did. I will try when I can, but sounds good to me. This may be the solution. Thanks, keltar. – ChemiCalChems Apr 24 '14 at 05:46
  • What you saying is complete nonsense. FreeSurface will destroy surface, but will *not* change value of argument - because it can't (standard C semantics - arguments passed by value). Resulting pointer will still point on memory location where surface was, but it no longer here. If FreeSurface called again, it have no ways to determine surface no longer exists and will try to free it again; results could be problematic. This is exactly what dangling pointer means. – keltar Apr 24 '14 at 05:55
  • Destroying something and changing its value to NULL is NOT the same thing, of course. So if I do SDL_FreeSurface(media), the var media is destroyed and program segfaults. Thanks for the correction, but chill bro ok? I'm learning and errors are one of the best ways to learn. So nothing is a complete nonsense in any way, it's just a misunderstanding, in my point of view. Thank you guys for all the help. :D No more segfault. – ChemiCalChems Apr 24 '14 at 06:25
-4

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.

  • 1
    Not very good example, in this particular case segfault is very unlikely to occur because it works on per-page level, not on per-byte. – keltar Apr 23 '14 at 06:05
  • I agree with keltar. In your case segmentation fault is NOT likely to occur. This code will probably trigger it: int *array = 0; *array = 0;. It depends on the underlying OS memory policy; in some embedded environment you can access all the memory. – Gianluca Ghettini Apr 23 '14 at 07:20