Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to free the memory

Status
Not open for further replies.

pierrotsc

Programmer
Nov 25, 2007
358
US
I would like when i close my application to free the memory of all the variables, loaded memorystream, loaded dll. Is there a command that does all that at one time?
Thanks.
PO
 
yes: use CTRL-ALT-DELETE (this is a joke [upsidedown] )

no seriously, there isn't such a 'command'.
the rule is simple: destroy what you created manually.

there are several ways to enlighten this task.

I'll give you one example:

-> TObjectList

only thing you need to do is to create the objectlist and destroy it. every other object you create, just add it to the objectlist. when the objectlist is cleared, all the items it owns will be freed.

Code:
procedure SomeObjectCreatingProcedureWithNoMemLeaks;

var ObjectList : TObjectList;
    StringList : TStringList;
    Index      : Integer;

begin
 ObjectList := TObjectList.Create(True);
 try
  for Index := 1 to 1000 do
  begin
   StringList := TStringList.Create;
   ObjectList.Add(StringList);
  end;
 finally
  FreeAndNil(ObjectList); // this will destroy the objectlist, but also the items it owns
 end;
end;

/Daddy


-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Thanks...It kind of help..I was hoping for a magic command...
Sincerely,
 
Again, doesn't the (*automatic*) Application object do that for you?
 
Again, doesn't the (*automatic*) Application object do that for you?

agreed, but not for all cases.

you can do it for components, but not for the smaller (vcl and others) objects that are not components...



-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
harebrain, forget to add :

small example that isn't covered:

create a Stringlist object

how will TApplication will free that?

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
I tried the application.terminate and that did not work for me..
Thanks guys...
PO
 
whosrdaddy said:
you can do it for components, but not for the smaller (vcl and others) objects that are not components...
It's precisely VCL objects that Application.Terminate (and closing the main form) clean up. As long as a form is the parent, it's cleaned up.

Because the OP asked about an application, that's what I'm limiting my discussion to.

All that being said, it sounds like someone is looking for a license for sloppy programming. (I disapprove.) If you've inherited a mess, well, tough luck.

OK, pierrotsc, what do you mean, "that did not work?" What are you doing?

 
harebrain, I repeat, some smaller vcl objects (like TStringList) don't have a owner. the OP is mentioning a Tmemorystream which falls in the same category as TStringlist (no owner).

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Sorry for the dealy. I am coding a plug-in for photoshop. I bought some codes for activating the software. When I click the button to activate the software, it load a dll. When I do that and I exit photoshop, photoshop will crash. If i do not click on the activate button and the program has or has not been activated previously, it will not crash. It's only when i load the activation window. The plug-in does not crash, only photoshop when you exit.
I have emailed the company that wrote the code but have not heard back.
That's why i wanted a method to free the memory or maybe just unload the dll.
Thanks.
PO
 
whosrdaddy, I know what you're saying, but you're misreading me. I stressed VCL objects in my posts; the TStringList and TMemoryStream objects you continue to cite are NOT VCL objects. I have not claimed what you have inferred from my posts. That's your problem.

pierrotsc, I'm sorry, but your description of events is so convoluted that it is making my head spin.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top