Solved

Freeing an oleobject

Posted on 2003-11-18
9
505 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
Comment Utility
var
  objVar: Variant;
...
  objVar := Unassigned;
0
 

Author Comment

by:mhertz
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 2

Expert Comment

by:Robn
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

9 Experts available now in Live!

Get 1:1 Help Now