内存泄漏问题,高手请进!

捕鲸叉 2001-11-16 07:30:58
我写了个Dialog Based 短程序,当先后打开该程序两个实例时,第一个实例自动终止。使用的是文件映射的实现进程间数据共享的方法。我用定时器进行检测可以达到目的;现在我用工作线程的方法时却遇到内存泄漏的问题,请各位给予指教。主要代码如下:

// XpDlg.h : header file
//
#if !defined(AFX_XPDLG_H__6A34E5E6_B4F0_11D5_839F_00D00914A418__INCLUDED_)
#define AFX_XPDLG_H__6A34E5E6_B4F0_11D5_839F_00D00914A418__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <afxmt.h>

/////////////////////////////////////////////////////////////////////////////
// CXpDlg dialog

class CXpDlg : public CDialog
{
// Construction
public:
CXpDlg(CWnd* pParent = NULL); // standard constructor
BOOL m_bFirst;
HANDLE hx;
// CCriticalSection m_criticalSection;
CMutex m_mutex;
LPVOID lpView;
#ifdef _DEBUG
CMemoryState oldMemState, newMemState, diffMemState;
#endif
// Dialog Data
//{{AFX_DATA(CXpDlg)
enum { IDD = IDD_XP_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CXpDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);// DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
HICON m_hIcon;

// Generated message map functions
//{{AFX_MSG(CXpDlg)
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnDestroy();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_XPDLG_H__6A34E5E6_B4F0_11D5_839F_00D00914A418__INCLUDED_)



// XpDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Xp.h"
#include "XpDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


UINT MyThreadProc( LPVOID pParam )
{

/******** 内存泄漏!!!! *************/


CXpDlg* pDlg=(CXpDlg*)pParam;
CSingleLock sLock(&(pDlg->m_mutex));

while(TRUE)
{
if(pDlg->m_bFirst)//FIRST INSTANCE
{
if((BYTE*)(pDlg->lpView)!=NULL)
{
if(*(BYTE*)(pDlg->lpView)==_T('1'))//FIND A SECOND INSTANCE
{
sLock.Lock();
*(BYTE*)(pDlg->lpView)=_T('0');
sLock.Unlock();
TRACE0("FIND A SECOND INSTANCE, THIS INSTANCE WILL EXIT!\n");
pDlg->SendMessage(WM_CLOSE);
}
}
}
else //SECOND INSTANCE
{
TRACE0("THIS IS THE SECOND INSTANCE!\n");
if((BYTE*)(pDlg->lpView)!=NULL)
{
sLock.Lock();
if(*(BYTE*)(pDlg->lpView)==_T('0'))
pDlg->m_bFirst=TRUE;
//NOW THE INSTANCE BECOME THE FIRST ONE
sLock.Unlock();
TRACE0("NOW WILL BE THE FIRST INSTANCE!\n\n");
}
}
}
return 0;
}

/////////////////////////////////////////////////////////////////////////////
// CXpDlg dialog

CXpDlg::CXpDlg(CWnd* pParent /*=NULL*/)
: CDialog(CXpDlg::IDD, pParent),
m_mutex(FALSE, NULL)
{
//{{AFX_DATA_INIT(CXpDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bFirst=TRUE;
hx=NULL;

}

void CXpDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CXpDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CXpDlg, CDialog)
//{{AFX_MSG_MAP(CXpDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CXpDlg message handlers

BOOL CXpDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

CSingleLock sLock(&m_mutex);

hx=::CreateFileMapping((HANDLE)0xffffffff,NULL,PAGE_READWRITE,0,128,"mymapfile01");
lpView=::MapViewOfFile(hx,FILE_MAP_WRITE|FILE_MAP_READ,0,0,0);
if(!hx)
{
AfxMessageBox("failed to create file map,will exit");
this->SendMessage(WM_CLOSE);
}
else if(hx)
{
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
TRACE0("a previous instance is running\n"); //SECOND INSTANCE
m_bFirst=FALSE;
if((BYTE*)lpView!=NULL)
{
sLock.Lock();
*(BYTE*)lpView=_T('1');
sLock.Unlock();

}
}
else//FIRST INSTANCE
{
if((BYTE*)lpView!=NULL)
{
sLock.Lock();
BYTE* lpBt=(BYTE*)lpView;
*(BYTE*)lpView=_T('0');
sLock.Unlock();
}

}
}

#ifdef _DEBUG
oldMemState.Checkpoint();
#endif

AfxBeginThread(MyThreadProc,this);
// SetTimer(1,100,NULL);

return TRUE; // return TRUE unless you set the focus to a control
}

void CXpDlg::OnTimer(UINT nIDEvent)
{
CSingleLock sLock(&m_mutex);

if(m_bFirst)
{
if((BYTE*)lpView!=NULL)
{
if(*(BYTE*)lpView==_T('1'))//FIND A SECOND INSTANCE
{
sLock.Lock();
*(BYTE*)lpView=_T('0');
sLock.Unlock();

SendMessage(WM_CLOSE);
return;
}
}
}
else //SECOND INSTANCE
{
if((BYTE*)lpView!=NULL)
{
if(*(BYTE*)lpView==_T('0'))
m_bFirst=TRUE; //NOW THE SECOND INSTANCE BECOME THE FIRST ONE
}

}
// CDialog::OnTimer(nIDEvent);
}

void CXpDlg::OnDestroy()
{
CDialog::OnDestroy();

if(hx)
{
::UnmapViewOfFile(lpView);
CloseHandle(hx);
}

this->KillTimer(1);

#ifdef _DEBUG
newMemState.Checkpoint();
if( diffMemState.Difference( oldMemState, newMemState ) )
{
TRACE( "Attention: Memory leaked!\n" );
}
#endif

}

void CXpDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

HCURSOR CXpDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
...全文
82 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
捕鲸叉 2001-11-23
  • 打赏
  • 举报
回复
http://www.pdfimage.com/ 图像处理
http://www.programsalon.com 名、密码等均为yansung
捕鲸叉 2001-11-19
  • 打赏
  • 举报
回复
问题已解决。错误是工作者线程未能正常退出。
捕鲸叉 2001-11-16
  • 打赏
  • 举报
回复
To sans:
我在程序里用了CMemoryState,请看我贴的代码。出现泄漏很可能是工作线程出了问题,但我
不知道该怎么改。谢谢你。
sans 2001-11-16
  • 打赏
  • 举报
回复
使用CMemoryState类替你去发现问题吧。
先在怀疑有内存泄漏的代码的开始部分插入CMemoryState类的Checkpoint();
然后在代码的结束部分插入另外一个Checkpoint();
最后再用Difference()进行查看即可。
CMemoryState类的具体用法可以看msdn。

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧