gpt4 book ai didi

c++ - exc_bad_access 错误 xcode5 c++ sdl2

转载 作者:太空宇宙 更新时间:2023-11-04 11:46:38 25 4
gpt4 key购买 nike

我一直在玩弄这段代码,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__) */

最佳答案

您没有为 cSprite 发布标题。但是,我认为 rect 是一个 SDL_Rect*

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

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

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

关于c++ - exc_bad_access 错误 xcode5 c++ sdl2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19627474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com