EDN Admin
Well-known member
I have a piece of C++ code that interacts with a managed DLL which performs some web service calls on its behalf and returns a struct of the data that is returned from the Web Services calls.
Unfortunately, I seem to be leaking memory, and I cant seem to figure out why. I have follwed the example code from Microsoft, but still I seem to be leaking. I was wondering if it is necesary to free the structs that are returned from COM explicitliy,
even though I have not found mention of having to do so in the articles I have read. I know that the two bits of memory are separate, and we have confirmed that everythign is getting GCd on the managed side. We also implemented an explicit Dispose()
method on the managed side to force GC and have verified that is working.
The code I have is as follows:
<div style="color:Black;background-color:White; <pre>
BOOL Class::MyFunction()
{
HRESULT hr;
CoInitialize(NULL);
Interop::IInteropPtr pIInterop;
pIInterop = <span style="color:Blue; new Interop::IInteropPtr(__uuidof(Interop::InteropClass));
InteropTypes::InteropListing listing;
listing = pIInterop->IGetContents(); <span style="color:Green; // listing is a struct
hr = S_OK;
<span style="color:Blue; for (<span style="color:Blue; int i = 0; SUCCEEDED(hr) && i < listing.Count; i++)
{
<span style="color:Blue; long lBound, uBound;
SafeArrayGetLBound( listing.VaultNames, 1, &lBound);
SafeArrayGetUBound( listing.VaultNames, 1, &uBound);
<span style="color:Blue; long j = i;
<span style="color:Green; // BSTRING representations of strings for extraction from COM
BSTR bDspName;
BSTR bVuid;
BSTR bRootTkn;
BSTR bModDate;
memset(&bDspName, 0, <span style="color:Blue; sizeof(BSTR));
memset(&bVuid, 0, <span style="color:Blue; sizeof(BSTR));
memset(&bRootTkn, 0, <span style="color:Blue; sizeof(BSTR));
memset(&bModDate, 0, <span style="color:Blue; sizeof(BSTR));
<span style="color:Green; // BSTR and WCHAR* so you can just copy from one to the other
<span style="color:Green; // http://www.codeguru.com/forum/archive/index.php/t-150671.html
SafeArrayGetElement(listing.VaultNames, &j, &bDspName); <span style="color:Green; // Get the display name
SafeArrayGetElement(listing.VaultUuids, &j, &bVuid); <span style="color:Green; // Get the UUID
SafeArrayGetElement(listing.VaultRootTokens, &j, &bRootTkn);
PWCHAR wcURL = L<span style="color:#A31515; "NO URL"; <span style="color:Green; // These are not downloadable, so no URLs.
PWCHAR wcSize = L<span style="color:#A31515; "
Unfortunately, I seem to be leaking memory, and I cant seem to figure out why. I have follwed the example code from Microsoft, but still I seem to be leaking. I was wondering if it is necesary to free the structs that are returned from COM explicitliy,
even though I have not found mention of having to do so in the articles I have read. I know that the two bits of memory are separate, and we have confirmed that everythign is getting GCd on the managed side. We also implemented an explicit Dispose()
method on the managed side to force GC and have verified that is working.
The code I have is as follows:
<div style="color:Black;background-color:White; <pre>
BOOL Class::MyFunction()
{
HRESULT hr;
CoInitialize(NULL);
Interop::IInteropPtr pIInterop;
pIInterop = <span style="color:Blue; new Interop::IInteropPtr(__uuidof(Interop::InteropClass));
InteropTypes::InteropListing listing;
listing = pIInterop->IGetContents(); <span style="color:Green; // listing is a struct
hr = S_OK;
<span style="color:Blue; for (<span style="color:Blue; int i = 0; SUCCEEDED(hr) && i < listing.Count; i++)
{
<span style="color:Blue; long lBound, uBound;
SafeArrayGetLBound( listing.VaultNames, 1, &lBound);
SafeArrayGetUBound( listing.VaultNames, 1, &uBound);
<span style="color:Blue; long j = i;
<span style="color:Green; // BSTRING representations of strings for extraction from COM
BSTR bDspName;
BSTR bVuid;
BSTR bRootTkn;
BSTR bModDate;
memset(&bDspName, 0, <span style="color:Blue; sizeof(BSTR));
memset(&bVuid, 0, <span style="color:Blue; sizeof(BSTR));
memset(&bRootTkn, 0, <span style="color:Blue; sizeof(BSTR));
memset(&bModDate, 0, <span style="color:Blue; sizeof(BSTR));
<span style="color:Green; // BSTR and WCHAR* so you can just copy from one to the other
<span style="color:Green; // http://www.codeguru.com/forum/archive/index.php/t-150671.html
SafeArrayGetElement(listing.VaultNames, &j, &bDspName); <span style="color:Green; // Get the display name
SafeArrayGetElement(listing.VaultUuids, &j, &bVuid); <span style="color:Green; // Get the UUID
SafeArrayGetElement(listing.VaultRootTokens, &j, &bRootTkn);
PWCHAR wcURL = L<span style="color:#A31515; "NO URL"; <span style="color:Green; // These are not downloadable, so no URLs.
PWCHAR wcSize = L<span style="color:#A31515; "