【问题标题】:C++ SDL Unhandled exception at 0x6C7DB2AA (SDL2.dll) in "File".exe: 0xC0000005: Access violation reading location 0x00000044“文件”.exe 中 0x6C7DB2AA (SDL2.dll) 处的 C++ SDL 未处理异常:0xC0000005:访问冲突读取位置 0x00000044
【发布时间】:2014-05-02 01:39:30
【问题描述】:

加入 SDL 我是按照 DreamInCode 上的“StaysCrisp”教程进行说明的。问题是当我尝试使用其中一种绘制方法时。我得到错误:

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

问题出在 Sprite 类的 draw 方法中。所以这里是代码。另外,我使用的是最新版本的 SDL(2.0.3),我可以从代码中看出 StaysCrisp 不是。

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

}

【问题讨论】:

  • 你可以找到我用于教程的链接here
  • 访问的地址 (0x00000044) 表明访问是通过指向该结构的空指针访问该结构的成员。您是否检查过表面是否都有效?
  • @JeffreyHantin 是的,它们似乎是“有效的”。
  • 您如何获得目标表面?在他的代码中,他使用 SDL_SetVideoMode(...) 来获取 SDL_Surface 但这不在 SDL2 中。他的代码看起来像 SDL 1.2。

标签: c++ memory sdl game-development


【解决方案1】:

我能够重现您的崩溃,

去掉这一行:

SDL_FreeSurface(temp);

temp和optimized指向同一个资源,所以释放一个意味着它们现在都指向垃圾。

在他的代码中,他调用了一个函数,该函数显然分配了该内存的一些副本,而在您的代码中,您只需分配不起作用的指针。

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

除此之外,我不确定这段代码会产生什么有用的东西,因为这段代码看起来像是为 SDL 1.2 编写的(SDL1.2 和 SDL2,0 是完全不同的野兽,您将无法混合和匹配代码除非你真的知道自己在做什么)。

有 2.0 的教程,我会看看我是否可以为你挖掘它们的位置。 (http://lazyfoo.net/tutorials/SDL/index.php)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 2013-04-05
    • 1970-01-01
    相关资源
    最近更新 更多