25 May
Posted by: Christian Wimmer in: Downloads, JEDI Windows Security Code Lib
JWSCL provides access to auto pointers or objects. It means that allocated space or objects are automtically destroyed/freed as soon as the auto object runs out of scope. What is a scope? A scope exists as long as the (I say) parent object exists. Parent objects can be:
What is not a parent object?
The current release only allows to create or wrap objects. It is done by the methods of TJwAutoPointer.
The following sample shows how to avoid to manually call free. It uses the Wrap method of TJwAutoPointer to activate the managed pointer mechanism. There is no method to undo this call.
The advantage of the auto pointer approach is that we do not need to call Free for every possible exit. Even exceptions cannot avoid that the managed object is destroyed.
The next sample demonstrates this fact.
if DoExit then
exit;
if Error then
raise Exception.Create(…);
…
//no need to free ManagedObject
end;
The same way can be used to add the IJwAutoPointer variable into classes and records.
TMyRecord = record
x : IJwAutoPointer;
end;
constructor TMyClass.Create(Name: String);
begin
inherited Create;
Writeln(‘Create :’,Name);
N := Name;
end;
destructor TMyClass.Destroy;
begin
Writeln(‘DesTMyRecordoy: ‘,N);
inherited;
end;
procedure X;
var Y,Z : TMyClass;
begin
Y := TMyClass.Create(’1′);
Z := TMyClass.Create(’2′);
Y.x := TJwAutoPointer.Wrap(Z);
Y.Free;
end;
procedure Y(UseDispose : Boolean);
var
L : TMyRecord;
pL : ^TMyRecord;
Y : TMyClass;
begin
new(pL);
Y := TMyClass.Create(’1′);
PL.x := TJwAutoPointer.Wrap(Y);
writeln;
if UseDispose then
dispose(PL);
end;
A call to X returns:
Create :1
Create :2
Destroy: 1
Destroy: 2
As you can see, the managed object (Name : “2″) is shortly destroyed after the parent object is destroyed. You should know about this fact, because accessing the parent object will fail (if you are luck) in an exception.
A call to Y(false) returns
Create :1
A call to Y(true) returns
Create :1
Destroy: 1
The next release of JWSCL will bring support for pointers created by New and GetMem as well as LocalAlloc. It will also implement thread safe locking mechanisms.
There is no need to save the COM object if you want to keep the object only alive in a single function.
Just wrap it.
You even can wrap it several times. All wrapped up instances will be freed, regardeless of the variable’s name.
Y := TMyClass.Create(’2′);
TJwAutoPointer.Wrap(Y);
end;
Of course you can’t access the first object anymore. But sometimes it comes quite handy.
4 Responses
Zzz
22|Apr|2008 1class function TJwAutoPointer.CreateInstance(
const ClassReference : TComponentClass;
const Owner : TComponent): IJwAutoPointer;
begin
Result := Wrap(classReference.Create(nil));
end;
nil should be owner?
Christian Wimmer
22|Apr|2008 2Yes indeed. Fixed in repository.
Joe White
27|Apr|2008 3“The advantage of the auto pointer approach is that we do not need to call Free for every possible exit. Even exceptions cannot avoid that the managed object is destroyed.”
Of course, try..finally gets you the same thing, even in the case of Exit. But your technique could still come in handy sometimes, as long as you don’t care what order things are freed.
Oliver
28|Apr|2008 4Joe, you’re totally right, but you miss the beauty of the approach of aut-pointers.
They are a long known concept in C++ and there are alternative uses which use the same concept (although hard to map to Delphi). One very visual example is the hourglass cursor if some operations is underway. Now in Delphi you would write something like:
In C++ you could then instantiate a simple wrapper class which takes care of the “reset-operation” in its destructor. So this becomes a one-liner
Same for auto-pointers: they tend to make the code more readable and consequently more maintainable.
// Oliver
Leave a reply
You must be logged in to post a comment.
Search
Paypal donation (EUR)
Download Win 7 Search Provider
Categories
Archives
Tags
Recent Posts
Recent Comments
Blogroll
JEDI Sites
Pages