-4

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

Y. mada
  • 1
  • 3
  • you never verify the return from loadimage which can return a NULL pointer. – dkackman Aug 11 '17 at 02:45
  • why do you use `veck` in `loadimage()`? looks to me that you only need a string... – kmdreko Aug 11 '17 at 02:47
  • @dkackman if i put "if (processedimage == NULL)" before the return in loadimage() it is the same – Y. mada Aug 11 '17 at 02:48
  • @vu1p3n0x was trying to store everything in a easy to find place for later use – Y. mada Aug 11 '17 at 02:51
  • @Y.mada you can debug programs built in release configuration too. You already have address where invalid operation happened; it is very likely debugger will give you a full stacktrace. Are you sure your `errorreport` actually gives a message in a way that you can see? – keltar Aug 11 '17 at 03:13

1 Answers1

2

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.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • well how can i get the value from loadimage() then. i have never had that problem in debugger and even if it does return a null which it should not cause of the error checks why would that cause a problem, it is a pointer. – Y. mada Aug 11 '17 at 02:52
  • @Y.mada consider creating a `picture` in `loadimage()` and return it instead – kmdreko Aug 11 '17 at 02:55
  • @vu1p3n0x it worked, thanks alot. not sure why i got down voted tho – Y. mada Aug 11 '17 at 03:17