I am debugging a piece of code suspecting memory leaks in com objects. Structure of the code is something like below:
Some method:
- Create a new instance of a com object into com smart ptr.
- Get its interface pointer, Addref and add it to a map.
Destructor:
- loops through the map and deletes all com objects. E.g.
delete iptr
This shows as probable leak in umdh tool output. The correct way to free the object should be to call release on all objects in that map.
What effect will calling delete have?
You mean you simply do
delete ptr;
whereptr
is the pointer to the COM Interface!?delete
is for destroying objects that were created usingnew
. That is the only correct use ofdelete
. It's not a valid thing in general todelete
anything that was not orignally created usingnew
. So unless you created the object that implements the COM interface yourself using anew
expression, doing so will result in undefined behavior. As you said yourself, COM interfaces have to be released by calling theRelease()
method. That is the only correct way to let go off a COM interface…Consider the fact that a COM interface is not the same thing as an object. The same object can implement multiple different COM interfaces, which you can query via the
QueryInterface()
method. There can also be multiple references to the same interface. So not every call toRelease()
necessarily destroys an object or free memory to begin with…