473,471 Members | 2,040 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2137
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.