-
May 22nd, 2015, 12:53 PM
#1
does memory need to be released for dynamic controls
I have an application that opens up another dialog. When that dialog is open, i do this:
Code:
BOOL CChangeSettings::OnInitDialog()
{
...
...
for(unsigned int i=0; i<temp_map.size(); i++)
{
CEdit* new_edit = new CEdit;
CStatic* new_static = new CStatic;
CButton* new_button = new CButton;
}
...
...
}
Do I need to release this memory? Is it released when the changesettings dialog is closed? And if not what is the best way to release it?
Thanks for taking the time to help.
Greg
-
May 22nd, 2015, 01:29 PM
#2
Re: does memory need to be released for dynamic controls
Originally Posted by Gregorina
Do I need to release this memory?
Yes
Originally Posted by Gregorina
Is it released when the changesettings dialog is closed?
No
Originally Posted by Gregorina
And if not what is the best way to release it?
How do you create that dialog? Is it modal or modeless?
You can release all allocated memory in the dialog's destructor, or, if you keep a dialog object around (and still need to release the memory right away) - in a handler for WM_CLOSE.
BTW, your code (might not be the REAL code) assigns newly allocated memory to the local variables, that go out of scope with every loop iteration. Unless you assign those values to some member variables, they are lost creating memory leaks (there is no way to recover it).
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
May 22nd, 2015, 01:30 PM
#3
Re: does memory need to be released for dynamic controls
Originally Posted by Gregorina
Do I need to release this memory?
Yes.
Originally Posted by Gregorina
Is it released when the changesettings dialog is closed?
No.
Originally Posted by Gregorina
And if not what is the best way to release it?
Review your application design.
-
May 22nd, 2015, 01:49 PM
#4
Re: does memory need to be released for dynamic controls
Originally Posted by VladimirF
How do you create that dialog? Is it modal or modeless?
You can release all allocated memory in the dialog's destructor, or, if you keep a dialog object around (and still need to release the memory right away) - in a handler for WM_CLOSE.
BTW, your code (might not be the REAL code) assigns newly allocated memory to the local variables, that go out of scope with every loop iteration. Unless you assign those values to some member variables, they are lost creating memory leaks (there is no way to recover it).
Great suggestion. Thanks. I created new member variables:
Code:
private:
CEdit* new_edit[MAX_CONTROLS];
CStatic* new_static[MAX_CONTROLS];
CButton* new_button[MAX_CONTROLS];
And in the destructor:
Code:
CChangeSettings::~CChangeSettings()
{
for(int i=0; i<temp_map.size(); i++)
{
delete new_edit[i];
delete new_static[i];
delete new_button[i];
}
}
-
May 22nd, 2015, 01:53 PM
#5
Re: does memory need to be released for dynamic controls
C++ is neither Java, nor C#
Once you dynamically allocated stuff like in your example
Code:
BOOL CChangeSettings::OnInitDialog()
{
//...
//...
for(unsigned int i=0; i<temp_map.size(); i++)
{
CEdit* new_edit = new CEdit;
CStatic* new_static = new CStatic;
CButton* new_button = new CButton;
}
//...
}
...there's no chance to release it other place than inside CChangeSettings::OnInitDialog.
-
May 22nd, 2015, 02:19 PM
#6
Re: does memory need to be released for dynamic controls
Originally Posted by Gregorina
Great suggestion. Thanks. I created new member variables:
Code:
private:
CEdit* new_edit[MAX_CONTROLS];
CStatic* new_static[MAX_CONTROLS];
CButton* new_button[MAX_CONTROLS];
Which is value of MAX_CONTROLS?
12, 666. 65535, INT_MAX, something else?
-
May 22nd, 2015, 02:20 PM
#7
Re: does memory need to be released for dynamic controls
Originally Posted by Gregorina
Great suggestion. Thanks. I created new member variables:
Code:
private:
CEdit* new_edit[MAX_CONTROLS];
CStatic* new_static[MAX_CONTROLS];
CButton* new_button[MAX_CONTROLS];
And in the destructor:
Code:
CChangeSettings::~CChangeSettings()
{
for(int i=0; i<temp_map.size(); i++)
{
delete new_edit[i];
delete new_static[i];
delete new_button[i];
}
}
Well, according to the code in your ctor the code in the dtor should look like
Code:
delete[] new_edit;
delete[] new_static;
delete[] new_button;
May I ask why you are going to create these child controls in the heap?
Why not on the stack? Why not in the resource editor?
Victor Nijegorodov
-
May 22nd, 2015, 03:32 PM
#8
Re: does memory need to be released for dynamic controls
Originally Posted by ovidiucucu
Which is value of MAX_CONTROLS?
12, 666. 65535, INT_MAX, something else?
20
-
May 22nd, 2015, 03:35 PM
#9
Re: does memory need to be released for dynamic controls
Originally Posted by VictorN
Well, according to the code in your ctor the code in the dtor should look like
Code:
delete[] new_edit;
delete[] new_static;
delete[] new_button;
May I ask why you are going to create these child controls in the heap?
Why not on the stack? Why not in the resource editor?
I don't know how many edit boxes/static texts/buttons i'll need until startup. At startup the application reads some values from an xml file and that tells me how many controls I need.
-
May 23rd, 2015, 02:43 AM
#10
Re: does memory need to be released for dynamic controls
Originally Posted by Gregorina
I don't know how many edit boxes/static texts/buttons i'll need until startup. At startup the application reads some values from an xml file and that tells me how many controls I need.
Maybe you should redesign to use some other controls? Like list control for example?
What would happen if this "some values from an xml file" were 30 or 50 or even more?
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|