内存泄漏问题,高手请进!
捕鲸叉 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;
}