_Block_Type_Is_Valid(pHead-> nBlockUse)错误 [英] _Block_Type_Is_Valid (pHead->nBlockUse) Error

查看:143
本文介绍了_Block_Type_Is_Valid(pHead-> nBlockUse)错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个新项目中工作,但遇到一个问题,我无法看到为什么会失败。

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

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

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

这是源代码:

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 / p>

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
$ b

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 / p>

Font.cpp

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

CGameApplication.cpp
$ b

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时,我使用一个复制构造函数,所以不同的指针得到复制没有问题。

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.

推荐答案

只需使用 std :: string 。这个错误意味着你双删除的东西,或类似的东西,如果你没有管理自己的内存,你不会有的问题。您的代码散落着内存泄漏和其他错误,你不会有 std :: string

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.

这篇关于_Block_Type_Is_Valid(pHead-> nBlockUse)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆