Solved

Freeing an oleobject

Posted on 2003-11-18
9
510 Views
Last Modified: 2010-04-05

How do you  free up an oleobject at the end of a program. This object was created with createoleobject and it is using a varaiant.


I
0
Comment
Question by:mhertz
  • 4
  • 3
  • 2
9 Comments
 

Expert Comment

by:Kumao
ID: 9776238
var
  objVar: Variant;
...
  objVar := Unassigned;
0
 

Author Comment

by:mhertz
ID: 9776424
Kumao,

Is that all you have to do.

I did objVar := Unassigned; and I don't see the memory being freed.
0
 

Expert Comment

by:Kumao
ID: 9776472
Here is may test code for Excel Application.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private 宣言 }
    objVar: Variant; //
  public
    { Public 宣言 }
  end;

var
  Form1: TForm1;

implementation

uses ComObj;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  objVar := CreateOleObject('Excel.Application');
  objVar.WorkBooks.Open('C:\test.xls');
  objVar.Visible := True;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  objVar.Quit;
  objVar := Unassigned;
end;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mhertz
ID: 9780096
Thanks for your help so far.

The ole object that I'm using doesn't have something equivalent to "objVar.Quit;"   that is my concern.

0
 
LVL 2

Expert Comment

by:Robn
ID: 9780583
Because you're using late binding here, you won't have any interface to interact with. A variant does not have the .Quit method on it. What ends up happening is the program attempts to find a method called 'Quit' in the created interface. Once found, it is called. You will need to know exactly how to interact with this object because you won't find the error until you run the application.
The application will compile this way.
0
 

Author Comment

by:mhertz
ID: 9784138
I guess that I'm back to my previous question.

Is objVar := Unassigned; all you have to do to free up memory etc..

I did objVar := Unassigned; and I don't see the memory being freed.
 
 
0
 
LVL 2

Expert Comment

by:Robn
ID: 9787130
objVar := Unassigned will free the memory.
It may not be immediate though. It depends on when the COM memory manager gets around to cleaning up the memory. There is an API you can call to tell the COM subsystem to clean up any libraries without reference called CoFreeUnusedLibraries defined in the ActiveX unit. Another reason the memory wouldn't be freed is if the COM object you have has circular referenced interfaces. This would stop the object from being destroyed even after it goes out of scope.

Hope this helps,
Rob
0
 

Author Comment

by:mhertz
ID: 9788255
Just one last thing so I can understand this.

I understand objVar := Unassigned.

In my app Iave a need to access a com object multiple times, about 10 times an hour, while the app is running. The app can possibly be running for 12 to 24 hours at a time.

I would like to do a createoleobject once at the begining and leave it for the lenght of the application. At the close of the app I would do I would like to do objVar := Unassigned.

Will this wok? Can I leave a com object active for a long time period. Would it cause memory leaks.

And just one more question. Can I use a check on objVar := Unassigned to tell me if the createoleobject has already taken place?

Thanks for your help.
0
 
LVL 2

Accepted Solution

by:
Robn earned 250 total points
ID: 9788378
Provided your COM object is written property, there is no problem leaving it open for any length of time.
Creating the COM object when the application starts is completely acceptable.
You can check to see if createoleobject has already taken place by doing this check. If objVar is global and not part of a class, you will need to initialize it to Unassigned like this.
var
  objVar: Variant = Unassigned;

What I normally do in code like this is write a wrapper function like this.

implementation

var
  objVar: Variant = Unassigned;

function MyObj: Variant;
begin
  if objVar = Unassigned then begin
    objVar := createoleobject(...);
  end;
  result := objVar;
end;

then when you want to access the COM object, you would use MyObj as a variable instead of referencing objVar directly.

procedure DoWork;
begin
  MyObj.Work;
  .....
  .....
end;

This way, the COM object is only created when the application needs it for the first time.
You don't need to write finalization code to set your variable to Unassigned, the OS will manage this for you. Some people like to do it anyway and that is completely fine also.

Hope this helps,
Rob
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question