Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CoUninitialize causes access violation

258 views
Skip to first unread message

Christian Holzner

unread,
Sep 11, 2002, 1:00:36 PM9/11/02
to
Every time I issue a 'CoUninitialize' I get an access violation. Even if
there is only a single call to e.g. 'CreateOleObject'. Does that mean, that
the OLE-Object (a third party) is buggy. Sorry I am a total OLE beginner.

Christian


Sample code:

procedure TForm1.BtnClick(Sender: TObject);
var
UA: Variant;
begin
CoInitialize(nil);
UA := CreateOleObject('UA.API2');
// UA := Unassigned;
CoUninitialize; <-- AV here (also with above line uncommented
end;


Dmitry Streblechenko

unread,
Sep 11, 2002, 1:41:33 PM9/11/02
to
Do not place CoInitialize()/ CoUninitialize() calls next to each other - for
out-of-proc servers, message pump has to run to make COM work properly.
OnFormCreate/OnFormDestroy are much better places. BTW, you do not have to call
these functions, Delphi does that for you. The only time you need to call them
if you are running in a thread other than your app's main thread.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool

"Christian Holzner" <noner@none> wrote in message
news:3d7f...@newsgroups.borland.com...

Christian Holzner

unread,
Sep 11, 2002, 4:11:56 PM9/11/02
to
It seems to me that I HAVE to use at least CoInitialize because if I don't
do it I get an EOLESysError with the message 'CoInitialize has not been
called'.

But your hint with placing them into OnFormCreate/OnFormDestroy worked - no
AV any more. Thanks!

Christian


"Dmitry Streblechenko" <dmi...@dimastr.com> wrote in message
news:3d7f7f79$2...@newsgroups.borland.com...

Rob Matthews

unread,
Sep 24, 2002, 1:45:50 PM9/24/02
to
I have a similar problem to Christian Holzer - I am using a wrapper
class for Excel (using early binding) with the following constructor and
destructor (i.e. in separate procedures as Dmitry Streblechenko suggested).

constructor TExcelR.Create(AOwner:TComponent);
(********************************************************************)
(* Constructor for the TExcelR object specifying an owner component.
(********************************************************************)
begin
CoInitialize(AOwner);
ExcelApplication1:=TExcelApplication.Create(AOwner);
WorkBook:=TExcelWorkBook.Create(AOwner);
WorkSheet:=TExcelWorksheet.Create(AOwner);
lcid:=GetUserDefaultLCID;
ExcelApplication1.AutoConnect:=true;
ExcelApplication1.Visible[lcid]:=true;
ExcelApplication1.DisplayAlerts[lcid]:=false;
end;

destructor TExcelR.Destroy;
(*********************************************************************)
(* Destructor for the TExcelR object.

(*********************************************************************)
begin
WorkSheet.Free; WorkBook.Free; ExcelApplication1.Free;
CoUninitialize;
end;

This works fine in programs that have forms (i.e. with a call in the DPR
file to Application.Initialize). However, I want to use the wrapper in a
Delphi program that doesn't have forms, and hence doesn't have the call
to Application.Initialize. Typical code in the DPR file is:

var ExcelFile:TExcelR;
begin
ExcelFile:=TExcelR.Create(nil);
<code in here to read/write to the Excel file>
ExcelFile.Destroy;
end;

It worked fine doing this on a machine running Windows ME, but I
upgraded recently to XP, and the problem has appeared. Incidently, it
also gave the same problem on a machine running NT4.

I need to have the CoInitialize in the constructor or else the same
error as Christian mentioned occurs, and if I comment out the
CoUnInitialize in the destructor, the error doesn't occur, but maybe I
am not freeing up memory or something if I do this?

Any help would be gratefully received.

Robin Matthews.

+++++

0 new messages