PDA

View Full Version : Questions about releasing your DirectX interfaces



chronozphere
06-08-2007, 08:36 PM
Hi everyone. :)

I was wondering...

Is there a dirrerence between doing this:


MyDirect3DInterface := nil;

//OR

MyDirect3dInterface.Release;


and this:


if MyDirect3dInterface <> nil then MyDirect3dInterface.Release;

//OR

if MyDirect3dInterface <> nil then MyDirect3dInterface := nil;


Until now, i usually set my interfaces to NIL to free them without the IF THEN check. However i often see this in other D3D sources (both pascal and C++).
So, Is there any difference between these methods.

I recall that i read somewere that releasing a method twice is bad:


MyDirect3dInterface.Release;
MyDirect3dInterface.Release;


Is this also true when i do this??


MyDirect3dInterface := nil;
MyDirect3dInterface := nil;


Can someone tell me what the right way is, because it's confusing me a bit.

Thanx alot ;)

Robert Kosek
06-08-2007, 08:40 PM
Yes, there is a difference!!

If you do this:
MyInterface.Release;
assert(MyInterface = nil);
The assert will always be called. The same goes for Free. This doesn't mean you shouldn't destroy the object, but just that the variable remains active though the item is gone.

You should always call:if MyInterface <> nil then begin
MyInterface.Release;
MyInterface := nil;
end;If you intend to check something based on the nil status.

technomage
06-08-2007, 11:00 PM
If I remember my COM correctly


MyDirect3DInterface := nil;


calls .Release in the background providing the interface supports IInterface (which all DirectX stuff does). I have never called .Release manually.

Robert Kosek
06-08-2007, 11:32 PM
Ah; I have never worked with Interfaces directly so I wouldn't know. I just know I hit the paradigm with a component I'm working on, that freed things aren't nil. Most things in pascal are cross-applicable but I guess this isn't one.

chronozphere
07-08-2007, 09:42 AM
So you mean calling "MyDirect3dInterface := nil" is enough?? :razz:

savage
07-08-2007, 02:12 PM
So you mean calling "MyDirect3dInterface := nil" is enough?? :razz:

Because COM is reference counted, calling := nil should be enough.
There are instances where you may need to call _release and other stuff, but that would tend to indicate that you are doing more advanced stuff. If you are just doing normal vanilla DirectX stuff, setting the interface to nil should be fine. Otherwise do a search for "Delphi COM reference counting."