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.

4

3 回答 3

2

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

除非是印刷错误,否则更改

if( i = 5 )

if( i == 5 )

i = 5操作将始终评估为TRUE并因此导致break. 除此之外,该程序在我的计算机上正常运行,没有分段错误。

于 2014-04-23T07:20:42.887 回答
1

SDL_FreeSurface(media);在每次循环迭代中,然后在循环中断后再次。这是错误的,afterfree media是悬空指针并且不再指向有效位置。从循环中删除它,绝对没有理由将它保留在那里。

此外,正如对问题的评论所述,检查window和的值media。不能保证图像存在并且可以加载,所以它可能是NULL.

于 2014-04-24T03:17:05.333 回答
-4

当您尝试访问不应该访问的内存时,会发生分段错误。例如下面:

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

因为您正在访问不允许访问的内存(尚未声明),所以您将遇到分段错误。如果您尝试动态声明对象,则内存泄漏也可能发生这种情况。

循环内部到底是什么?我不太确定是什么原因导致的,除非您访问的是不允许访问的内存部分。尝试运行调试器并查看哪行代码完全失败。

于 2014-04-23T06:00:05.390 回答