Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell if I'm leaking COM objects?

Tags:

c++

com

winapi

I'm writing some code that makes (relatively simple) use of COM, calling AddRef() on some objects and Release()ing them later. Other than just checking the code really thoroughly, is there a way I can check to see if I'm leaking COM objects everywhere?

(I can't use reference counted IBlahBlahPtrs because I need to pass the objects to a set of APIs who don't know what a COM is, and so don't understand the whole "reference counting pointers" thingy - they pass the pointer around like a token.)

Thanks!

like image 998
Colen Avatar asked Apr 20 '11 04:04

Colen


1 Answers

It is no different from checking for leaks in any C or C++ code. Use <crtdbg.h> to detect leaks, the MSDN library article is here. You'll get a leak report for the class factory if there were not enough IUnknown::Release() calls.

Reference counting interface pointers is a hard COM requirement, you cannot just shrug it off. If the client code doesn't do it then you'll have to take care of it yourself before you pass a pointer to that code. Knowing when the pointer is no longer in use is of course the trickier issue.

like image 82
Hans Passant Avatar answered Oct 05 '22 22:10

Hans Passant