C++ _Block_Type_Is_Valid (pHHead->nBlockUse) 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6397545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:02:11  来源:igfitidea点击:

_Block_Type_Is_Valid (pHead->nBlockUse) Error

c++debuggingvisual-c++pointerssdl

提问by oscar.rpr

I been working in a new project but I encounter with a problem which I can't see why fail.

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

When I perfom this line delete textY give me the error _Block_Type_Is_Valid (pHead->nBlockUse). So what am I doing wrong?

当我执行这一行时,删除 textY 给我错误 _Block_Type_Is_Valid (pHead->nBlockUse)。那么我做错了什么?

This is the source code:

这是源代码:

Text.h

文本.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

字体.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

字体.cpp

Font::~Font()
{
    TTF_CloseFont(font);
}

CGameApplication.cpp

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

So I think the problem is that when I destroy the object textY, the pointer to the TTF_Font is destroyed. But I'm not sure because when I add a object Text in the vector I use a copy-constructor so the different pointers got copy without problems.

所以我认为问题是当我销毁对象textY时,指向TTF_Font的指针被销毁了。但我不确定,因为当我在向量中添加一个对象 Text 时,我使用了一个复制构造函数,所以不同的指针可以毫无问题地复制。

采纳答案by Puppy

Just use a std::string. That error means that you double deleted something, or something like that, a problem that you wouldn't have if you didn't manage your own memory. Your code is littered with memory leaks and other bugs that you won't have with std::string.

只需使用一个std::string. 这个错误意味着你重复删除了一些东西,或者类似的东西,如果你不管理自己的内存,你就不会遇到这个问题。你的代码充满了内存泄漏和其他你不会有的错误std::string

回答by Xeo

From what I can see, the error has to do with the default ctor for Text. You take in a char*pointer, allocate space for the string, but don't actually copy the textinto str, but simply assign the pointer! You do it correct in the copy ctor though. Now, consider this example:

据我所知,该错误与Text. 您接收一个char*指针,为字符串分配空间,但实际上并不复制textinto str,而是简单地分配指针!不过,您在复制 ctor 中做对了。现在,考虑这个例子:

class Foo{
public:
    Foo(char* text){
        str = text;
    }

    ~Foo(){
        delete str;
    }

private:
    char* str;
};

int main(){
    Foo f("hi");
}

C++03 (for backwards compatability...) allows literal strings ("hi") to bind to non-const char*pointers, as seen in this code. C++11 thankfully fixed that and this should actually no longer compile. Now, deleting a literal string obviously doesn't work, as the string is placed in the read-only section of the .exe and as such isn't deleteable. I guess this is where your error comes from, if you instantiate a Textobject from a literal string.

C++03(为了向后兼容...)允许文字字符串 ( "hi") 绑定到非常量char*指针,如此代码所示。谢天谢地,C++11 修复了这个问题,这实际上应该不再编译。现在,删除文字字符串显然不起作用,因为该字符串位于 .exe 的只读部分,因此无法删除delete。如果您Text从文字字符串实例化一个对象,我想这就是您的错误的来源。

Note that this also happens if you create it from a char[]created on the stack:

请注意,如果您从char[]堆栈上创建的对象创建它,也会发生这种情况:

char text[] = "hi";
Foo f(text);

as the Foowill now try to deletea stack-object.

因为Foo现在将尝试delete堆栈对象。

Another case where this might happen is if you double-delete an object:

另一种可能发生这种情况的情况是,如果您双击删除一个对象:

char* text = new char[3];
Foo f(text);
delete text;