T
Trokey
I am making interop calls to an object in a .NET component from a C++
program and am leaking memory... the following is some sample code:
////////////////////////////////////////
// .NET component (Test)
////////////////////////////////////////
// interface
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("D3DF5658-3D3F-436b-B6A6-F10E77AE3F5F")]
public interface IDotNetInterface
{
int getString(string aParam1, string aParam2, out string aValue);
}
// class
[ClassInterface(ClassInterfaceType.None)]
[ProgId("Test.DotNetInterface")]
public class DotNetInterface : IDotNetInterface
{
public int getString(string aParam1, string aParam2, out string aValue)
{
aValue = "some text";
GC.Collect();
return 1;
}
}
///////////////////////////////////
// C++ program
///////////////////////////////////
#import "Test.tlb" no_namespace named_guids
int _tmain(int argc, _TCHAR* argv[])
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);
IDotNetInterfacePtr pPtr = IDotNetInterface(__uuidof(DotNetInterface));
pPtr->AddRef();
_bstr_t lParam1 = "1";
_bstr_t lParam2 = "2";
BSTR lValue;
int lResult = pPtr->getString(lParam1, lParam2, &lValue); // <-- LEAK!!
pPtr->Release();
pPtr = NULL;
}
I've tried all sorts of things (trying to explicitly release the BSTR
memory, etc.), but no effect. What is causing this leak, and how do I solve
it???
Thanks in advance,
Matt
program and am leaking memory... the following is some sample code:
////////////////////////////////////////
// .NET component (Test)
////////////////////////////////////////
// interface
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("D3DF5658-3D3F-436b-B6A6-F10E77AE3F5F")]
public interface IDotNetInterface
{
int getString(string aParam1, string aParam2, out string aValue);
}
// class
[ClassInterface(ClassInterfaceType.None)]
[ProgId("Test.DotNetInterface")]
public class DotNetInterface : IDotNetInterface
{
public int getString(string aParam1, string aParam2, out string aValue)
{
aValue = "some text";
GC.Collect();
return 1;
}
}
///////////////////////////////////
// C++ program
///////////////////////////////////
#import "Test.tlb" no_namespace named_guids
int _tmain(int argc, _TCHAR* argv[])
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);
IDotNetInterfacePtr pPtr = IDotNetInterface(__uuidof(DotNetInterface));
pPtr->AddRef();
_bstr_t lParam1 = "1";
_bstr_t lParam2 = "2";
BSTR lValue;
int lResult = pPtr->getString(lParam1, lParam2, &lValue); // <-- LEAK!!
pPtr->Release();
pPtr = NULL;
}
I've tried all sorts of things (trying to explicitly release the BSTR
memory, etc.), but no effect. What is causing this leak, and how do I solve
it???
Thanks in advance,
Matt