我在类A中加入了一个CMutex对象指针成员,m_pMutex 。(具体的类不是A,这里为了叙述方便)在类A的构造函数中创建了一个CMutex对象:
TCHAR sz[]=L"aaa";
m_pMutex = new CMutex(FALSE,sz);在类A的析构函数中删除该CMutex对象:
if (m_pMutex != NULL)
{
delete m_pMutex;
//m_pMutex = NULL;
}程序运行后发现有内存泄漏:
Dumping objects ->
strcore.cpp(118) : {280} normal block at 0x00434C30, 20 bytes long.
Data: < a a > 01 00 00 00 03 00 00 00 03 00 00 00 61 00 61 00
#File Error#(192) : {279} client block at 0x00434C70, subtype 0, 12 bytes long.
a CMutex object at $00434C70, 12 bytes long
strcore.cpp(118) : {212} normal block at 0x00433180, 20 bytes long.
Data: < a a > 01 00 00 00 03 00 00 00 03 00 00 00 61 00 61 00
E:\workhere\LocalPlay\AfterDecodingData.cpp(21) : {211} client block at 0x004331C0, subtype 0, 12 bytes long.
a CMutex object at $004331C0, 12 bytes long
strcore.cpp(118) : {149} normal block at 0x00433790, 20 bytes long.
Data: < b b > 01 00 00 00 03 00 00 00 03 00 00 00 62 00 62 00
E:\workhere\LocalPlay\BeforeDecodingData.cpp(21) : {148} client block at 0x004337D0, subtype 0, 12 bytes long.
a CMutex object at $004337D0, 12 bytes long
Object dump complete.
First-chance exception in LocalPlay.exe (KERNEL32.DLL): 0xC0000005: Access Violation.类A被我跨线程使用,因此其他线程里面创建了同名的CMutex对象。不知道我下面的想法对不对:“同名的CMutex对象实际上是对原来对象的应用,因此,只要删除一次就行了。”实际上,我删除两次,程序无法正常执行。删除一次,执行过程没问题,程序结束时候出现内存泄漏。如何正确删除CMutex对象呢?这里delete为什么不行?
TCHAR sz[]=L"aaa";
m_pMutex = new CMutex(FALSE,sz);在类A的析构函数中删除该CMutex对象:
if (m_pMutex != NULL)
{
delete m_pMutex;
//m_pMutex = NULL;
}程序运行后发现有内存泄漏:
Dumping objects ->
strcore.cpp(118) : {280} normal block at 0x00434C30, 20 bytes long.
Data: < a a > 01 00 00 00 03 00 00 00 03 00 00 00 61 00 61 00
#File Error#(192) : {279} client block at 0x00434C70, subtype 0, 12 bytes long.
a CMutex object at $00434C70, 12 bytes long
strcore.cpp(118) : {212} normal block at 0x00433180, 20 bytes long.
Data: < a a > 01 00 00 00 03 00 00 00 03 00 00 00 61 00 61 00
E:\workhere\LocalPlay\AfterDecodingData.cpp(21) : {211} client block at 0x004331C0, subtype 0, 12 bytes long.
a CMutex object at $004331C0, 12 bytes long
strcore.cpp(118) : {149} normal block at 0x00433790, 20 bytes long.
Data: < b b > 01 00 00 00 03 00 00 00 03 00 00 00 62 00 62 00
E:\workhere\LocalPlay\BeforeDecodingData.cpp(21) : {148} client block at 0x004337D0, subtype 0, 12 bytes long.
a CMutex object at $004337D0, 12 bytes long
Object dump complete.
First-chance exception in LocalPlay.exe (KERNEL32.DLL): 0xC0000005: Access Violation.类A被我跨线程使用,因此其他线程里面创建了同名的CMutex对象。不知道我下面的想法对不对:“同名的CMutex对象实际上是对原来对象的应用,因此,只要删除一次就行了。”实际上,我删除两次,程序无法正常执行。删除一次,执行过程没问题,程序结束时候出现内存泄漏。如何正确删除CMutex对象呢?这里delete为什么不行?
.h文件
#pragma once
#include "afxmt.h"
class a
{private:
CMutex *m_pMutex;
public:
a(void);
~a(void);
};
.cpp文件
#include "StdAfx.h"
#include "a.h"a::a(void)
{
this->m_pMutex=new CMutex(FALSE,"aaa");
}a::~a(void)
{
if (m_pMutex != NULL)
{
delete m_pMutex;
// delete m_pMutex; //我加上这个后,删出了两次并未有内存泄露的提示,只出现:
//test.exe 中的 0x00414bd3 处未处理的异常:0xC0000005: 读取位置 0xfeeefef2 时发生访问冲突 。
//m_pMutex = NULL;
}
}然后在对话框类中建一个按钮。加上#include "a.h"
还有在按钮函数中只添加a m_test;可见内存泄露不只是这一个CMutex对象吧