删除ptr时堆损坏
Heap corruption when deleting ptr
我有以下类:
class Label : public Object
{
public:
Label ();
~Label ();
void create (const unsigned int x, const unsigned int y, const wchar_t* text);
void destroy ();
private:
unsigned int x, y;
wchar_t* text;
void draw (HDC hdc);
void confirmed (ObjectManager* m);
};
,代码如下:
Label::Label ()
{
type = LABEL;
text = NULL;
}
Label::~Label ()
{
destroy ();
}
void Label::create (const unsigned int x, const unsigned int y, const wchar_t* text)
{
unsigned int len = wcslen (text);
this->x = x;
this->y = y;
this->text = new wchar_t[len];
wcscpy (this->text, text);
}
void Label::destroy ()
{
if (text) {
delete[] text;
text = NULL;
}
if (m) {
m->remove (this);
m = NULL;
}
}
void Label::draw (HDC hdc)
{
if (text)
TextOut (hdc, x, y, text, wcslen (text));
}
void Label::confirmed (ObjectManager* m)
{
this->m = m;
}
退出应用程序时,Visual Studio报告堆损坏。我首先调用"创建",然后调用"确认",然后调用"绘制",最后调用解构器。文本初始化正确,所以我不知道这个代码中的问题是什么。有人能解释一下是怎么回事吗?当调用"delete[] text"时,会发生堆损坏。
wcslen -返回不包括 的字符个数
unsigned int len = wcslen (text);
this->text = new wchar_t[len + 1];
见http://www.cplusplus.com/reference/cwchar/wcslen/
相关文章:
- 为什么会发生堆损坏
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 平均图像时图像损坏
- 如何针对特定情况调试和修复此双自由内存损坏问题
- CLANG 编译器 说:变量"PTR"可能未初始化
- 为什么C中的通用链表中存储的数据已损坏
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- gdb错误:Backtrace已停止:上一帧与此帧相同(堆栈已损坏?)
- C++双重释放或损坏(out)
- 捕获标准输出以压缩并使用 CTRL-C 中断会给出损坏的 zip 文件
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 使用全局声明的向量时,C++双重释放错误/损坏
- 变量周围的堆栈'...'已损坏
- 运行时检查失败 #2 变量"A"周围的堆栈已损坏
- 为共享 ptr 向量实现复制 c'tor?
- 字符和整数中 **(ptr+1) 的值差异
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- 检测到堆损坏:在正常块 c++ 动态 2D 数组之后
- 为什么这个显式析构函数会导致共享ptr中的内存损坏
- 删除ptr时堆损坏