_Block_Type_Is_Valid(pHead-> nBlockUse)错误

时间:2011-06-18 17:05:27

标签: c++ debugging visual-c++ pointers sdl

我一直在从事一个新项目,但遇到一个我无法理解为什么会失败的问题。

当我执行此行时删除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时,我使用了一个复制构造函数,因此不同的指针可以没有问题地复制。

2 个答案:

答案 0 :(得分:12)

只需使用std::string即可。这个错误意味着你删除了一些东西,或类似的东西,如果你没有管理自己的记忆就不会有的问题。您的代码中充斥着内存泄漏和std::string未提供的其他错误。

答案 1 :(得分:10)

从我所看到的,该错误与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;