473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

C++ singletons and .NET AppDomains

I am struggling with the following:
We have legacy classes written in C++ that are compiled to static
libraries. Some of these classes are singletons. In order to make
these classes available to C#, we wrapped them with Managed C++. The
wrapper classes works fine in most of the cases. However, if they are
used in ASP.NET Web Applications a problem may arise in the following
scenario:
Step 1: The first Web Application starts in its own AppDomain inside
aspnet_wp.exe process and creates wrapper singleton class, which in
turn creates underlying unmanaged C++ singleton.
Step 2: We change the web.config file of our Web Application. This
causes unloading AppDomain of the Web Application.
Step 3: Web Application starts again and a new AppDomain is loaded
upon HTTP request.
Step 4: A new AppDomain gets an instance of Singleton. Since the
underlying C++ singleton has a static variable holding pointer to
singleton instance, this instance lives until aspnet_wp.exe process
exits. So, when trying to access this instance from a new AppDomain
raises AppDomainUnloadedException, since we access memory that was
allocated for an old AppDomain.
The solution I found is using Remoting and create wrapper Singleton
together with the underlying C++ singleton in the "remote" AppDomain.
However, there is too much overhead. Another way is, of course, to
rewrite the unmanaged C++ class and make it non-singleton.

Are there any other cleaner solutions for this problem?

Many thanks,
Yevgeny Menaker
Author, Software Engineer.
Nov 18 '05 #1
1 2134
why don't you update your logic to cause the singleton instance to go away
when the application domain unloads?

--
Regards,
Alvin Bruney
Got tidbits? Get it here...
http://tinyurl.com/3he3b
"Yevgeny Menaker" <ge****@bezeqint.net> wrote in message
news:c1*************************@posting.google.co m...
I am struggling with the following:
We have legacy classes written in C++ that are compiled to static
libraries. Some of these classes are singletons. In order to make
these classes available to C#, we wrapped them with Managed C++. The
wrapper classes works fine in most of the cases. However, if they are
used in ASP.NET Web Applications a problem may arise in the following
scenario:
Step 1: The first Web Application starts in its own AppDomain inside
aspnet_wp.exe process and creates wrapper singleton class, which in
turn creates underlying unmanaged C++ singleton.
Step 2: We change the web.config file of our Web Application. This
causes unloading AppDomain of the Web Application.
Step 3: Web Application starts again and a new AppDomain is loaded
upon HTTP request.
Step 4: A new AppDomain gets an instance of Singleton. Since the
underlying C++ singleton has a static variable holding pointer to
singleton instance, this instance lives until aspnet_wp.exe process
exits. So, when trying to access this instance from a new AppDomain
raises AppDomainUnloadedException, since we access memory that was
allocated for an old AppDomain.
The solution I found is using Remoting and create wrapper Singleton
together with the underlying C++ singleton in the "remote" AppDomain.
However, there is too much overhead. Another way is, of course, to
rewrite the unmanaged C++ class and make it non-singleton.

Are there any other cleaner solutions for this problem?

Many thanks,
Yevgeny Menaker
Author, Software Engineer.

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Daylor | last post by:
hi. i have mult thread vb.net application. it has 30 threads. each thread serves 1 phone caller. the question : is there a reason , to create appdomain for each Phone Service ? meaning 30...
4
by: Mountain Bikn' Guy | last post by:
I need some advice on this. I am working on a fairly complex calculator app (C#) with lots of functions (and these functions in turn use math functions from an unmanaged C DLL). A calculation takes...
0
by: Yevgeny Menaker | last post by:
I am struggling with the following: We have legacy classes written in C++ that are compiled to static libraries. Some of these classes are singletons. In order to make these classes available to...
11
by: John Fly | last post by:
I'm working on a large project(from scratch). The program is essentially a data file processor, the overall view is this: A data file is read in, validated and stored in a memory structure...
1
by: billr | last post by:
hi there, I hope that someone will be able to shed some light on little old confused me. We are developing an application which will be deployed onto a Terminal Server machine. The application...
1
by: mortb | last post by:
Hi, If I want to implement a singleton using a class that has a static variable containing the singleton object, can I guarantee that it will be a singleton to my entire webapplication on a...
0
by: aghalnan | last post by:
In doing some research it is said that a singleton is bounded to the appdomain that created it. But im experiencing and issue where my singleton is crossing appdomains. Now both my appDomains are...
8
by: Fred Mertz | last post by:
I'm working towards an MCTS cert and I'm having to learn a bunch of stuff that I doubt I'd stumble across on my own. One such feature of .NET is AppDomains; programatically creating new AppDomains...
3
by: | last post by:
If this is simple, forgive my ignorance, but I'm coming from the CompactFramework where we don't use AppDomains. I did a fair bit of archive searching and couldn't find an answer and I got no...
0
by: jeremyje | last post by:
I would like to create an application where I have many concurrent processes being managed by a monitoring process. Each process that is "managed" will be invoked from an assembly dll (think...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.