_Block_Type_Is_Valid (pHead->nBlockUse) 错误

_Block_Type_Is_Valid (pHead->nBlockUse) Error

本文关键字:gt nBlockUse 错误 pHead- Block Type Is Valid      更新时间:2023-10-16

我一直在一个新项目中工作,但我遇到了一个问题,我不明白为什么会失败。

当我执行这一行时,删除文本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;