mhertz
asked on
Freeing an oleobject
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
ASKER
Kumao,
Is that all you have to do.
I did objVar := Unassigned; and I don't see the memory being freed.
Is that all you have to do.
I did objVar := Unassigned; and I don't see the memory being freed.
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.App lication') ;
objVar.WorkBooks.Open('C:\ test.xls') ;
objVar.Visible := True;
end;
procedure TForm1.Button2Click(Sender : TObject);
begin
objVar.Quit;
objVar := Unassigned;
end;
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
begin
objVar := CreateOleObject('Excel.App
objVar.WorkBooks.Open('C:\
objVar.Visible := True;
end;
procedure TForm1.Button2Click(Sender
begin
objVar.Quit;
objVar := Unassigned;
end;
ASKER
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.
The ole object that I'm using doesn't have something equivalent to "objVar.Quit;" that is my concern.
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.
The application will compile this way.
ASKER
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.
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.
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
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
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
objVar: Variant;
...
objVar := Unassigned;