Board index » delphi » Threads: Access violation on COM-Server destroing

Threads: Access violation on COM-Server destroing

Hello all,

I'll try to explain, what I'm trying to do:

My program uses COM-Server - say Foo1
From the thread I'm creating (this is only one thread I'm creating) I want
to initialize another Com-Server Foo2 using Foo1.Connect function.
The idea is that the Foo2 Server will be inialized only once because of
performance reasons. (...and it has to be initialised in this thread).

procedure TThread.Execute;
begin
...
  Foo1.Connect;
...
end;

function Foo1.Connect: boolean;
begin
  if Connected then Exit;

  try
    ComInit := Succeeded(CoInitializeEx (nil, COINIT_APARTMENTTHREADED));

    FFoo2 := CoFoo2.Create;
    FFoo2.Connect;
    Connected := True;

  except
    on E: Exception do
    begin
      .....
    end;
  end;
end;

That works so far. After that the thread goes terminated.
On Exit I want to destroy Foo2 in that way:

destructor Main.Destroy;
begin
  Foo1 := nil;
  inherited;
end;

destrucor Foo1.Destroy;
begin
  Foo1.Disconnect;
  inherited;
end;

function Foo1.Disconnect;
begin
  if Connected then
  begin

    if FFoo2.IsConnected then
      FFoo2.Disconnect;

    try
      FFoo2 := nil;
      // I get access violation here
      // I even cannot catch it with this try...except block.

    except
      on E: Exception do
        ShowMessage(E.Message);
    end;

    if ComInit then CoUninitialize;
  end;

  inherited;
end;

Any Ideas?
Alex

 

Re:Threads: Access violation on COM-Server destroing


Can you show your actual code that compiles? This code looks like its
inconsitent and its hard to determine which is which.

--
have fun
Binh Ly
www.techvanguards.com

Quote
"Alex Penner" <a...@c-studios.de> wrote in message

news:3b0b7bd6$1_1@dnews...
Quote
> procedure TThread.Execute;
> begin
> ...
>   Foo1.Connect;
> ...
> end;

> function Foo1.Connect: boolean;
> begin
>   if Connected then Exit;

>   try
>     ComInit := Succeeded(CoInitializeEx (nil, COINIT_APARTMENTTHREADED));

>     FFoo2 := CoFoo2.Create;
>     FFoo2.Connect;
>     Connected := True;

>   except
>     on E: Exception do
>     begin
>       .....
>     end;
>   end;
> end;

> That works so far. After that the thread goes terminated.
> On Exit I want to destroy Foo2 in that way:

> destructor Main.Destroy;
> begin
>   Foo1 := nil;
>   inherited;
> end;

> destrucor Foo1.Destroy;
> begin
>   Foo1.Disconnect;
>   inherited;
> end;

> function Foo1.Disconnect;
> begin
>   if Connected then
>   begin

>     if FFoo2.IsConnected then
>       FFoo2.Disconnect;

>     try
>       FFoo2 := nil;
>       // I get access violation here
>       // I even cannot catch it with this try...except block.

>     except
>       on E: Exception do
>         ShowMessage(E.Message);
>     end;

>     if ComInit then CoUninitialize;
>   end;

>   inherited;
> end;

Re:Threads: Access violation on COM-Server destroing


Hello Binh,

The actual code is pretty large, I tried to extract the things where error
happens.

However I found a dirty workaround. If I calls Foo1.Connect with Synchronize
the release of Foo2 works very well. Have you an idea how can I get working
code without Synchronize-Function?

Alex

Quote
> Can you show your actual code that compiles? This code looks like its
> inconsitent and its hard to determine which is which.

> --
> have fun
> Binh Ly
> www.techvanguards.com

> "Alex Penner" <a...@c-studios.de> wrote in message
> news:3b0b7bd6$1_1@dnews...
> > procedure TThread.Execute;
> > begin
> > ...
> >   Foo1.Connect;
> > ...
> > end;

> > function Foo1.Connect: boolean;
> > begin
> >   if Connected then Exit;

> >   try
> >     ComInit := Succeeded(CoInitializeEx (nil,

COINIT_APARTMENTTHREADED));

- Show quoted text -

Quote

> >     FFoo2 := CoFoo2.Create;
> >     FFoo2.Connect;
> >     Connected := True;

> >   except
> >     on E: Exception do
> >     begin
> >       .....
> >     end;
> >   end;
> > end;

> > That works so far. After that the thread goes terminated.
> > On Exit I want to destroy Foo2 in that way:

> > destructor Main.Destroy;
> > begin
> >   Foo1 := nil;
> >   inherited;
> > end;

> > destrucor Foo1.Destroy;
> > begin
> >   Foo1.Disconnect;
> >   inherited;
> > end;

> > function Foo1.Disconnect;
> > begin
> >   if Connected then
> >   begin

> >     if FFoo2.IsConnected then
> >       FFoo2.Disconnect;

> >     try
> >       FFoo2 := nil;
> >       // I get access violation here
> >       // I even cannot catch it with this try...except block.

> >     except
> >       on E: Exception do
> >         ShowMessage(E.Message);
> >     end;

> >     if ComInit then CoUninitialize;
> >   end;

> >   inherited;
> > end;

Other Threads