Board index » delphi » COM object life span

COM object life span

Hi all,

I am a little confused about how COM releases an interface. Below are 3
snippets of code from a client that calls a COM server. In sample 1 and 3
the server is released (i.e. it shuts down) at the end of the procedure. In
sample 2 however it shuts down on the "obj := Nil" line.
Why the different behaviour? I would expect them all to behave like sample 2
and release the object when the variable is reassigned.

(Note. The MessageBeep is just to let me trace where the server is released)

PS. If anyone has some examples of how to build a reasonable object model it
woulld be greatly appreciated. By reasonable I mean multiple levels of
objects and collections.

procedure TForm1.Button1Click(Sender: TObject);
var
    obj : OleVariant;
begin
    obj := CreateOleObject('Proj.Thingies');
    if VarisEmpty(obj) then
        exit;
    obj := Unassigned;
    messagebeep(-1);
end;                                                { Com server shuts down
here.}

procedure TForm1.Button2Click(Sender: TObject);
var
    obj : IThingies;
begin
    obj := CoThingies.Create;
    if obj = Nil then
        exit;
    obj := Nil;                                    { Com server shuts down
here.}
    messagebeep(-1);
end;

procedure TForm1.Button3Click(Sender: TObject);
var
    obj : IThingies;
begin
    obj := CreateComObject(Class_Thingies) as IThingies;
    if obj = Nil then
        exit;
    obj := Nil;
    Messagebeep(-1);
end;                                                { Com server shuts down
here.}

 

Re:COM object life span


I would have thought that all the samples should behave the same, like
sample 2. Is this code that you show here the actual code you tested
with? How do you know that the server is unloaded before or after the
beep? If the server is in it's own process space it could be releasing
itself simultaneously to the beep? I take it that your server is a
separate exe? Would it be better to put a message box in to pause the
client instead of the beep so that you can wait to make sure that the
server wasn't actually released right after the assignment? I might
try you sample code later..

Quote

>PS. If anyone has some examples of how to build a reasonable object
model it
>woulld be greatly appreciated. By reasonable I mean multiple levels
of
>objects and collections.

What sorts of examples were you after? Just code or the whole object
model thing with diagrams and code as well? I know I had trouble
finding examples especially of objects supporting interfaces..
hopefully one day soon I'll be able to get together a lot of the
things I have done and put them on line..

David Shaw

Re:COM object life span


Delphi helps you with releasing interface pointers. You have
defined your interface pointer within a routine. When you don't call
obj = nil, Delphi will do this automatically when your finished with
the routine, i.e. when the variable is out of scope.

If obj was a property of your class then it won't be set to nil.

On Wed, 18 Mar 1998 09:57:28 +1100, "Phil Haselden" <p...@bigpond.com>
wrote:

Quote
>Hi all,

>I am a little confused about how COM releases an interface. Below are 3
>snippets of code from a client that calls a COM server. In sample 1 and 3
>the server is released (i.e. it shuts down) at the end of the procedure. In
>sample 2 however it shuts down on the "obj := Nil" line.
>Why the different behaviour? I would expect them all to behave like sample 2
>and release the object when the variable is reassigned.

>(Note. The MessageBeep is just to let me trace where the server is released)

>PS. If anyone has some examples of how to build a reasonable object model it
>woulld be greatly appreciated. By reasonable I mean multiple levels of
>objects and collections.

>procedure TForm1.Button1Click(Sender: TObject);
>var
>    obj : OleVariant;
>begin
>    obj := CreateOleObject('Proj.Thingies');
>    if VarisEmpty(obj) then
>        exit;
>    obj := Unassigned;
>    messagebeep(-1);
>end;                                                { Com server shuts down
>here.}

>procedure TForm1.Button2Click(Sender: TObject);
>var
>    obj : IThingies;
>begin
>    obj := CoThingies.Create;
>    if obj = Nil then
>        exit;
>    obj := Nil;                                    { Com server shuts down
>here.}
>    messagebeep(-1);
>end;

>procedure TForm1.Button3Click(Sender: TObject);
>var
>    obj : IThingies;
>begin
>    obj := CreateComObject(Class_Thingies) as IThingies;
>    if obj = Nil then
>        exit;
>    obj := Nil;
>    Messagebeep(-1);
>end;                                                { Com server shuts down
>here.}

Re:COM object life span


Hi,

Yeah.. that is what I thought Phil's question was about to start with
as well. I think he understands that, it's just in one case he says
that the server gets released immediately when the reference is lost
(i.e. := nil) and in the other two cases it doesn't seem as though the
server is realeased immediately when assigned nil but after the
variable obj goes out of scope even though it has been assigned nil..?
Is this what your question was Phil?

- David Shaw

Quote
Paul Chung wrote in message <3510eeec.2501...@forums.borland.com>...
>Delphi helps you with releasing interface pointers. You have
>defined your interface pointer within a routine. When you don't call
>obj = nil, Delphi will do this automatically when your finished with
>the routine, i.e. when the variable is out of scope.

>If obj was a property of your class then it won't be set to nil.

Re:COM object life span


Yes exactly. Sample 2 behaves as expected, samples 1 and 3 do not.

(BTW obj is snot a property of the class, it is just a local variable in all
cases)

Quote
David Shaw wrote in message <6es82u$3a...@forums.borland.com>...
>Hi,

>Yeah.. that is what I thought Phil's question was about to start with
>as well. I think he understands that, it's just in one case he says
>that the server gets released immediately when the reference is lost
>(i.e. := nil) and in the other two cases it doesn't seem as though the
>server is realeased immediately when assigned nil but after the
>variable obj goes out of scope even though it has been assigned nil..?
>Is this what your question was Phil?

>- David Shaw

>Paul Chung wrote in message <3510eeec.2501...@forums.borland.com>...
>>Delphi helps you with releasing interface pointers. You have
>>defined your interface pointer within a routine. When you don't call
>>obj = nil, Delphi will do this automatically when your finished with
>>the routine, i.e. when the variable is out of scope.

>>If obj was a property of your class then it won't be set to nil.

Re:COM object life span


I know the server is unloaded because its window goes away.

Using a messagebox is a good idea, but since I am stepping through the code,
not running it, the beep works just as well.

Phil

Quote
David Shaw wrote in message <6eq956$18...@forums.borland.com>...
>I would have thought that all the samples should behave the same, like
>sample 2. Is this code that you show here the actual code you tested
>with? How do you know that the server is unloaded before or after the
>beep? If the server is in it's own process space it could be releasing
>itself simultaneously to the beep? I take it that your server is a
>separate exe? Would it be better to put a message box in to pause the
>client instead of the beep so that you can wait to make sure that the
>server wasn't actually released right after the assignment? I might
>try you sample code later..

>>PS. If anyone has some examples of how to build a reasonable object
>model it
>>woulld be greatly appreciated. By reasonable I mean multiple levels
>of
>>objects and collections.

>What sorts of examples were you after? Just code or the whole object
>model thing with diagrams and code as well? I know I had trouble
>finding examples especially of objects supporting interfaces..
>hopefully one day soon I'll be able to get together a lot of the
>things I have done and put them on line..

>David Shaw

Other Threads