_Block_Type_Is_Valid(pHead-> nBlockUse)错误
||
我一直在一个新项目中工作,但是遇到一个我看不到为什么失败的问题。
当我执行此行时,删除textY给我错误_Block_Type_Is_Valid(pHead-> nBlockUse)。那我在做什么错?
这是源代码:
文字
#ifndef TEXT_H
#define TEXT_H
typedef boost::shared_ptr<Font> FontPtr;
class Text
{
public:
Text(FontPtr font, char *text)
{
str = new char[35];
this->font = font; str = text;
}
Text(const Text& cSource);
Text& operator=(const Text& cSource);
~Text();
.
.
.
.
private:
FontPtr font;
char *str;
GLuint texture;
GLfloat pos_x, pos_y, width, height;
};
#endif
Text.cpp
Text::Text(const Text& cSource)
{
font = cSource.font;
texture = cSource.texture;
pos_x = cSource.pos_x;
pos_y = cSource.pos_y;
width = cSource.width;
height = cSource.height;
int sizeString = 35;
if (cSource.str)
{
str = new char[sizeString];
strncpy(str, cSource.str, sizeString);
}
else
{
str = 0;
}
}
Text& Text::operator=(const Text& cSource)
{
delete[] str;
font = cSource.font;
texture = cSource.texture;
pos_x = cSource.pos_x;
pos_y = cSource.pos_y;
width = cSource.width;
height = cSource.height;
int sizeString = 35;
if (cSource.str)
{
str = new char[sizeString];
strncpy(str, cSource.str, sizeString);
}
else
{
str = 0;
}
return *this;
}
Text::~Text()
{
delete[] str;
}
字体
#ifndef FONT_H
#define FONT_H
class Font
{
public:
Font(TTF_Font *font, SDL_Color color)
{
this->font = font; this->color = color;
}
~Font();
.
.
.
private:
TTF_Font *font;
SDL_Color color;
};
#endif
Font.cpp
Font::~Font()
{
TTF_CloseFont(font);
}
CGameApplication.cpp
.
.
.
.
void CGameApplication::initializeApplicationFonts()
{
TTF_Font* font;
SDL_Color color;
font = TTF_OpenFont(\"test.ttf\", 15);
color.r = color.g = color.b = 255;
GApp->addFont(font, color);
Text *text = new Text(GApp->getFonts().at(0), \" \");
text->setTexture( CTextM->textToGLTexture(GApp->getFonts().at(0), text) );
text->setPosX(20); text->setPosY(20);
GApp->addText(new Text(*text));
Text *textY = new Text(GApp->getFonts().at(0), \" \");
textY->setTexture( CTextM->textToGLTexture(GApp->getFonts().at(0), textY) );
textY->setPosX(80); textY->setPosY(20);
GApp->addText(new Text(*textY));
delete textY; //-----> This line crashes the program with that error
}
.
.
.
GameApp.h
#ifndef GAMEAPP_H
#define GAMEAPP_H
class GameApp
{
public:
GameApp(){
}
//~GameApp();
void addFont(TTF_Font *font, SDL_Color color) {
vFonts.push_back(FontPtr( new Font(font, color) ) ); }
vector<FontPtr> getFonts() { return vFonts; }
void addText(Text *text) {
vTexts.push_back(new Text(*text));}
private:
SDL_Surface *gameMainSurface;
vector<Image*> vImages;
std::vector<FontPtr> vFonts;
vector<Text*> vTexts;
vector<Tile*> vTiles;
Map *currentMap;
};
#endif
所以我认为问题是当我销毁对象textY时,指向TTF_Font的指针也被销毁了。但是我不确定,因为当我在向量中添加对象Text时,我使用了复制构造函数,因此不同的指针获得了复制而没有问题。
没有找到相关结果
已邀请:
2 个回复
门锑腺潮
。该错误意味着您删除了一些东西,或者类似的东西,如果您不管理自己的内存,则不会遇到这个问题。您的代码中充斥着内存泄漏和
所没有的其他错误。
唤副埂侧壬
的默认ctor有关。您使用一个
指针,为该字符串分配空间,但实际上并没有将
复制到
中,而只是分配了指针!不过,您可以在复制控制器中正确地进行操作。现在,考虑以下示例:
C ++ 03(用于向后兼容...)允许文字字符串(
)绑定到非常量
指针,如该代码所示。值得庆幸的是,C ++ 11修复了该问题,实际上它应该不再编译。现在,删除文字字符串显然是行不通的,因为该字符串位于.exe的只读部分中,因此无法进行“ 15”操作。如果您从文字字符串中实例化一个“ 8”对象,我想这就是您错误的出处。 请注意,如果从堆栈上创建的
创建它,也会发生这种情况:
因为ѭ19现在将尝试ѭ15堆叠对象。 可能发生这种情况的另一种情况是,如果您双重删除了一个对象: