簡體   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