Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Does Form.Release free memory?


This question is answered. Helpful answers available: 1. Correct answers available: 1.


Permlink Replies: 4 - Last Post: Feb 8, 2017 1:11 PM Last Post By: Andrea Kaiser
Andrea Kaiser

Posts: 63
Registered: 11/17/15
Does Form.Release free memory?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 8, 2017 8:13 AM
In Delphi Berlin 10.1 I have written an application that creates a form dynamically:

Application.CreateForm(FormClass, MyForm);


This form consists of about 200 controls (TEdit, TListbox and so on). These controls are created dynamically, since it depends on certain circumstances, what controls are used. While creating, the owner is indicated.

MyEdit        := TEdit.Create(AOwner);
MyEdit.Parent := AParent;


When I run my application and call this form several times I get an error "out of memory".

So I added the following code before creating the form:

try
  if MyForm <> nil then MyForm.Release;
  Application.ProcessMessages;
  MyForm := nil;
except
end;
Application.CreateForm(FormClass, MyForm);


And yet, the error message "out of memory" shows up. Does Form.Release not free all the memory used by the controls?
Jeff Overcash (...

Posts: 1,529
Registered: 9/23/99
Re: Does Form.Release free memory?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 8, 2017 9:21 AM   in response to: Andrea Kaiser in response to: Andrea Kaiser
Andrea Kaiser wrote:
In Delphi Berlin 10.1 I have written an application that creates a form dynamically:

Application.CreateForm(FormClass, MyForm);


This form consists of about 200 controls (TEdit, TListbox and so on). These controls are created dynamically, since it depends on certain circumstances, what controls are used. While creating, the owner is indicated.

MyEdit        := TEdit.Create(AOwner);
MyEdit.Parent := AParent;


When I run my application and call this form several times I get an error "out of memory".

So I added the following code before creating the form:

try
  if MyForm <> nil then MyForm.Release;
  Application.ProcessMessages;
  MyForm := nil;
except
end;
Application.CreateForm(FormClass, MyForm);


And yet, the error message "out of memory" shows up. Does Form.Release not free all the memory used by the controls?

It should,. but you should be calling Free. Release is there for freeing it
from within an event. Calling Application.ProcessMessages is wrong here. The
whole point of Release is to allow it to exit the event before freeing the
object as Release puts a call into the queue to release it.

--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
Learning is finding out what you already know. Doing is demonstrating that you
know it. Teaching is reminding others that they know it as well as you. We are
all leaners, doers, teachers. (R Bach)
Andrea Kaiser

Posts: 63
Registered: 11/17/15
Re: Does Form.Release free memory?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 8, 2017 10:24 AM   in response to: Jeff Overcash (... in response to: Jeff Overcash (...
Jeff Overcash (TeamB) wrote:
It should,. but you should be calling Free. Release is there for freeing it
from within an event. Calling Application.ProcessMessages is wrong here. The
whole point of Release is to allow it to exit the event before freeing the
object as Release puts a call into the queue to release it.

--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
Learning is finding out what you already know. Doing is demonstrating that you
know it. Teaching is reminding others that they know it as well as you. We are
all leaners, doers, teachers. (R Bach)

Thanks for your reply, Jeff.
I'm calling the Release from outside the Form. I made the Application.ProcessMessages because the help file states, that the Release-Command returns immediately, even if it the release is not finished.

In the meantime I was able to narrow the memory leak down to an ImageList, that is connected to all the TEdit controls. I add bitmap pictures to the ImageList while creating it and free it only with the command:
ImageList.Free


It seems like the memory is not freed by this statement. Do I have to free the bitmaps individually before freeing the whole object ImageList?
Jeff Overcash (...

Posts: 1,529
Registered: 9/23/99
Re: Does Form.Release free memory?
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 8, 2017 11:37 AM   in response to: Andrea Kaiser in response to: Andrea Kaiser
Andrea Kaiser wrote:
Jeff Overcash (TeamB) wrote:
It should,. but you should be calling Free. Release is there for freeing it
from within an event. Calling Application.ProcessMessages is wrong here. The
whole point of Release is to allow it to exit the event before freeing the
object as Release puts a call into the queue to release it.

--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
Learning is finding out what you already know. Doing is demonstrating that you
know it. Teaching is reminding others that they know it as well as you. We are
all leaners, doers, teachers. (R Bach)

Thanks for your reply, Jeff.
I'm calling the Release from outside the Form. I made the Application.ProcessMessages because the help file states, that the Release-Command returns immediately, even if it the release is not finished.

Right the purpose of release is so that you can complete your event before the
form is released. You do not want the event to still be running and the object
get freed in the middle of it. Also note I said event, not just any old code
for the form. So you do not want to pump the message queue when calling release.

In the meantime I was able to narrow the memory leak down to an ImageList, that is connected to all the TEdit controls. I add bitmap pictures to the ImageList while creating it and free it only with the command:
ImageList.Free


It seems like the memory is not freed by this statement. Do I have to free the bitmaps individually before freeing the whole object ImageList?

No, but you can be having a memory fragmentation problem and you can't get a big
enough contiguous memory space to hold the next imagelist. If the images are
not changing, that should be put on a global data module and just share the
images and only have them in memory once.

--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
Learning is finding out what you already know. Doing is demonstrating that you
know it. Teaching is reminding others that they know it as well as you. We are
all leaners, doers, teachers. (R Bach)
Andrea Kaiser

Posts: 63
Registered: 11/17/15
Re: Does Form.Release free memory?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 8, 2017 1:11 PM   in response to: Jeff Overcash (... in response to: Jeff Overcash (...
Jeff Overcash (TeamB) wrote:
No, but you can be having a memory fragmentation problem and you can't get a big
enough contiguous memory space to hold the next imagelist. If the images are
not changing, that should be put on a global data module and just share the
images and only have them in memory once.

Thanks for the info and the suggestion, Jeff.
I'll give the global ImageList a try.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02