vc++调试断言在程序退出时失败
VC++ debug assertion failed on program exit
我正在尝试创建一个链表类模板(是的,我知道c++库中有一个,但我想创建自己的乐趣)。我已经跟踪了代码,直到程序退出,一切似乎都很正常。
下面是使用的代码:
list.h:
#ifndef LIST_H
#define LIST_H
#include "misc.h"
template <typename T> class CList {
private:
class CNode {
friend CList;
private: T data;
CNode* next;
public: CNode() : next(NULL) {}
~CNode() { delete [] next; }
};
private: int length;
CNode* first;
public:
CList() : length(0), first(NULL) {}
CList(int i_length) : first(NULL) {
int i;
CNode* cur = NULL;
CNode* prev = NULL;
if (i_length < 0) length = 0;
else length = i_length;
for (i=0;i<length;i++) {
// allocate new CNode on heap
cur = new2<CNode>();
// attach preceding CNode pointer
if (prev) prev->next = cur;
else first = cur;
prev = cur;
}
}
~CList() { delete first; }
};
misc.h
#ifndef MISC_H
#define MISC_H
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
inline void terminate( const char* message, int code ) {
printf("nn%snn",message);
system("pause");
exit(code);
};
template <typename T> inline T* new2() {
T* ret = new T;
if (!ret) terminate("Insufficient Memory",-2);
return ret;
}
template <typename T> inline T* new2(int num) {
if (num <= 0) terminate("Invalid Argument",-1);
T* ret = new T[num];
if(!ret) terminate("Insufficient Memory",-2);
return ret;
}
#endif
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include "../Misc/misc.h"
#include "../Misc/list.h"
int main(int argc, char* argv[]) {
//CList<int> m;
CList<int> n(5);
system("pause");
return 0;
}
下面是变量"n"在"return 0;"前的断点处的样子。
http://s20.beta.photobucket.com/user/marshallbs/media/Untitled_zps52497d5d.png.html下面是错误发生的上下文。不幸的是,现在我不能再在观察列表中查看变量"n"了。
_mlock(_HEAP_LOCK); /* block other threads */
__TRY
/* get a pointer to memory block header */
pHead = pHdr(pUserData);
/* verify block type */
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));
当我为列表使用默认构造函数时没有错误。我不明白发生了什么,因为内存释放进程应该在到达第五个CNode对象时停止,该对象有一个空的"下一个"指针。它的行为就好像它试图释放一个无效的非空指针,但我不知道这是怎么发生的
我按原样构建并运行代码(从调试器),没有发现断言失败。实际上,根本没有内存释放,因为CList没有析构函数(您没有发布完整的代码吗?)
一个问题是您使用new
分配next
并使用delete[]
释放它。这是未定义的行为。
cur = new2<CNode>(); // new2 uses `new' and not `new[]'
回收:
~CNode() { delete [] next; }
将后者替换为delete next;
相关文章:
- 如何让LLDB在成功时退出,在失败时等待
- Netbeans 10:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- 链接器命令失败,macOS 上的退出代码为 1(使用 -v 查看调用)
- QDateTime::toString() 在退出处理程序中使用时失败
- CMake 错误:链接器命令失败,退出代码为 1 和 cpp.o 文件
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 如何修复 clang: 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- Xcode:链接器命令失败,退出代码为 1(使用 -v 查看调用)[C++]
- Android NDK.Build命令失败.未定义的引用.clang++:错误:链接器命令失败,退出代码为1
- 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用):在 Macbook 上
- C++ XCODE ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -
- clang:错误:链接器命令失败,C++代码中的退出代码为 1(使用 -v 查看调用)
- Xcode 10.0:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- C++编译错误:ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v
- QT Q_PROPERTY错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- 链接器命令失败,退出代码 1 C++
- 链接器命令失败,退出代码 1 Xcode 9
- ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- Xcode链接器错误:链接器命令失败,退出代码为1(使用-v查看调用)
- WndProc作为类成员,退出失败