duplicated Singleton instances problem... 
Author Message
 duplicated Singleton instances problem...

Hi all,

I have a problem with a singleton, I created a singleton
with the:

 DECLARE_CLASSFACTORY_SINGLETON(MySingletonObj)

and it is ok, there is only one instance of the component
interface MySingletonObj. This way each time it is
requested an instance by COM, CoCreateInstanceEx it will
effectivelly create/return the only one instance.

The problem arises when I try to make this object global
(TopLevelObject) for easing the access from a Visual Basic
client. I created an additional interface TopLevelObject
that has a property that creates and returns an instance of
MySingletonObj (created with the "within component way"):

CComObject<MySingletonObj> *pMySingletonObj;
CComObject<MySingletonObj>::CreateInstance(&pMySingletonObj);
hr = pMySingletonObj->QueryInterface(IID_IDispatch,(void**)pVal);

this obviously bypass the Class factory and enables having several
instances of the MySingletonObj object.

Any workarounds to this problem? I don't want to renounce to the
ToLevelObject...

Thanks in advance,
Giovanni



Fri, 09 Jul 2004 17:49:55 GMT  
 duplicated Singleton instances problem...
Use CComObjectGlobal - what the singleton class factory uses.
It must be a global variable instance of course... Or if you want
the object public as well - use CoCreateInstance.

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD

MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

Quote:

> Hi all,

> I have a problem with a singleton, I created a singleton
> with the:

>  DECLARE_CLASSFACTORY_SINGLETON(MySingletonObj)

> and it is ok, there is only one instance of the component
> interface MySingletonObj. This way each time it is
> requested an instance by COM, CoCreateInstanceEx it will
> effectivelly create/return the only one instance.

> The problem arises when I try to make this object global
> (TopLevelObject) for easing the access from a Visual Basic
> client. I created an additional interface TopLevelObject
> that has a property that creates and returns an instance of
> MySingletonObj (created with the "within component way"):

> CComObject<MySingletonObj> *pMySingletonObj;
> CComObject<MySingletonObj>::CreateInstance(&pMySingletonObj);
> hr = pMySingletonObj->QueryInterface(IID_IDispatch,(void**)pVal);

> this obviously bypass the Class factory and enables having several
> instances of the MySingletonObj object.

> Any workarounds to this problem? I don't want to renounce to the
> ToLevelObject...

> Thanks in advance,
> Giovanni



Sun, 11 Jul 2004 05:32:33 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. ATL MFC EXE singleton server, single instance application

2. ATL MFC EXE singleton server, single instance application

3. access violation creating singleton instance

4. access violation cteating singleton instance

5. Avoid duplicate instance

6. Singleton not a singleton?

7. ATL Singleton and Singleton Class also from C++

8. Pleading for fresh eyes on my problem using a PropertyGrid with a Singleton

9. Reference count problem in singleton container

10. singleton : memory problem

11. Problems with Singleton

12. Singleton problem in MFC

 

 
Powered by phpBB® Forum Software