Solved

Freeing an oleobject

Posted on 2003-11-18
9
506 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using FastMM4... should I enable MMX option? 9 76
Virtualstringtree carsh after invalidate for while 5 86
delphi prevent click fast 2 191
delphi exception 7 63
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now