We help IT Professionals succeed at work.

How to Release IStream?

zxw
zxw asked
on
Hi,

Why does not IStream have Release function? I wrote the follwoing codes:

var DataStream : IStream;

     CreateStreamOnHGlobal(0, True, DataStream);  
     with DataM.Table1 do
     begin
         Open;
         (FieldByName('Content') as TBlobField).SaveToStream(TOleStream.Create(DataStream));
         
         DataStream.Seek(0, STREAM_SEEK_SET, Dummy);
         Close;
     end;

     {......}

but how to release DataStream?    
Comment
Watch Question

Russell LibbySoftware Engineer, Advisory
CERTIFIED EXPERT
Top Expert 2005

Commented:

IStream inherits from IUnknown, but the way Delphi wraps the COM interfaces, it is exposed as "._Release". It is worth mentioning that if defined as a var in a procedure or function, the istream will be automatically released by Delphi when the function/procedure is finished.

Russell

Commented:
Hi,

As far as I know, your IStream is an interface.  So normally assigning nil to the variable that holds the interface will also free it.

I suggest you try it out with something like

DataStream := Nil;

But try it out first ;-)

Best regards,



Stefaan

Commented:
It all depends on the Delphi version you're using. If you're still using D2, IStream should have a Release method, in that case you HAVE to call it.
If you're using D3-D6, you can forget about Release, Delphi does that for you in the propriate location. Just trust Delphi, everything gets freed up automatically. (BTW, Russell, it doesn't depend on whether you declared a variable or not, Delphi frees it in any case correctly).

Regards, Madshi.

Commented:
P.S: Never call ._Release, this will only lead to wild exceptions, because Delphi already calls it for you. If you call it manually, it is called too often, thus the exceptions...
Russell LibbySoftware Engineer, Advisory
CERTIFIED EXPERT
Top Expert 2005

Commented:

Madshi,

Your correct, delphi DOES do all the cleanup, but at different times depending on scope. (If the interface is declared as a global variable, the interface is not released until program finalization.)

Russell
Russell LibbySoftware Engineer, Advisory
CERTIFIED EXPERT
Top Expert 2005

Commented:

Madshi,

Your correct, delphi DOES do all the cleanup, but at different times depending on scope. (If the interface is declared as a global variable, the interface is not released until program finalization.)

Russell

Commented:
Russel,

Have you tried to assign nil to the interface variable ?

Best regards,


Stefaan
Russell LibbySoftware Engineer, Advisory
CERTIFIED EXPERT
Top Expert 2005

Commented:
Stefaan,

Yes. That causes Delphi to immediately release the interface, behind the scenes the internal IUnknown.Release is called.

D3-D6:
If an interface is created it will be available as long as the interface variable remains in scope. Any ":=nil" assignment will cause the interface to be released correctly. Calling _Release without a matching call to _AddRef results in an a crash as soon as the interface variable goes out of scope.  

Does that pretty much sum it up?...

Regards,
Russell

Commented:
Russel,

Well you almost got it.

Any ":=nil" assignment will cause the interface to be released correctly.

In fact, assigning nil or any new instance of the same interface will cause the previous one to be freed.

Best regards,


Stefaan

Commented:
Russel,

Let me clear up some thing first.  Assigning nil to a interface variable, letting it go out of scope, or assigning anything to the variable does not necessarely mean that the interfaced object will be destroyed.

What it actually does is decrease its reference count.  And once the reference count is 0 it will be released thus freeing the object.

Best regards,



Stefaan