CComObject<> Problem
-
Hi, I have a COM server, and a private class for the internal use of the COM object, what I want to do is to add a a method that returns a CComObject<> * Object, I have two problems: If I make this:
CComObject *DBGroup::GetGroupConnections(_bstr_t strGroup) { CComObject* pConnections; HRESULT hr = CComObject::CreateInstance(&pConnections); (...) return pConnections; }
1. I get a compiler error telling me that he don't knows about the Connections com component that I have created in the same server. 2. If Instead of that I return and IConnection interface, Do I generate there a leakage ? ( I don't call the "delete pConnection"), what is the solution or workaround for this problem ? Thanks in advance, greetings Braulio -
Hi, I have a COM server, and a private class for the internal use of the COM object, what I want to do is to add a a method that returns a CComObject<> * Object, I have two problems: If I make this:
CComObject *DBGroup::GetGroupConnections(_bstr_t strGroup) { CComObject* pConnections; HRESULT hr = CComObject::CreateInstance(&pConnections); (...) return pConnections; }
1. I get a compiler error telling me that he don't knows about the Connections com component that I have created in the same server. 2. If Instead of that I return and IConnection interface, Do I generate there a leakage ? ( I don't call the "delete pConnection"), what is the solution or workaround for this problem ? Thanks in advance, greetings Braulio1.Initialize your object
CComObject<CYourBaseClass>* pConnection;
...
pConnection->AddRef();
return pConnections;2.Returning a pointer cuases very much a memory leak UNLESS you destroy it in your caller function
CComObject *pConnection = GetGroupConnections(...);
pConnection->...
pConnection->Release();Peter Molnar
-
1.Initialize your object
CComObject<CYourBaseClass>* pConnection;
...
pConnection->AddRef();
return pConnections;2.Returning a pointer cuases very much a memory leak UNLESS you destroy it in your caller function
CComObject *pConnection = GetGroupConnections(...);
pConnection->...
pConnection->Release();Peter Molnar
So, I can do directly ?
CComObject* pConnection; pConnection->AddRef(); return pConnections;
And I don't have to call "CreateInstance" ? If I want to return my object as an interface( e.g. a list of users that matches a certain criteria), so the collection will not be hold as a member variable, can I use ?get_Connection(IConnection **pVal) { CComObject* pConnection; pConnection->AddRef(); return pConnections->QueryInterface(...) }
And then in the client code retrieve that value with an smart pointer ?IConnectionPtr pConn = pMyComObject->Connection;
So with the smart pointer, the object will be released automatically ? Thanks in advance, greetings Braulio -
So, I can do directly ?
CComObject* pConnection; pConnection->AddRef(); return pConnections;
And I don't have to call "CreateInstance" ? If I want to return my object as an interface( e.g. a list of users that matches a certain criteria), so the collection will not be hold as a member variable, can I use ?get_Connection(IConnection **pVal) { CComObject* pConnection; pConnection->AddRef(); return pConnections->QueryInterface(...) }
And then in the client code retrieve that value with an smart pointer ?IConnectionPtr pConn = pMyComObject->Connection;
So with the smart pointer, the object will be released automatically ? Thanks in advance, greetings BraulioYou have very much to call CreateInstance, which I ment to indicate with ... (dots) in my code. Since it was OK in yours, I didn' repeated it again, modify your get_Connection function accordingly. AddRef tells the object that there will be one more call on it, it should not unload itself, and Release tells that the call have already taken place, it can unload. So after you received the interface pointer,you should release it. Smart pointers indeed eliminate this, because they call AddRef on object creation and Release on when the object goes out of scope. Peter Molnar