【问题标题】:I am getting an "Access violation reading location" error when running in release mode - C++在发布模式下运行时出现“访问冲突读取位置”错误 - C++
【发布时间】:2017-08-11 02:36:42
【问题描述】:

我已经尝试到处寻找答案,但无济于事。 在调试器模式下它工作得很好,但是当我把它置于发布模式时它给了我这个

sdl project.exe 中 0x00DC1814 处未处理的异常:0xC0000005:访问冲突读取位置 0x00000000。

我的调试器和发布模式包含和库相同,甚至我的子系统也相同。这是我的代码,我把它缩短了一点

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 Express 2013。我不明白我做错了什么。

我确实删掉了我认为在此处添加毫无意义的内容

【问题讨论】:

  • 你永远不会验证 loadimage 的返回值,它可以返回一个 NULL 指针。
  • 你为什么在loadimage()中使用veck?在我看来你只需要一个字符串...
  • @dkackman 如果我在 loadimage() 中的返回之前放置“if (processedimage == NULL)”,它是相同的
  • @vu1p3n0x 试图将所有内容存储在一个容易找到的地方以备后用
  • @Y.mada 您也可以调试发布配置中内置的程序。您已经有发生无效操作的地址;调试器很可能会给你一个完整的堆栈跟踪。您确定您的errorreport 确实以您可以看到的方式传递信息吗?

标签: c++ sdl access-violation release-mode


【解决方案1】:

您在veck[0]-&gt;surface = loadimage(); 中有未定义的行为,因为veck[0] 在loadimage() 执行之前不会获得对象。

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

【讨论】:

  • 那么我如何从 loadimage() 中获取值。我在调试器中从来没有遇到过这个问题,即使它确实返回了一个它不应该导致错误的 null 检查为什么会导致问题,它是一个指针。
  • @Y.mada 考虑在loadimage() 中创建一个picture 并将其返回
  • @vu1p3n0x 成功了,非常感谢。不知道为什么我被否决了
猜你喜欢
  • 2013-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多