Board index » delphi » When to free an object ???

When to free an object ???


2003-08-19 09:07:57 PM
delphi118
Using D5 Ent. ... I have seen many messages here that indicate that if an
object is instantiated in a action in an ISAPI that I should not free the
object because the webbroker will do it for me when I exit the action and
the webbroker (send the html to the client). How do it know ??? I think I
should do it myself before I exit the procedure. If I do , will that mess
up anything .. Does it even matter ??? If I am going to use an object over
and over, doesn't it make sense to create in in the webmodule "oncreate"
event and then free it in the "destroy" event. Then just use it when
required in various actions ?
 
 

Re:When to free an object ???

Well, I have a TFileStream that is declared in the private var section of
the whole webmodule ...
fs : tFileStream;
Then in a webaction, I instantiate it ....
fs := TfileStream.create('xx.txt', fmShareDenyNone);
If I then try to free fs (fs.free) right before exiting the action, I get an
access violation
If I just leave it be, then it works fine.
This runs contrary to your technique. However, I do what you do with other
objects and it works fine, this seems to be a quirk with tFileStream maybe.
What I am trying to do is to avoid the overhead of creating the tFileStream
every time i need it, just use it but put different content in it from
various files. I dont think its possible because of the way it works. If I
dont destroy it, the webbroker will so it will have to be recreated every
time it is uses. Seems such a waste.
 

Re:When to free an object ???

Del Murray (XXXX@XXXXX.COM) moved some electrons around and created the
following:
Quote
Well, I have a TFileStream that is declared in the private var
section of the whole webmodule ...

get an access violation
If I just leave it be, then it works fine.

This runs contrary to your technique. However, I do what you do with
other objects and it works fine, this seems to be a quirk with
tFileStream maybe.
i never work with global vars. everything i was speaking about was local vars.
so it might be different.
___________________________________________
Robert MacLean
robert at sadev dot co dot za
Web: www.sadev.co.za
 

Re:When to free an object ???

You should not free stream objects that are assigned to
Response.ContentStream
All other objects you create should be treated as "normal"
You should move the declaration of your TFileStream variable from global var
section (I'm not sure what you mean by private var section) to local to the
action(s).
Since the instance will be freed by IIS eventually, you should make sure you
create a new instance each time (as you seem to be doing).
--
Shiv R. Kumar
The Delphi Apostle
www.matlus.com
 

Re:When to free an object ???

Del,
A TMemoryStream has a LoadFromFile method that you can use. However, you
still need to create a new instance for each Request since IIS will free the
memory allocated for your stream.
So other then attempting to free your stream instance, you're doing fine :)
--
Shiv R. Kumar
The Delphi Apostle
www.matlus.com
 

Re:When to free an object ???

One drawback with this is that you now use twice the memory and half of that
"left behind" till another request uses this web module instance.
In the other way, you use a certain amount of memory (depending on file
size) and it is freed as soon as the web server has finished sending back
the response.
--
Shiv R. Kumar
The Delphi Apostle
www.matlus.com
 

Re:When to free an object ???

Why? I use much less space then before and I don't load file again and
again.
I use only one global memory stream which shared between all instances.
I.e. all instances use the same memory stream, but they work with this
memory stream
via proxy stream which only store reference to global memory stream and
position in the stream.
In the end of request only 'proxy' stream is deleted. But global stream
stays alive.
It will be destroyed in the end of program (or if I detect that file has
changed).
Result - less memory space required (because all instances use the same
global memory stream)
and I don't need to load file again and again (because it loaded once)
Regards,
Dmitri Oulitski
 

Re:When to free an object ???

For me, Shivs way is best because I load a different file each time. This is
a PDF creation and Document management subsystem.