How can I say that I am leaking COM objects?

I am writing code that makes (relatively simple) use of COM, calling AddRef () for some objects and Release () them later. Besides just checking the code in fact, is there a way to check if I damage COM objects?

(I cannot use the counted IBlahBlahPtr link because I need to pass objects to the API set that do not know what COM is and therefore do not understand the whole thingy reference count pointers - they skip the pointer around as a token.)

Thanks!

+6
source share
4 answers

This is no different than checking for leaks in any C or C ++ code. Use <crtdbg.h> for leak detection, the MSDN library article is here . You will receive a leak report for the factory class if there were not enough IUnknown :: Release () calls.

Link counting interface pointers are a complex COM requirement; you cannot just wave it off. If the client code does not do this, you will have to take care of yourself before passing the pointer to this code. Knowing when the pointer is no longer in use is of course a more complex problem.

+1
source

If you use CrtDebug DEBUG_NEW to allocate your objects, you will receive an automatic dump of all leaked objects during exit (basically, all the memory that is not freed), as well as the file name and the line in which the memory was allocated.

0
source

Based on our conversation in the comments, I would say that you could do the following:

  • Use smart pointers (i.e. IBlahBlahPtr ) to create and manage COM objects in your own code.
  • Maintain a collection of smart pointers representing your caller links to pointers that you walked up. Each time you pass a new COM pointer to your caller, put its smart pointer in the collection.
  • If your caller somehow refuses the COM pointer (for example, passing you the COM pointer token as some kind of "release" function), then find its smart pointer in the collection and delete it. If this smart pointer (now representing a non-existent link to the call object) is the only remaining reference account holder for the object, then destruction will occur at will.
  • If your caller passes you a COM pointer without removing it, you can wrap the new smart pointer object around the raw pointer value for the duration of the call, just so that your use of smart pointers inside your own code is consistent. This is great if multiple smart pointers reference the same COM object.
0
source

Various tools will test for you. BoundsChecker does. I think, but I'm not 100% sure what AppVerifier does (it has the added benefit of being free).

0
source

Source: https://habr.com/ru/post/886287/


All Articles