繁体   English   中英

C ++ SDL在“文件”中的0x6C7DB2AA(SDL2.dll)处的未处理异常.exe:0xC0000005:访问冲突读取位置0x00000044

[英]C++ SDL Unhandled exception at 0x6C7DB2AA (SDL2.dll) in “File”.exe: 0xC0000005: Access violation reading location 0x00000044

进入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;

}

我能够重现你的崩溃,

摆脱这条线:

SDL_FreeSurface(temp);

temp和optimize指向同一个资源,因此释放一个意味着它们现在都指向垃圾。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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