Memory leak in this case?

This is a discussion on Memory leak in this case? within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, Suppose I have a component (class) CX implements IUnknown interface, and I have retrieved a pointer of CX ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    Memory leak in this case?

    Hello everyone,


    Suppose I have a component (class) CX implements IUnknown interface, and I have retrieved a pointer of CX by IUnknown pointer (IUnknown*), that is.

    Code:
    IUnknown* pCX;
    QueryInterface (IID_IUnknown, &pCX);
    
    //...
    
    delete pCX; // memory and resource leak here?
    Even if I declare the component CX's destructor as virtual, if I delete through IUnknown pointer to "release" the object, there is still potential memory and resource leak, because in IUnknown interface, destructor is the compiler provided default one, which is non-virtual and public, right?


    thanks in advance,
    George

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by George2 View Post
    Hello everyone,


    Suppose I have a component (class) CX implements IUnknown interface, and I have retrieved a pointer of CX by IUnknown pointer (IUnknown*), that is.

    Code:
    IUnknown* pCX;
    QueryInterface (IID_IUnknown, &pCX);
    
    //...
    
    delete pCX; // memory and resource leak here?
    Even if I declare the component CX's destructor as virtual, if I delete through IUnknown pointer to "release" the object, there is still potential memory and resource leak, because in IUnknown interface, destructor is the compiler provided default one, which is non-virtual and public, right?


    thanks in advance,
    George
    An interface obtained through QueryInterface always has its refcount increased. The correct way to counter this is to cause the refcount to be decreased again when the interface pointer is no longer needed.
    Don't ever explicitly delete COM objects! You should always allow the last Release call to free them.

    In this case you should do this:
    Code:
    CComPtr<IUnknown> spCX;
    QueryInterface(IID_IUnknown, &spCX);
    That's it. Now when the CComPtr is destroyed, Release gets called automatically.
    Note that this is using ATL, which I use all the time at work. You may have to include some header file(s) that you aren't currently using.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,765
    CComPtr is hidden deep within atlbase.h
    Very nice class, CComPtr.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks iMalc,


    Question answered. Cool!!

    Quote Originally Posted by iMalc View Post
    An interface obtained through QueryInterface always has its refcount increased. The correct way to counter this is to cause the refcount to be decreased again when the interface pointer is no longer needed.
    Don't ever explicitly delete COM objects! You should always allow the last Release call to free them.

    In this case you should do this:
    Code:
    CComPtr<IUnknown> spCX;
    QueryInterface(IID_IUnknown, &spCX);
    That's it. Now when the CComPtr is destroyed, Release gets called automatically.
    Note that this is using ATL, which I use all the time at work. You may have to include some header file(s) that you aren't currently using.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 09:18 PM
  3. Reducing Code size from ridiculous length
    By DanFraser in forum C# Programming
    Replies: 10
    Last Post: 01-18-2005, 04:50 PM
  4. Keypress reading
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2004, 12:16 PM
  5. Changing bkgrnd color of Child windows
    By cMADsc in forum Windows Programming
    Replies: 11
    Last Post: 09-10-2002, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21