Skip to main content
GameDev.net gamedev.net
🔒 Locked

Reference Counting

Started by Nacho Jul 31, 2002 at 12:56 PM 5 replies 350+ views
Original Post
Nacho
Nacho
Hi! I´ve got a little question on reference counting on DirectX. I´m currently using DDraw 6.1 and I´m a little bit confused on when do I have to call the Release() method on an interface. For example, if I have this code snippet: LPDIRECTDRAW4 lpDD; DirectDrawCreate(NULL,&lpDD,NULL); The reference counting on lpDD increments to one, right? But what if I have a bunch of globals in my program that keep track of all the interfaces created and I make an assigment such as the following one? LPDIRECTDRAW4 lpDD2; // Suppose this is global. lpDD2 = lpDD; // Assume lpDD is valid. Does the reference count increment in this situation? What will I have to do in the shutdown function? This? if(lpDD) { lpDD->Release(); lpDD = NULL; } or this? if(lpDD) { lpDD->Release(); lpDD = NULL; } if(lpDD2) { lpDD2->Release(); lpDD2 = NULL; } I hope you can help me. I looked it up in the sdk help but the explanation was not very clear. Thanks in advance!
atcdevil
atcdevil
the 1st one, (I tried to explain myself but it didn't make sense)

EDIT - It'd be a good idea to set the other pointer to NULL as well

[edited by - atcdevil on July 31, 2002 2:12:03 PM]
smanches
smanches
I just want to say; Why are you using globals?!?!?! Wasn''t anybody taught in school to never usee globals?

If you copy an interface it will not increment the ref count. You have to do a lpDD2->AddRef() on it. Then do the two releases later.

Stephen Manchester
Senior Technical Lead
Virtual Media Vision, Inc.
stephen@virtualmediavision.com
(310) 930-7349
Stephen ManchesterSenior Technical LeadVirtual Media Vision, Inc.stephen@virtualmediavision.com(310) 930-7349
don
don
BTW, DirectDrawCreate returns an IDirectDraw *, not IDirectDraw4 *. Use QueryInterface to retrieve the associated IDirectDraw4 interface pointer. This is demonstrated in the SDK samples. It''s also in the DX6.1 SDK docs under "Retrieving Newer Interfaces".


smanches, lighten up on his choice to use variables of global scope in his app. It''s his code and it really has nothing to do with his question. Variables with global scope do have a legitimate use. To make the blanket statement that they should never be used is silly.

Nacho
Nacho
Thanks for the replies! No, in fact I´m not using globals. I have a class that builds the objects related to DirectDraw and I want to keep a copy of them inside the class. In fact most of them are 1 at most (there´s just one 1º surface, one dd object, one clipper, etc), except for the offscreen surfaces (which depend on the sprites used). I didn´t want to put the class here because understanding my question wouldn´t have been so simple. don, I forgot to write the QueryInterface thing here in my post but it is implemented in that way in my function. Thanks for telling me anyway. smanches, thanks for your reply. This sentence: "If you copy an interface it will not increment the ref count" explained what I wanted to know. Thanks for your answers!!
smanches
smanches
Now that I look at the post, I have no idea why I thought you were using globals. I must be tired. Yep, I''m tired.

Stephen Manchester
Senior Technical Lead
Virtual Media Vision, Inc.
stephen@virtualmediavision.com
(310) 930-7349
Stephen ManchesterSenior Technical LeadVirtual Media Vision, Inc.stephen@virtualmediavision.com(310) 930-7349
Nacho
Nacho
quote:
Original post by smanches
Now that I look at the post, I have no idea why I thought you were using globals. I must be tired. Yep, I''m tired.

Stephen Manchester
Senior Technical Lead
Virtual Media Vision, Inc.
stephen@virtualmediavision.com
(310) 930-7349


No, you´re not tired! I said I used globals in my first post but just to keep things simple. Anyway, thanks a lot for your help!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.