Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CComObject<> Problem

CComObject<> Problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestioncomsysadmin
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Braulio Dez
    wrote on last edited by
    #1

    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

    P 1 Reply Last reply
    0
    • B Braulio Dez

      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

      P Offline
      P Offline
      Peter Molnar
      wrote on last edited by
      #2

      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

      B 1 Reply Last reply
      0
      • P 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

        B Offline
        B Offline
        Braulio Dez
        wrote on last edited by
        #3

        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

        P 1 Reply Last reply
        0
        • B Braulio Dez

          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

          P Offline
          P Offline
          Peter Molnar
          wrote on last edited by
          #4

          You 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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups