2017-08-11 60 views
-3

我试图找遍各地的答案,但无济于事。 在调试模式下它工作得很好,但是当我把它在发行模式下,它给我的SDL project.exe在0x00DC1814这个我在发布模式下运行时出现“访问冲突读取位置”错误 - C++

未处理的异常:0000005:访问冲突读取位置00000000。

我有我的调试器和发布模式包括和库相同,甚至我的子系统是相同的。这里是我的代码,我有一点缩短

SDL_Surface *loadimage(); 

struct picture 
{  
    int maxframe; 
    SDL_Surface *surface = NULL; 
    SDL_Rect rect; 
    std::string filepath; 
}; 

SDL_Surface *background = NULL; 
SDL_Surface *backbuffer = NULL; 
SDL_Surface *holder = NULL; 


std::vector<picture *> veck; 

int main(int argc, char* argu[]){ 
    background = SDL_LoadBMP("pics/bac.bmp"); 

    if (background == NULL){ 
     return false; 
    } 

    int height = background->h; 
    int width = background->w; 

    init_testprogram(); 



    backbuffer = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE); 
    SDL_WM_SetCaption("Yamada's first window", NULL); 

    //this was here to test if i could format a surface more then once in 
    //the same format kept in just in case 
    holder = SDL_DisplayFormat(background); 
    background = SDL_DisplayFormat(holder); 
    SDL_FreeSurface(holder); 

     //this is where i get the error 
veck[0]->surface = loadimage(); 
veck[0]->rect.w = veck[0]->surface->w; 
veck[0]->rect.h = veck[0]->surface->h; 

//if commented out this is where i get my second error 
veck.push_back(new picture); 
veck[1]->rect.x = 39; 
veck[1]->rect.y = 49; 
veck[1]->surface = veck[0]->surface; 
veck[1]->rect.w = veck[0]->surface->w; 
veck[1]->rect.h = veck[0]->surface->h; 
veck[0]->rect.x = 500; 
veck[0]->rect.y = 200; 

    //printing to screan 

TTF_Font *font = NULL; 
Mix_Chunk *sound = NULL; 

picture *picture1;  

//if commented out again this is where i get my third error in sound 
sound = Mix_LoadWAV("sound/walking in grass.wav"); 
font = TTF_OpenFont("fonts/CaviarDreams.ttf", 100); 


    while (programisrunning()){ 

    //do SDL stuff herre 

    } 

    SDL_Delay(3000); 
    SDL_Quit(); 
    TTF_Quit(); 
    Mix_CloseAudio(); 

    int t; 
    std::cin >> t; 
    return 0; 
} 

/////定义

SDL_Surface *loadimage(){ 
    veck.push_back(new picture); 

    SDL_Surface* rawimage = NULL; 
    SDL_Surface* processedimage = NULL; 

    veck[0]->filepath = "pics/walk 3.png"; 
    rawimage = IMG_Load(veck[0]->filepath.c_str()); 

    if (rawimage == NULL){ 
     errorreport("image 'walk 3.png' failed to load\n"); 
     return false; 
    } 

    processedimage = SDL_DisplayFormat(rawimage); 
    SDL_FreeSurface(rawimage); 

    if (processedimage == NULL){ 
     errorreport("image 'walk 3.png' failed to process\n"); 
     return false; 
    } 

    Uint32 colorkey = SDL_MapRGB(processedimage->format, 255, 255, 255); 
    SDL_SetColorKey(processedimage, SDL_SRCCOLORKEY, colorkey); 

// EDIt 
     if (processedimage == NULL) 
    errorreport("ERRRORORROROOR BUT WHY\n"); 



    return processedimage; 
} 

我知道它不是做事情的最佳途径,但这是我的测试项目在sdl中做东西。如果我注释洞veck [0]的东西,然后我得到相同的错误,但在veck.pushback()进一步下降,如果我注释掉所有vecks然后我得到声音的错误。如果有帮助的话,我正在使用visual studio 2013。我不明白我做错了什么。

我也切出的东西,我认为是没有意义在这里

+0

你永远不会验证可以返回NULL指针的loadimage返回。 – dkackman

+0

为什么在'loadimage()'中使用'veck'?在我看来,你只需要一个字符串... – vu1p3n0x

+0

@dkackman如果我把“if(processedimage == NULL)”在loadimage()返回之前它是一样的 –

回答

2

你有veck[0]->surface = loadimage();因为veck[0]直到loadimage()已执行不会得到一个对象未​​定义的行为增加。

无法保证loadimage()将在veck[0]被评估之前调用。

+0

以及如何从loadimage()然后获得值。我从来没有在调试器中遇到过这个问题,即使它返回了一个空值,它不应该导致错误检查,为什么会导致一个问题,它是一个指针。 –

+0

@ Y.mada考虑在'loadimage()'中创建一个'picture',并将其返回而不是 – vu1p3n0x

+0

@ vu1p3n0x它的工作,非常感谢。不知道为什么我放弃投票寿 –