Click here to Skip to main content
15,894,546 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to remove flicker effect? Pin
Rhuros17-May-11 1:02
professionalRhuros17-May-11 1:02 
AnswerRe: How to remove flicker effect? Pin
Hans Dietrich17-May-11 2:15
mentorHans Dietrich17-May-11 2:15 
Questionconvert a CString variable to unsigned long long Pin
VCProgrammer16-May-11 21:31
VCProgrammer16-May-11 21:31 
AnswerRe: convert a CString variable to unsigned long long Pin
Hans Dietrich16-May-11 21:52
mentorHans Dietrich16-May-11 21:52 
GeneralRe: convert a CString variable to unsigned long long Pin
Richard MacCutchan16-May-11 21:55
mveRichard MacCutchan16-May-11 21:55 
AnswerRe: convert a CString variable to unsigned long long Pin
Richard MacCutchan16-May-11 21:54
mveRichard MacCutchan16-May-11 21:54 
AnswerRe: convert a CString variable to unsigned long long Pin
Hans Dietrich16-May-11 22:24
mentorHans Dietrich16-May-11 22:24 
QuestionMemory leak in CListCtrl::InsertColumn() and CListCtrl::DeleteColumn(). Can anybody explain why this is happning? Pin
Abhijit_Satpute16-May-11 18:34
Abhijit_Satpute16-May-11 18:34 
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()
{
   .
   .
   .
   // TODO: Add extra initialization here
   m_Count = 1;
}

void CListControlDlg::OnBnClickedAdd()
{
	// TODO: Add your control notification handler code here
	//Add 1000 colums in List Control
	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()
{
	// TODO: Add your control notification handler code here
	//Removes 1000 colums in List Control
	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.

AnswerRe: Memory leak in CListCtrl::InsertColumn() and CListCtrl::DeleteColumn(). Can anybody explain why this is happning? Pin
Hans Dietrich16-May-11 22:20
mentorHans Dietrich16-May-11 22:20 
GeneralRe: Memory leak in CListCtrl::InsertColumn() and CListCtrl::DeleteColumn(). Can anybody explain why this is happning? Pin
Abhijit_Satpute17-May-11 1:45
Abhijit_Satpute17-May-11 1:45 
AnswerRe: Memory leak in CListCtrl::InsertColumn() and CListCtrl::DeleteColumn(). Can anybody explain why this is happning? Pin
Hans Dietrich17-May-11 2:04
mentorHans Dietrich17-May-11 2:04 
GeneralRe: Memory leak in CListCtrl::InsertColumn() and CListCtrl::DeleteColumn(). Can anybody explain why this is happning? Pin
Abhijit_Satpute17-May-11 5:00
Abhijit_Satpute17-May-11 5:00 
QuestionRe: Memory leak in CListCtrl::InsertColumn() and CListCtrl::DeleteColumn(). Can anybody explain why this is happning? Pin
David Crow17-May-11 3:00
David Crow17-May-11 3:00 
AnswerRe: Memory leak in CListCtrl::InsertColumn() and CListCtrl::DeleteColumn(). Can anybody explain why this is happning? Pin
Abhijit_Satpute17-May-11 4:59
Abhijit_Satpute17-May-11 4:59 
GeneralRe: Memory leak in CListCtrl::InsertColumn() and CListCtrl::DeleteColumn(). Can anybody explain why this is happning? Pin
David Crow17-May-11 5:03
David Crow17-May-11 5:03 
GeneralRe: Memory leak in CListCtrl::InsertColumn() and CListCtrl::DeleteColumn(). Can anybody explain why this is happning? Pin
Abhijit_Satpute17-May-11 5:25
Abhijit_Satpute17-May-11 5:25 
QuestionRe: Memory leak in CListCtrl::InsertColumn() and CListCtrl::DeleteColumn(). Can anybody explain why this is happning? Pin
David Crow17-May-11 5:51
David Crow17-May-11 5:51 
AnswerRe: Memory leak in CListCtrl::InsertColumn() and CListCtrl::DeleteColumn(). Can anybody explain why this is happning? Pin
Abhijit_Satpute17-May-11 19:58
Abhijit_Satpute17-May-11 19:58 
QuestionStart application at specific time Pin
Pranit Kothari15-May-11 22:00
Pranit Kothari15-May-11 22:00 
AnswerRe: Start application at specific time Pin
Richard MacCutchan15-May-11 22:09
mveRichard MacCutchan15-May-11 22:09 
GeneralRe: Start application at specific time Pin
Pranit Kothari16-May-11 0:30
Pranit Kothari16-May-11 0:30 
GeneralRe: Start application at specific time Pin
Richard MacCutchan16-May-11 1:08
mveRichard MacCutchan16-May-11 1:08 
GeneralRe: Start application at specific time Pin
Pranit Kothari16-May-11 1:12
Pranit Kothari16-May-11 1:12 
GeneralRe: Start application at specific time Pin
ShilpiP16-May-11 1:46
ShilpiP16-May-11 1:46 
GeneralRe: Start application at specific time Pin
Pranit Kothari16-May-11 2:02
Pranit Kothari16-May-11 2:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.