Results 1 to 6 of 6

Thread: Questions about releasing your DirectX interfaces

  1. #1

    Questions about releasing your DirectX interfaces

    Hi everyone.

    I was wondering...

    Is there a dirrerence between doing this:

    [pascal]
    MyDirect3DInterface := nil;

    //OR

    MyDirect3dInterface.Release;
    [/pascal]

    and this:

    [pascal]
    if MyDirect3dInterface <> nil then MyDirect3dInterface.Release;

    //OR

    if MyDirect3dInterface <> nil then MyDirect3dInterface := nil;
    [/pascal]

    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:

    [pascal]
    MyDirect3dInterface.Release;
    MyDirect3dInterface.Release;
    [/pascal]

    Is this also true when i do this??

    [pascal]
    MyDirect3dInterface := nil;
    MyDirect3dInterface := nil;
    [/pascal]

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

    Thanx alot
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  2. #2

    Questions about releasing your DirectX interfaces

    Yes, there is a difference!!

    If you do this:
    [pascal]MyInterface.Release;
    assert(MyInterface = nil);[/pascal]
    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:[pascal]if MyInterface <> nil then begin
    MyInterface.Release;
    MyInterface := nil;
    end;[/pascal]If you intend to check something based on the nil status.

  3. #3

    Questions about releasing your DirectX interfaces

    If I remember my COM correctly

    [pascal]
    MyDirect3DInterface := nil;
    [/pascal]

    calls .Release in the background providing the interface supports IInterface (which all DirectX stuff does). I have never called .Release manually.
    <A HREF="http://www.myhpf.co.uk/banner.asp?friend=139328">
    <br /><IMG SRC="http://www.myhpf.co.uk/banners/60x468.gif" BORDER="0">
    <br /></A>

  4. #4

    Questions about releasing your DirectX interfaces

    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.

  5. #5

    Questions about releasing your DirectX interfaces

    So you mean calling "MyDirect3dInterface := nil" is enough?? :razz:
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  6. #6

    Questions about releasing your DirectX interfaces

    Quote Originally Posted by chronozphere
    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."
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •