Why wrapping unmanaged C++ singletons can be dangerous 
Author Message
 Why wrapping unmanaged C++ singletons can be dangerous

Ok, after some research, I've come up with some sample code to
demonstrate the issue I brought up in a previous thread regarding the
thread-safety of unmanaged singletons wrapped by managed singletons.
To recap, the basic problem is this:

In managed code, a static object is global to a particular AppDomain,
while in unmanaged code, a static object is global to the entire
process. In an environment such as ASP.NET, where multiple AppDomains
are used in a single process, this can create potential thread-safety
hazards. In particular, it is not sufficient to wrap a
(non-thread-safe) unmanaged singleton with a managed thread-safe
wrapper. The unmanaged singleton must be thread-safe to begin with, or
it is not safe to use from managed code in a multi-AppDomain host such

The sample code is structured as a VS.NET solution with three
- UnsafeSingleton: A Managed C++ Class Library containing an unmanaged
singleton, GlobalCounter, and its managed wrapper, UnsafeSingleton.
- DriverApp: A C# console application that uses the MC++ class library
to increment the shared counter several times and output the results
to the console. Uncomment line 23 of DriverApp.cs to observe what
happens when GlobalCounter relies on UnsafeSingleton to protect it
from concurrent access.
- AppDomainRepro: A C# console application that creates numerous
application domains and executes DriverApp in each one on a new thread
using AppDomain.ExecuteAssembly.

If anybody wants to see the sample code, please e-mail me at
<anti-spam>c_kernel at hotmail dot com</anti-spam>.

Sat, 05 Mar 2005 04:47:08 GMT  
 [ 1 post ] 

 Relevant Pages 

1. Creating verifiable dotnet assembly wrapping unmanaged C++ classes

2. Help me!! wrap unmanaged class with managed c++

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

4. Wrapping Unmanaged Class with std::fstream object fails

5. Wrapping unmanaged operator overloads?

6. why is goto dangerous?

7. Why is allocating with Num Rec dangerous ?

8. String Functions: Why are thy dangerous?

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

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

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

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


Powered by phpBB® Forum Software