Tôi đã làm việc trong một dự án mới nhưng tôi gặp phải một vấn đề mà tôi không thể thấy tại sao không thành công._Block_Type_Is_Valid (pHead-> nBlockUse) Lỗi
Khi tôi perfom dòng này xóa textY cho tôi lỗi _Block_Type_Is_Valid (pHead-> nBlockUse). Vậy tôi đang làm gì sai?
Đây là mã nguồn:
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
Vì vậy, tôi nghĩ rằng pro blem là khi tôi phá hủy đối tượng textY, con trỏ tới TTF_Font bị hủy. Nhưng tôi không chắc chắn vì khi tôi thêm một đối tượng Văn bản trong vectơ, tôi sử dụng một bản sao-constructor để các con trỏ khác nhau được sao chép mà không có vấn đề gì.
Cậu đi bộ lên các cuộc gọi stack để xem đó là cuộc gọi cuối cùng trong mã của bạn gây ra sự thất bại gỡ lỗi? (Ngoài ra, Raw Con trỏ trong container STL! Mắt tôi! :)) –
Vâng, nó chỉ là một thử nghiệm trong khi tôi thực hiện con trỏ thông minh trong tất cả các container STL. Tôi sẽ kiểm tra ngăn xếp cuộc gọi. Cảm ơn. –