Print Page | Close Window

COM protocol non-compliance

Printed From: Codejock Forums
Category: Codejock Products
Forum Name: Toolkit Pro
Forum Description: Topics Related to Codejock Toolkit Pro
URL: http://forum.codejock.com/forum_posts.asp?TID=1969
Printed Date: 09 November 2025 at 12:48am
Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com


Topic: COM protocol non-compliance
Posted By: Green
Subject: COM protocol non-compliance
Date Posted: 15 March 2005 at 12:05pm

Hello I use Xtreme Toolkit Pro v9.60. Look at the next functions:

1)
void CXTPCommandBar::SetImageManager(CXTPImageManager* pImageManager)
{
    // You call InternalRelease() but there is InternalAddRef() call?
    if (m_pImageManager)
        m_pImageManager->InternalRelease();

    m_pImageManager = pImageManager;
}

CXTPCommandBar::~CXTPCommandBar()
{
    ....................
    if (m_pImageManager)
        m_pImageManager->InternalRelease();
    ....................
}

2) And the same notes for CXTPCommandBars implementation: 

CXTPCommandBars::~CXTPCommandBars()
{
    ....................
    // You call InternalRelease() but there is InternalAddRef() call?
    if (m_pImageManager)
        m_pImageManager->InternalRelease();
    ....................
}

void CXTPCommandBars::SetImageManager(CXTPImageManager* pImageManager)
{
 ASSERT(m_pImageManager == NULL);
 m_pImageManager = pImageManager;
}

May be you rely what nobody use it anywhere else. But this is incorrect. Client should not call AddRef() as well. Because it can't know what you want to do with transmitted pointer (use it once for initialization or save it inside for later use).

Lets open any COM book. According to protocol, methods recipient interface pointer as a parameter should call AddRef and Release within their body. 
Otherwise you will have unpredictable behaviour for some cases.




Replies:
Posted By: Sven
Date Posted: 16 March 2005 at 5:56am

If you create a CCmdTarget object the internal reference count is always set to 1. CCmdTarget::InternalRelease decrements this reference count and deletes the object if the reference count is zero.

 



Posted By: Green
Date Posted: 16 March 2005 at 7:24am

Hi, Sven. I know this. But, for example:

1) Parent creates 2 childs and give them the pointer on the same CCmdTarget.
(Reference count will be equal to 1 if we don't call InternalAddRef() for each child). Each child saves pointer for later use.

2) For example one from childs became destroyed. It calls InternalRelease() and CCmdtTarget's reference count became 0 (look into the MFC it will be deleted!!!)But the second child still alive and it has invalid pointer on the CCmdtTarget. So, any CCmdtTarget's method call will generate JPF.

Correct way:

1) Parent creates 2 childs and give them the pointer on the same CCmdTarget.
Each child saves pointer for later use and call InternalAddRef. (Reference count will be equal to 3)

2) If parent don't want to use CCmdTarget by itself it should call InternalRelease. So, the ref count will be equal to 2.

2) For example, one from childs became destroyed. It calls InternalRelease() and CCmdtTarget's reference count became 1 (and it will be still alive!!!). Second child can use it safely.



Posted By: umcbrad
Date Posted: 26 September 2005 at 3:45pm

any more information on this topic?

I'm not 100% sure, but I think that Green is correct...



Posted By: Oleg
Date Posted: 26 September 2005 at 11:44pm

Hi,
We don't call InternalAddRef to allow such code:

 

pCommandBars->SetImageManager(new CXTPImageManager());

if you need to share ImageManager between two controls you must call AddRef manually:

CXTPImageManager* pImageManager = pCommandBars->GetImageManager();

pImageManager->InternalAddRef();

m_wndReport.SetImageManager(pImageManager);



-------------
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.04 - http://www.webwizforums.com
Copyright ©2001-2021 Web Wiz Ltd. - https://www.webwiz.net