?
Solved

Freeing an oleobject

Posted on 2003-11-18
9
Medium Priority
?
513 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 

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 1000 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

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!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

800 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