_Block_Type_Is_Valid(pHead-> nBlockUse)错误

osc*_*rpr 18 c++ debugging pointers sdl visual-c++

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

当我执行此行时删除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 
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Font.cpp

Font::~Font()
{
    TTF_CloseFont(font);
}
Run Code Online (Sandbox Code Playgroud)

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
}
.
.
.
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

所以我认为问题在于,当我销毁对象textY时,指向TTF_Font的指针被销毁.但我不确定,因为当我在向量中添加一个对象Text时,我使用了一个复制构造函数,因此不同的指针得到了复制而没有问题.

Pup*_*ppy 11

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

  • "如果你没有管理自己的记忆就不会有这个问题"我讨厌垃圾收集器和引用计数领域的这种情绪,这种情况更难以调试.确定字符串是一种特殊情况,但这里正确的答案是"如果你正确管理你的记忆就不会有问题". (5认同)

Xeo*_*Xeo 10

从我所看到的,错误与默认的ctor有关Text.你接受一个char*指针,为字符串分配空间,但实际上并没有复制textstr,但只是分配指针!你可以在复制文件中更正.现在,考虑这个例子:

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

    ~Foo(){
        delete str;
    }

private:
    char* str;
};

int main(){
    Foo f("hi");
}
Run Code Online (Sandbox Code Playgroud)

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

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

char text[] = "hi";
Foo f(text);
Run Code Online (Sandbox Code Playgroud)

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

可能发生这种情况的另一种情况是双重删除对象:

char* text = new char[3];
Foo f(text);
delete text;
Run Code Online (Sandbox Code Playgroud)