Hi friends,
I have written simple program of adding and removing columns from using CListCtrl of MFC.
Behavior of the application:
1. When Add button is clicked, its adds 1000 columns to the List control.
2. When Remove Button is clicked it removes the last added 1000 columns from the List Control.
So, when 1000 columns are added to the List Control using InsertColumn() function of MFC, memory usages increased by certain amount(For e.g Say by 500KB), but when we remove the added 1000 columns from the List Control using DeleteColumn() function, the memory is not freed as it was added during the addition of 1000 columns(Only 300KB is freed, resulting in 200KB of Memory Leak).
Create the sample MFC Application and use the below code.
ListControlDlg.cpp
void CListControlDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST, m_List);
}
BEGIN_MESSAGE_MAP(CListControlDlg, CDialog)
.
.
.
ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedAdd)
ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedRemove)
END_MESSAGE_MAP()
BOOL CListControlDlg::OnInitDialog()
{
.
.
.
m_Count = 1;
}
void CListControlDlg::OnBnClickedAdd()
{
int max_limit = m_Count + 1000;
CString columnstr = "Column";
CString Columnno = "";
for (int i = m_Count; i <= max_limit; i++)
{
Columnno.Format("%d", i);
CString columnstrTemp = columnstr + Columnno;
m_List.InsertColumn(i, columnstrTemp, LVCFMT_LEFT, 80);
}
m_Count = max_limit;
}
void CListControlDlg::OnBnClickedRemove()
{
int max_limit = m_Count - 1000;
CString columnstr = "Column";
CString Columnno = "";
for (int i = m_Count; i >= max_limit - 1; i--)
{
Columnno.Format("%d", i);
CString columnstrTemp = columnstr + Columnno;
m_List.DeleteColumn(i);
}
m_Count = max_limit;
}
CListControlDlg.h
class CListControlDlg : public CDialog
{
.
.
.
.
private:
CListCtrl m_List;
int m_Count;
public:
afx_msg void OnBnClickedAdd();
afx_msg void OnBnClickedRemove();
}
ListControl.rc
IDD_LISTCONTROL_DIALOG DIALOGEX 0, 0, 501, 289
STYLE DS_SETFONT | DS_MODALFRAME | DS_3DLOOK | DS_FIXEDSYS | WS_MAXIMIZEBOX |
WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_APPWINDOW
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
DEFPUSHBUTTON "OK",IDOK,444,7,50,16
PUSHBUTTON "Cancel",IDCANCEL,389,7,50,16
CTEXT "TODO: Place dialog controls here.",IDC_STATIC,7,174,300,
8
CONTROL "",IDC_LIST,"SysListView32",LVS_REPORT | LVS_ALIGNLEFT |
WS_BORDER | WS_TABSTOP,27,30,454,236,WS_EX_CLIENTEDGE |
WS_EX_LEFTSCROLLBAR
PUSHBUTTON "Add",IDC_BUTTON1,247,7,50,14
PUSHBUTTON "Remove",IDC_BUTTON2,311,7,50,14
END
Abhijit Satpute.
|