managed C++ Singleton and usage in VB 
Author Message
 managed C++ Singleton and usage in VB

Can someone give me some guidance as to how to create a Singleton object in
managed C++ for use in Visual Basic.NET? I can't seem to create one without
explicitly calling some constructor or another, plus I'm getting a
NullReferenceException when I try to call the Instance() routine. How do you
do it?

Thanks



Wed, 25 May 2005 15:34:50 GMT  
 managed C++ Singleton and usage in VB
#include "stdafx.h"

#using <mscorlib.dll>

__gc public class Singleton

{

protected:

static Singleton* instance;

Singleton() {}

public:

static Singleton* getInstance();

Quote:
};

Singleton* Singleton::getInstance()

{

if (instance == 0)

instance = new Singleton();

return instance;

Quote:
}

make sure that Instance() method of your's is delcared as static.
-dec


Quote:
> Can someone give me some guidance as to how to create a Singleton object
in
> managed C++ for use in Visual Basic.NET? I can't seem to create one
without
> explicitly calling some constructor or another, plus I'm getting a
> NullReferenceException when I try to call the Instance() routine. How do
you
> do it?

> Thanks



Thu, 26 May 2005 05:34:26 GMT  
 managed C++ Singleton and usage in VB
P.S. helping you solve that problem helped me solve some stupid ass problem
I have been stuck on for like 2 hours...so thanx hehe

-dec


Quote:
> Can someone give me some guidance as to how to create a Singleton object
in
> managed C++ for use in Visual Basic.NET? I can't seem to create one
without
> explicitly calling some constructor or another, plus I'm getting a
> NullReferenceException when I try to call the Instance() routine. How do
you
> do it?

> Thanks



Thu, 26 May 2005 05:35:45 GMT  
 managed C++ Singleton and usage in VB

Quote:

> if (instance == 0)

> instance = new Singleton();

This is perfect code as long as this method is only called on a single
thread.  If the program is at all multithreaded, you definitely want to make
sure a lock is taken before executing the if statement.

You can easily do this by putting the following attribute on the method from
the System::Runtime::CompilerServices namespace.
   [MethodImpl(MethodImplOptions::Synchronized)]

There are probably more efficient design patterns out there, but be
cautious -- some design suggested design patterns don't always work (such as
double checked locking).

Cheerio!

--
Brandon Bray                                          Visual C++ Compiler
This posting is provided AS IS with no warranties, and confers no rights.



Sat, 28 May 2005 04:05:25 GMT  
 managed C++ Singleton and usage in VB
Hi Brandon,

Quote:
> > if (instance == 0)

> > instance = new Singleton();

> This is perfect code as long as this method is only called on a single
> thread.

You are absolutely right on this. However, wouldn't the easier way be to
make use of type initializers (aka static constructors) instead of locking?

--
Tomas Restrepo



Sat, 28 May 2005 07:52:13 GMT  
 managed C++ Singleton and usage in VB

Quote:

> You are absolutely right on this. However, wouldn't the easier way be to
> make use of type initializers (aka static constructors) instead of
> locking?

Hi Tomas,
  You're right.  To my chagrin, I completely forgot that static class
constructors were even supported in the language right now.  Doh!

--
Brandon Bray                                          Visual C++ Compiler
This posting is provided AS IS with no warranties, and confers no rights.



Sat, 28 May 2005 08:20:09 GMT  
 managed C++ Singleton and usage in VB
How is the static class implemented for this scenario?

can you throw down a code sample? Having a lil trouble conceptualizing it..

cheers
-dec



Quote:

> > You are absolutely right on this. However, wouldn't the easier way be to
> > make use of type initializers (aka static constructors) instead of
> > locking?

> Hi Tomas,
>   You're right.  To my chagrin, I completely forgot that static class
> constructors were even supported in the language right now.  Doh!

> --
> Brandon Bray                                          Visual C++ Compiler
> This posting is provided AS IS with no warranties, and confers no rights.



Sat, 28 May 2005 11:41:36 GMT  
 managed C++ Singleton and usage in VB

Quote:

> How is the static class implemented for this scenario?

> can you throw down a code sample? Having a lil trouble conceptualizing
> it..

Sure.  The singleton pattern is enforced by the fact that the CLR guarantees
a static class constructor will be called exactly once.  The code you
originally posted would be written as follows to use this design:

#using <mscorlib.dll>

__gc class Singleton {
protected:
  static Singleton* instance;

  static Singleton() {
    instance = new Singleton();
  }

  Singleton() {}

public:
  static Singleton* getInstance();

Quote:
};

Singleton* Singleton::getInstance() {
  return instance;

Quote:
}

--
Brandon Bray                                          Visual C++ Compiler
This posting is provided AS IS with no warranties, and confers no rights.


Sat, 28 May 2005 12:13:55 GMT  
 managed C++ Singleton and usage in VB
Oh I thought you ment like ...
__gc static class Xxx { };

In any case, I fail to see how making the constructor static changes
anything, multithreaed or not...

If you have a private constructor and a static method which calls this
constructor if and only if it has never been called before, what does it
matter how many threads try to capture the object? Multiple threads won't
create multiple objects =P

To take it furthure...
(if instance == 0) instance = new ClassName();

Is it even possible for a thread to get throgh the if and stop right before
class instantiation only to let another thread start up and complete the if
check and actualy instatiate the object then stopping and the first thread
instatiates again??? I always thought threaed or not, an executable line is
locked. Rather, if a thread starts to execute a line of code it does not
stop until it hits a semi-collon....am I wrong on this?

Thanks for you feedback and helping me to furthure my knowledge
-dec



Quote:

> > How is the static class implemented for this scenario?

> > can you throw down a code sample? Having a lil trouble conceptualizing
> > it..

> Sure.  The singleton pattern is enforced by the fact that the CLR
guarantees
> a static class constructor will be called exactly once.  The code you
> originally posted would be written as follows to use this design:

> #using <mscorlib.dll>

> __gc class Singleton {
> protected:
>   static Singleton* instance;

>   static Singleton() {
>     instance = new Singleton();
>   }

>   Singleton() {}

> public:
>   static Singleton* getInstance();
> };

> Singleton* Singleton::getInstance() {
>   return instance;
> }

> --
> Brandon Bray                                          Visual C++ Compiler
> This posting is provided AS IS with no warranties, and confers no rights.



Sun, 29 May 2005 14:43:20 GMT  
 managed C++ Singleton and usage in VB
Decrypted,

Quote:
> Oh I thought you ment like ...
> __gc static class Xxx { };

> In any case, I fail to see how making the constructor static changes
> anything, multithreaed or not...

Actually, it does.

Quote:

> If you have a private constructor and a static method which calls this
> constructor if and only if it has never been called before, what does it
> matter how many threads try to capture the object? Multiple threads won't
> create multiple objects =P

If the object is a singleton, it sure matters as hell. You might very well
end up creating more than one object, which defeats the whole purpose of
making it a singleton.

You're confusing what a static constructor does, though. The runtime
guarantees that the static constructor will be called exactly once, even in
the presence of multiple threads, the first time a static member of the type
is accessed.

Quote:
> To take it furthure...
> (if instance == 0) instance = new ClassName();

> Is it even possible for a thread to get throgh the if and stop right
before
> class instantiation only to let another thread start up and complete the
if
> check and actualy instatiate the object then stopping and the first thread
> instatiates again???

Yep. Very possible.

Quote:
> I always thought threaed or not, an executable line is
> locked.

Not at all. A line of code could generate dozens of individual instructions.
And why lock them, anyway? most apps aren't multithreaded, and so, locking
on them would only cause a performance hit.

Quote:
> Rather, if a thread starts to execute a line of code it does not
> stop until it hits a semi-collon....am I wrong on this?

Completely :)

--
Tomas Restrepo



Sun, 29 May 2005 18:57:39 GMT  
 
 [ 10 post ] 

 Relevant Pages 

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

2. Question about converting code from VB .NET to Managed C++ / C#

3. pass vb.net function pointer to Managed C++

4. using Managed dlls from Managed C++

5. Singleton not a singleton?

6. managed C++ wrapper around unmanaged C++ classes: causing StackOverflow exception

7. Performance of unmanaged C++ in a managed C++ app

8. managed c++ vs unmanaged c++

9. How to pass a function pointer from Managed C++ to unmanaged c++

10. C# client crashs when calling into Managed C++ which calls unmanaged c++ function

11. Inherit unmanaged c++ classes from .Net platform (managed c++ or c#)

12. Fatal Error C1010 in Mixing Managed C++ and Unmanaged C++ Code

 

 
Powered by phpBB® Forum Software