Re: How do you free COM memory and resources?
AndersG schrieb:
Thanks Joe, basically what I do is:
hr = m_Application.CreateInstance("CrystalRuntime.Application");
m_Report = m_Application->OpenReport(ReportPath, dummy);
job->PrintOut(vPromptUser, vnCopies, dummy, dummy, dummy);
if(m_Report!=NULL) m_Report->Release();
if (m_Application != NULL) m_Application.Release();
So I assume that I release all that I have grabbed? Is there a way to
see what my referece cont is?
I have not much experience with COM but I worked with OPC which is
com-based.
I have learned two rules, though:
Use CComPtr to deal with COM interface pointers. That class does all
AddRef and Release automatically, so usually you don't need to be
worried about correct releasing of the objects.
When COM functions return data, then you need to free the data after
use, usually using the IMalloc allocator interface (use CoGetMalloc to
get the allocator, or call CoTaskMemFree on the returned data pointers).
I see many dummy arguments in your COM function calls. Maybe the
functions return valid data pointers that need to be freed instead just
ignored with a "dummy" argument? The function documentation should tell
you that.
Norbert
If I look at the program with
ProcessExplorer I see the viewer being loaded first time it is used,
but it does not unload even after being released. Also, the leak only
happens when I either preview on screen or export to PDF. If I just
print, then there is no leak. I have leak detection enabled on my
project so I assume that I would see any leaks that I cause, but not
leaks in loaded DLLs.