How to delete a COM object - My Other Code
-
I have a C# (managed code) DLL and I am accessing this DLL's functions from C++ (unmanaged code) using COM. I create a COM object with CoCreateInstance function. Then I do work on this object. After that I call Release() function on this object. But after calling Release() function the object is not destroyed automatically. After calling Release function I called some more functions on this object and all those calls were successful. I also checked the return value of Release function call which is ZERO which shows the reference count of the object is ZERO and it should automatically destroy the object as soon as the reference count becomes ZERO. But in my case the object is not destroyed. Actually in my project work I have to create and destroy the COM object many times (1000-2000). So in each iteration the memory size is increasing and after some iteration it becomes more than 1 GB and fills all the page file space and the program hangs. Could Anybody suggest me how to delete the COM object. I have also given below the server side code which is in C#. The client side code which I am using is following : #import "ServerSideCode.tlb" named_guids high_method_prefix("") #include #include using namespace ServerSideCode; class Test { public: IEnterDetails *pPER1; IEnterDetails *pPER; IEnterAge *pAge; public: void Func1(); }; void Test::Func1() { pPER1 = NULL; HRESULT hr = CoInitialize(NULL); hr=CoCreateInstance(__uuidof(Manager), NULL, CLSCTX_INPROC_SERVER, __uuidof(IEnterDetails), reinterpret_cast(&pPER1)); pPER1->Print(); long a = pPER1->Release(); a = pPER1->Release(); a = pPER1->Release(); a = pPER1->Release(); // pPER1 = NULL; pPER1->EnterName(); pPER1->Print(); pPER1->QueryInterface(__uuidof(IEnterAge), reinterpret_cast(&pAge)); pAge->EnterAge(); pAge->Print(); a = pPER1->Release(); CoUninitialize(); } void main() { Test tt; tt.Func1(); } The C# code which was used to build the DLL (ServerSideCode.dll) is the following using System; namespace ServerSideCode { public interface IEnterDetails { void EnterName(); void EnterDesignation(); void EnterIncome1(); void EnterIncome2(); void Add(); void Print(); } public interface IEnterAge { void EnterAge(); void Print(); int Age {get; set;} } public class Person : IEnterDetails { private string Name = "DefaultName", Des = "DefaultDesignation"; private int i
-
I have a C# (managed code) DLL and I am accessing this DLL's functions from C++ (unmanaged code) using COM. I create a COM object with CoCreateInstance function. Then I do work on this object. After that I call Release() function on this object. But after calling Release() function the object is not destroyed automatically. After calling Release function I called some more functions on this object and all those calls were successful. I also checked the return value of Release function call which is ZERO which shows the reference count of the object is ZERO and it should automatically destroy the object as soon as the reference count becomes ZERO. But in my case the object is not destroyed. Actually in my project work I have to create and destroy the COM object many times (1000-2000). So in each iteration the memory size is increasing and after some iteration it becomes more than 1 GB and fills all the page file space and the program hangs. Could Anybody suggest me how to delete the COM object. I have also given below the server side code which is in C#. The client side code which I am using is following : #import "ServerSideCode.tlb" named_guids high_method_prefix("") #include #include using namespace ServerSideCode; class Test { public: IEnterDetails *pPER1; IEnterDetails *pPER; IEnterAge *pAge; public: void Func1(); }; void Test::Func1() { pPER1 = NULL; HRESULT hr = CoInitialize(NULL); hr=CoCreateInstance(__uuidof(Manager), NULL, CLSCTX_INPROC_SERVER, __uuidof(IEnterDetails), reinterpret_cast(&pPER1)); pPER1->Print(); long a = pPER1->Release(); a = pPER1->Release(); a = pPER1->Release(); a = pPER1->Release(); // pPER1 = NULL; pPER1->EnterName(); pPER1->Print(); pPER1->QueryInterface(__uuidof(IEnterAge), reinterpret_cast(&pAge)); pAge->EnterAge(); pAge->Print(); a = pPER1->Release(); CoUninitialize(); } void main() { Test tt; tt.Func1(); } The C# code which was used to build the DLL (ServerSideCode.dll) is the following using System; namespace ServerSideCode { public interface IEnterDetails { void EnterName(); void EnterDesignation(); void EnterIncome1(); void EnterIncome2(); void Add(); void Print(); } public interface IEnterAge { void EnterAge(); void Print(); int Age {get; set;} } public class Person : IEnterDetails { private string Name = "DefaultName", Des = "DefaultDesignation"; private int i