在我的应用我创建一个对象非常类似这样的:与对象处理损坏堆
connect() {
mVHTGlove = new vhtGlove(params);
}
,一旦我即将关闭应用程序,我称它是:
disconnect() {
if (mVHTGlove)
delete mVHTGlove;
}
此调用总是触发一个带有以下消息的断点:
Windows触发了一个断点 Des ignerDynD.exe。
这可能是由于 堆,这表明在 DesignerDynD.exe或任何它 已加载的DLL的一个错误的损坏。
这也可能是由于用户 按F12而DesignerDynD.exe 有焦点。
输出窗口可能有更多 诊断信息。
我无法修改vhtGlove类来修复堆栈损坏,因为它是仅以头文件,lib文件和dll的形式提供的外部库。
有什么方法可以用干净的方式使用这个类吗?
****编辑:::我试图剥离下来到最低限度,但我得到相同的结果......在这里你有整个代码。
#include "vhandtk/vhtCyberGlove.h"
#include "vhandtk/vhtIOConn.h"
#include "vhandtk/vhtBaseException.h"
using namespace std;
int main(int argc, char* argv[])
{
vhtCyberGlove* testGlove = NULL;
vhtIOConn gloveAddress("cyberglove", "localhost", "12345", "com1", "115200");
try
{
testGlove = new vhtCyberGlove(&gloveAddress,false);
if (testGlove->connect())
cout << "Glove connected successfully" << endl;
else
{
throw vhtBaseException("testGlove()->connect() returned false.");
}
if (testGlove->disconnect())
{
cout << "Glove disconnected successfully" << endl;
}
else
{
throw vhtBaseException("testGlove()->disconnect() returned false.");
}
}
catch (vhtBaseException *e)
{
cout << "Error with gloves: " << e << endl;
system("pause");
exit(0);
}
delete testGlove;
return 0;
}
删除手套仍然崩溃。
编辑#2 ::如果我只是分配和删除vhtCyberGlove的一个实例,它也崩溃。
int main(int argc, char* argv[])
{
vhtCyberGlove* testGlove = NULL;
vhtIOConn gloveAddress("cyberglove", "localhost", "12345", "com1", "115200");
testGlove = new vhtCyberGlove(&gloveAddress,false);
delete testGlove; //<<crash!
return 0;
}
任何想法?
谢谢!
JC
请注意,您不需要检查_mVHTGlove_不是_NULL_。它很好地定义了将_NULL_指针传递给_delete_。 – sbi 2009-07-22 16:15:14
disconnect()调用了多少次? – MSN 2009-07-22 16:44:53
只是一次。 。 – levesque 2009-07-22 16:56:45