_Block_Type_Is_Valid (pHead->nBlockUse) 错误
_Block_Type_Is_Valid (pHead->nBlockUse) Error
我一直在一个新项目中工作,但我遇到了一个问题,我不明白为什么会失败。
当我执行这一行时,删除文本Y会给我错误_Block_Type_Is_Valid(pHead->nBlockUse)。那么我做错了什么?
这是源代码:
文本.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
文本.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
中,而是简单地分配指针!不过,您可以在副本ctor中进行更正。现在,考虑这个例子:
class Foo{
public:
Foo(char* text){
str = text;
}
~Foo(){
delete str;
}
private:
char* str;
};
int main(){
Foo f("hi");
}
C++03(为了向后兼容…)允许文字字符串("hi"
)绑定到非常量的char*
指针,如本代码所示。值得庆幸的是,C++11修复了这个问题,它实际上不应该再编译了。现在,删除文本字符串显然不起作用,因为该字符串被放在.exe的只读部分,因此不能使用delete
。如果您从文字字符串实例化一个Text
对象,我想这就是错误的来源。
请注意,如果您从堆栈上创建的char[]
创建它,也会发生这种情况:
char text[] = "hi";
Foo f(text);
因为CCD_ 12现在将尝试CCD_。
另一种可能发生这种情况的情况是,如果您双重删除一个对象:
char* text = new char[3];
Foo f(text);
delete text;
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- C 字符串比较“祝您好运”&gt;“再见”
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- C 操作员&gt;&gt;与突变器过载
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 是否需要使用 - &gt;运算符在C 中调用成员函数时
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- 错误c++visual studio c2227左侧'->;Init';必须指向类/结构/联合/泛型类型
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- ``这个''不能用this-&gt;指针变量
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 调试断言失败 BLOCK_TYPE_IS_VALID (pHead->nblockuse) 从解构函数
- 使用shared_ptr<字符串>转换为一个无序集合<字符串>
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾