简体   繁体   中英

I am getting an "Access violation reading location" error when running in release mode - C++

I have tried looking all over the place for an answer but to no avail. in debugger mode it worked fine but when i put it in release mode it give me this

Unhandled exception at 0x00DC1814 in sdl project.exe: 0xC0000005: Access violation reading location 0x00000000.

i have my debugger and release mode include and library the same, even my subsystems are the same. here is my code, i have shorten it by a bit

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;
}

/////definitions

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;
}

i know its not in the best way of doing things but this is my test project for doing stuff in sdl. if i comment out the hole veck[0] thing then i get the same error but at further down in veck.pushback() and if i comment out all the vecks then i get the error at sound. i am using visual studio exress 2013 if that helps at all. i don't understand what i am doing wrong.

i did cut out stuff i thought was pointless to add here

You have undefined behavior in veck[0]->surface = loadimage(); since veck[0] won't get an object until loadimage() has executed.

There's no guarantee that loadimage() will be called before veck[0] is evaluated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM