When to use addref?

Started by
8 comments, last by LittleFreak 16 years, 8 months ago
Ive been working with animation in directx and I came across something new that I realy cant get a straight answer from searching on google. the AddRef() function. What is it doing exactly??? When do I use it??? When dont I use it??? If I forget to use it what will happen so I can detect it while debugging???
Advertisement
It's used to indicate that an additional client object is holding a reference to the target object, and that the target object should thus not be destroyed until the client has released it. More info, and even more info.
The easy answer is don't explicitly call AddRef() and Release(). Instead rely on a smart pointer object that does it for you. One lives in <atlbase.h>: CComPtr<>. Example usage:
typedef ATL::CComPtr<IDirect3D9> Direct3DPtr;typedef ATL::CComPtr<IDirect3DDevice9> DevicePtr;Direct3DPtr direct3d;direct3d.Attach(Direct3DCreate9(D3D_SDK_VERSION));DevicePtr device;direct3d->CreateDevice(/* other parameters */, &device);
Im starting to understand... But what happens if I dont call it? Will the memory location of my pointers get over writen giving me corrupt data? Ive never called it on any of my Directx pointers. First time I have scene this used in Directx was in animation with the SKININFO COM. So is this 100% needed or is it just for optimization?
Quote:Original post by LittleFreak
Im starting to understand... SiCrane method seems to much of a hassle... I like to know whats going on exactly in my programs since the one Im currently working on has gotten perty deep and I spend time debugging I like to know whats going wrong.

It is exactly in "deep" programs that conveniences like scoped COM objects become especially important. There are good reasons to do manual reference counting at times. "I like to know whats going on exactly" is not one of them.
Quote:But what happens if I dont call it? Will the memory location of my pointers get over writen giving me corrupt data? Ive never called it on any of my Directx pointers. First time I have scene this used in Directx was in animation with the SKININFO COM. So is this 100% needed or is it just for optimization?

The creation functions for COM objects increment the reference count as part of creation, so if your program only does very simple lifetime management, or has its own layered reference counting mechanism, you can just keep that single reference around until the object should die.
Perty much the way my system is working now is every object's variables are loaded into a link list of structs. Steps are taken to make sure the same file isnt loaded twice (meshs and textures). Then a Class in relationship to the object created is returned that holds a pointer to the structure of the object created. So the things Im seeing in tutorals that use AddRef only exsist 1 place and thats in those structs and those struct are ether deleted when the object is no longer needed or when the program ends. Is it safe to continue not using them?
It's always potentially safe to not use it. The question is whether it is more convenient to use it.
Ive been doing more reading on the subject from the wiki site given. See If I understand this function now. When an object is created the refernce count is set to one. With every call to AddRef this count is incremented and with every call to Release its decremented. If Release is called and the count = 0 then the pointer is deleted? So by not using AddRef every time I create a new pointer to the object If I were to call Release on the pointer to the object I will end up deleting the object rather then just removing the refernce. Is this correct?
Yep, pretty much.
Thank you very much Sneftel you have been a great help. I know what Im doing now. Im not gonna use it in the corrent instance but I can see where I will be using it and thinking of doing further research to add this function to my own objects. You have been alot of help.

This topic is closed to new replies.

Advertisement