1

我一直在玩弄这个代码,youtube 上的一个人附在他的一个教程视频中……我遇到了一些我似乎无法解决的问题……当前的问题是这个“EXC_BAD_ACCESS”错误。 ..根据我在网上简要研究的内容,当您不正确地使用堆栈并将内存分配给不再存在的东西或类似的东西时,就会发生这些错误......我只是无法准确指出我做错了什么...... . 我已经完成了一些故障排除——但对于 xcode 5 还没有

该错误出现在名为“cSprite.cpp”的类中:

// ~~~~~~~~~~~~~~~~~~~     cSprite.cpp      ~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include "stdafx.h"
#include "cSprite.h"
//#include "gMain.h"
//#include <SDL2/SDL_main.h>
//#include <SDL2/SDL.h>
//#include "cSDL_Setup.h"

using namespace std;

void draw(SDL_Renderer, SDL_Texture, SDL_Rect);

cSprite::cSprite(SDL_Renderer* passed_renderer, string filePath, int x, int y, int w, int h)
{
    renderer = passed_renderer;

    //... image
    image = NULL;
    image = IMG_LoadTexture(renderer,filePath.c_str());

    // image error message
    if (image == NULL)
    {
        cout<<"Couldnt load image..."<<filePath.c_str()<<endl;
    }

    //image dimensions
    rect->x = x;            // ***** Error: Thread 1:EXC_BAD_ACCESS(code=EXC_1386_GPFLT)
    rect->y = y;
    rect->w = w;
    rect->h = h;

};

cSprite::~cSprite(void)
{
    SDL_DestroyTexture(image);
}

//get methods
//SDL_Rect* cSprite::getRect()
//{
//    return rect;
//}

//get methods
//SDL_Texture* cSprite::getImage()
//{
//    return image;
//}

void cSprite::draw()
{
    SDL_RenderCopy(renderer, image, NULL, rect);
}

... 'rect->x = x;' 行

得到我放在它旁边的错误...但是我知道还有什么可能导致它...我希望有人可以解释为什么甚至会发生 EXC_BAD_ACCESS 错误...和/或在我的其余代码中它可能来自哪里...

这是我的其他 3 个 .cpp 文件

……

// ~~~~~~~~~~~~~~~~~~~~~~~~        gMain.cpp        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <iostream>
#include "stdafx.h"
#include "gMain.h"


using namespace std;

gMain::gMain(int passed_screenWidth, int passed_screenHeight)
{
    screenWidth = passed_screenWidth;
    screenHeight = passed_screenHeight;

    // quit boolean
    quit = false;

    // create instance of cSDL class
    csdl_setup = new cSDL_Setup(&quit, screenWidth, screenHeight);

    grass = new cSprite(csdl_setup->getRenderer(), "/Users/jamesbryant/Desktop/nuGame/nuGame/images.jpeg", screenWidth, screenHeight, screenWidth, screenHeight);

    bruce_Lee = new cSprite(csdl_setup->getRenderer(), "/Users/jamesbryant/Desktop/nuGame/nuGame/lee.bmp", 300, 300, 200, 200);


}

gMain::~gMain(void)
{
}

void gMain::gameLoop(void)
{
    // game loop logic
    while (!&quit && csdl_setup->getMainEvent() ->type != SDL_QUIT)
    {
        csdl_setup->begin();

        grass->draw();
        bruce_Lee->draw();


        csdl_setup->end();

    }
}

……

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ cSDL_Setup.cpp ~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include "stdafx.h"
#include "cSDL_Setup.h"
//#include "gMain.h"

using namespace std;

cSDL_Setup::cSDL_Setup(bool* quit, int screenWidth, int screenHeight)
{
    // create window
    window = NULL;
    window = SDL_CreateWindow("rWDY_pWDR", 400, 400, screenWidth, screenHeight, SDL_WINDOW_RESIZABLE);

    // if window couldnt be created...
    if (window == NULL)
    {
        cout<<"Window couldnt be created..."<<endl;

        *quit = true;
        //exit(0);
    }

    //create renderer
    renderer = NULL;
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    // initialize images
    mainEvent = new SDL_Event();

}

cSDL_Setup::~cSDL_Setup(void)
{
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    delete mainEvent;
}

//get methods
SDL_Renderer* cSDL_Setup::getRenderer()
{
    return renderer;
}

SDL_Event* cSDL_Setup::getMainEvent()
{
    return mainEvent;
}

void cSDL_Setup::begin()
{
    SDL_PollEvent(mainEvent);
    SDL_RenderClear(renderer);
}

void cSDL_Setup::end()
{
    SDL_RenderPresent(renderer);

}

……

// ~~~~~~~~~~~~~~~~~~~~~~~~~~ firstGame.cpp ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include "stdafx.h"
#include "cSDL_Setup.h"
//#include "gMain.h"

using namespace std;

cSDL_Setup::cSDL_Setup(bool* quit, int screenWidth, int screenHeight)
{
    // create window
    window = NULL;
    window = SDL_CreateWindow("rWDY_pWDR", 400, 400, screenWidth, screenHeight, SDL_WINDOW_RESIZABLE);

    // if window couldnt be created...
    if (window == NULL)
    {
        cout<<"Window couldnt be created..."<<endl;

        *quit = true;
        //exit(0);
    }

    //create renderer
    renderer = NULL;
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    // initialize images
    mainEvent = new SDL_Event();

}

cSDL_Setup::~cSDL_Setup(void)
{
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    delete mainEvent;
}

//get methods
SDL_Renderer* cSDL_Setup::getRenderer()
{
    return renderer;
}

SDL_Event* cSDL_Setup::getMainEvent()
{
    return mainEvent;
}

void cSDL_Setup::begin()
{
    SDL_PollEvent(mainEvent);
    SDL_RenderClear(renderer);
}

void cSDL_Setup::end()
{
    SDL_RenderPresent(renderer);

}

...这是我的 cSprite.h 文件现在的样子:

#ifndef __game__cSprite__
#define __game__cSprite__
#pragma once

#include <iostream>
#include <SDL2/SDL_main.h>
#include <SDL2/SDL.h>

using namespace std;


class cSprite
{
public:
    cSprite(SDL_Renderer* passed_renderer, string filePath, int x, int y, int w, int h);
    ~cSprite(void);

    void draw();

private:
    SDL_Texture* image = NULL;

    SDL_Rect* rect = NULL;

    SDL_Renderer* renderer = NULL;

};

#endif /* defined(__game__cSprite__) */
4

1 回答 1

1

您没有发布 cSprite 的标题。但是,我想那rect是一个SDL_Rect*.

得到错误的函数是cSprite::cSprite(),即。构造函数。它的职责之一是将所有类成员初始化为合理的值。

但是,我没有看到任何初始化rect为指向任何东西的代码。您当前遇到的错误表明您正在取消引用 bum 指针,这与此观察结果一致。

您要么需要分配一个SDL_Rect来分配给rect,要么您应该更改rect为 只是SDL_Rect,而不是SDL_Rect*。如果您进行后者更改,那么您还需要更改所有rect->to ,并在需要它的 SDL 调用中 rect.传递一个指针(即。 )rectSDL_RenderCopy(renderer, image, NULL, &rect);

于 2013-10-28T13:05:47.310 回答