[英]Expression _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) Error
[英]_Block_Type_Is_Valid (pHead->nBlockUse) Error
我一直在一個新項目工作,但我遇到一個問題,我不明白為什么失敗。
當我執行此行時刪除textY給我錯誤_Block_Type_Is_Valid(pHead-> nBlockUse)。 那么我做錯了什么?
這是源代碼:
Text.h
#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;
}
Font.h
#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時,我使用了一個復制構造函數,因此不同的指針得到了復制而沒有問題。
只需使用std::string
。 這個錯誤意味着你刪除了一些東西,或類似的東西,如果你沒有管理自己的記憶就不會有的問題。 您的代碼中充斥着內存泄漏和std::string
不具備的其他錯誤。
從我所看到的,錯誤與Text
的默認ctor有關。 你接受一個char*
指針,為字符串分配空間,但實際上並沒有將text
復制到str
,而是簡單地指定指針! 你可以在復制文件中更正。 現在,考慮這個例子:
class Foo{
public:
Foo(char* text){
str = text;
}
~Foo(){
delete str;
}
private:
char* str;
};
int main(){
Foo f("hi");
}
C ++ 03(用於向后兼容性......)允許文字字符串( "hi"
)綁定到非const char*
指針,如此代碼所示。 謝天謝地,C ++ 11解決了這個問題,實際上這應該不再編譯了。 現在,刪除文字字符串顯然不起作用,因為字符串放在.exe的只讀部分,因此不能delete
。 我猜這是你的錯誤來自哪里,如果你從文字字符串實例化一個Text
對象。
請注意,如果您從堆棧上創建的char[]
創建它,也會發生這種情況:
char text[] = "hi";
Foo f(text);
因為Foo
現在會嘗試delete
堆棧對象。
可能發生這種情況的另一種情況是雙重刪除對象:
char* text = new char[3];
Foo f(text);
delete text;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.