C++ SDL Unhandled exception at 0x6C7DB2AA (SDL2.dll) in "File".exe: 0xC0000005: Access violation reading location 0x00000044

StackOverflow https://stackoverflow.com/questions/23419493

문제

Getting in to SDL I stated following the tutorials "StaysCrisp" on DreamInCode. The problem is that when I am trying to use one of the draw methods. I get the Error :

Unhandled exception at 0x6C7DB2AA (SDL2.dll) in Tutorial.exe:
0xC0000005: Access violation reading location 0x00000044. 

The problem is in the Sprite class draw method. So here's the code. Also on a side note I am using the latest version of SDL(2.0.3), and I can tell from the code that StaysCrisp is not.

#include "Sprite.h"

//constructor
Sprite::Sprite()
{

}

SDL_Surface*Sprite::Load(char*File)
{
    SDL_Surface*temp = NULL;
    SDL_Surface*optimized = NULL;

    if ((temp = IMG_Load(File)) == NULL)
    {
        return NULL;
    }

    optimized = temp;
    SDL_FreeSurface(temp);

    return optimized;
}

bool Sprite::Draw(SDL_Surface*dest, SDL_Surface*src, int x, int y)
{
    if (dest == NULL || src == NULL)
    {
        return false;
        std::cout << "Could not draw the entire surface!\n ERROR: SPRITE.CCP";
    }

    SDL_Rect destR;

    destR.x = x;
    destR.y = y;

    SDL_BlitSurface(src, NULL, dest, &destR); //Compiler says the problem is here

    return true;

}

bool Sprite::Draw(SDL_Surface*dest, SDL_Surface*src, int x, int y,
    int x2, int y2, int width, int height)
{
    if (dest == NULL || src == NULL)
    {
        std::cout << "Could not draw sprite. SPRITE.CCP \n";
        return false;
    }

    SDL_Rect destR;

    destR.x = x;
    destR.y = y;


    SDL_Rect srcR;

    srcR.x = x2;
    srcR.y = y2;
    srcR.w = width;
    srcR.h = height;

    SDL_BlitSurface(src, &srcR, dest, &destR); //Compiler says the problem is here

    return true;

}
도움이 되었습니까?

해결책

I was able to reproduce your crash,

Get rid of this line:

SDL_FreeSurface(temp);

temp and optimized point to the same resource ,so freeing one means they both point to garbage now.

In his code he called a function which evidently allocated some copy of that memory, in yours you simply assign the pointer which wont work.

optimized = SDL_DisplayFormatAlpha(temp); //this will create a copy
SDL_FreeSurface(temp);

Aside from that I am not sure this code will produce anything useful as this code looks like it was written for SDL 1.2 (SDL1.2 and SDL2,0 are sufficiently different beasts that you will not be able to mix and match code unless you really know what you are doing).

There are tutorials for 2.0 , i will see if i can dig up where they are for you. ( http://lazyfoo.net/tutorials/SDL/index.php)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top