Solved

Freeing an oleobject

Posted on 2003-11-18
9
508 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

803 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