Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > Visual C++ MFC > Toolkit Pro
  New Posts New Posts RSS Feed - COM protocol non-compliance
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

COM protocol non-compliance

 Post Reply Post Reply
Author
Message
Green View Drop Down
Newbie
Newbie
Avatar

Joined: 14 January 2005
Location: Russian Federation
Status: Offline
Points: 9
Post Options Post Options   Thanks (0) Thanks(0)   Quote Green Quote  Post ReplyReply Direct Link To This Post Topic: COM protocol non-compliance
    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.

Back to Top
Sven View Drop Down
Senior Member
Senior Member


Joined: 21 August 2003
Location: Germany
Status: Offline
Points: 127
Post Options Post Options   Thanks (0) Thanks(0)   Quote Sven Quote  Post ReplyReply Direct Link To This Post 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.

 

Back to Top
Green View Drop Down
Newbie
Newbie
Avatar

Joined: 14 January 2005
Location: Russian Federation
Status: Offline
Points: 9
Post Options Post Options   Thanks (0) Thanks(0)   Quote Green Quote  Post ReplyReply Direct Link To This Post 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.

Back to Top
umcbrad View Drop Down
Newbie
Newbie


Joined: 26 April 2005
Status: Offline
Points: 10
Post Options Post Options   Thanks (0) Thanks(0)   Quote umcbrad Quote  Post ReplyReply Direct Link To This Post 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...

Back to Top
Oleg View Drop Down
Senior Member
Senior Member


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.062 seconds.