CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2011
    Posts
    60

    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

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: does memory need to be released for dynamic controls

    Quote Originally Posted by Gregorina View Post
    Do I need to release this memory?
    Yes
    Quote Originally Posted by Gregorina View Post
    Is it released when the changesettings dialog is closed?
    No
    Quote Originally Posted by Gregorina View Post
    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...

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: does memory need to be released for dynamic controls

    Quote Originally Posted by Gregorina View Post
    Do I need to release this memory?
    Yes.
    Quote Originally Posted by Gregorina View Post
    Is it released when the changesettings dialog is closed?
    No.
    Quote Originally Posted by Gregorina View Post
    And if not what is the best way to release it?
    Review your application design.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  4. #4
    Join Date
    Mar 2011
    Posts
    60

    Re: does memory need to be released for dynamic controls

    Quote Originally Posted by VladimirF View Post
    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];
    	}
    }

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    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.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: does memory need to be released for dynamic controls

    Quote Originally Posted by Gregorina View Post
    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?
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: does memory need to be released for dynamic controls

    Quote Originally Posted by Gregorina View Post
    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

  8. #8
    Join Date
    Mar 2011
    Posts
    60

    Re: does memory need to be released for dynamic controls

    Quote Originally Posted by ovidiucucu View Post
    Which is value of MAX_CONTROLS?
    12, 666. 65535, INT_MAX, something else?
    20

  9. #9
    Join Date
    Mar 2011
    Posts
    60

    Re: does memory need to be released for dynamic controls

    Quote Originally Posted by VictorN View Post
    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.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: does memory need to be released for dynamic controls

    Quote Originally Posted by Gregorina View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured