Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting memory leaks on Ref-counted COM objects (Addref without Release)

I'm writing a Direct3D 10 application and want to make sure I don't have COM objects leaking.

Yes, I am wrapping the interfaces with CComPtr, but I'd like a leak check anyway for the same reason I still use an ordinary heap allocation leak detector even though RAII and smart pointers guarantees there will be no leaks: sometimes (especially when interfacing to a C library like Direct3D) you have to fallback to lower levels of abstraction and use raw pointers, new, delete, and you make mistakes. (Not to mention some calls to Release() don't return 0 at the end of the program)

I've #defined _ATL_DEBUG_INTERFACES before I include atlbase.h but nothing appears in the output window! Is there something else I need to do to get _ATL_DEBUG_INTERFACES to work?

like image 574
Jeff Linahan Avatar asked Oct 11 '22 05:10

Jeff Linahan


1 Answers

If you're using CComPtr then that's generally the main thing I'd advise, but I did find something that might help, if you're using Visual Studio.

Sara Fords blog has a really cool tip:

Debugging AddRef/Release issues in VS

You might get a lot of information logged, but the basic idea is to trace your addref/release calls in Visual Studio and you can look for mismatched pairs of AddRef/Release.

Might be a bit of legwork but it looks like quite a cunning idea.

Edit 2: Good morning;

Ok, if you're not using DllGetClassObject then that's not going to be a reference problem. Using IUknowns as in/out parameters does have a caveat though; This is the blurb from MSDN for AddRef:

Call this method for every new copy of an interface pointer that you make. For example, if you are passing a copy of a pointer back from a method, you must call AddRef on that pointer. You must also call AddRef on a pointer before passing it as an in-out parameter to a method; the method will call IUnknown::Release before copying the out-value on top of it.

Similarly for release:

Call this method when you no longer need to use an interface pointer. If you are writing a method that takes an in-out parameter, call Release on the pointer you are passing in before copying the out-value on top of it.

like image 92
Russ Clarke Avatar answered Oct 27 '22 11:10

Russ Clarke