473,406 Members | 2,259 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,406 software developers and data experts.

ASP.NET and managed C++: Access violation when AppDomain is unloaded.

Hello,

(sorry for reposting this.)

We are having the following scenario:

A ASP.NET Application (C#) is using a managed C++ assembly, which is a
thin wrapper around an old C++ lib.
(We are using .NET framework 1.1)

In the managed C++ assembly there are some managed (gc) and some
unmanaged (nogc) classes. There is one unmanaged class (a singleton),
which has a reference (gcroot/GCHandle) to a System.Collections.Hashtable.

So the callgraph is something like this: .NET --> C++ --> .NET

Everything works fine, until ASP.NET decides to unload and reload the
AppDomain. (e.g. touching the web.config/machine.config).
If now a call is made to the component, it crashes at the point, where
the referenced Hashtable (GCHandle) is used.

I saw a few articles about this problem, including a bug report in msdn
(http://support.microsoft.com/default...;EN-US;309694).
The bug report applies to .NET framework 1.0, so I'm wondering, if this
bug still exists in 1.1?

Is this a known bug in 1.1 too? Will the next version of the framework
fix this?
Are there any other workarounds beside the solutions in 309694.

Thanks in advance,

Hans-Jürgen.

Nov 18 '05 #1
5 2696
Hi Hans,

As for the problem you mentioned, do you means that currently if we call
that MC++ component after the asp.net appdomain unload and reload, the
application will crash?

As for the kb article you mentioned, I'll consult some further experts to
confirm this and I think we can do some further tests currrently:

Try creating another simple MC++ assemlby( reference a unamaged dll, but
don't be too complex) and use it in the asp.net web application and do the
same test to see whether the problem reoccur. That'll help confirm whehter
the problem is only specified to the particular Mc++ component.

I'll update you about the kb article's info as soon as possible, and if you
have any other findings , please also feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #2
Hello,
I can reproduce the error in a simple example.

I think the problem is, that the singleton (static variable) in the
managed c++ assembly is not notified of the AppDomain unloading.
So it creates a reference (gcroot) to a managed object in the first
AppDomain and after unloading the AppDomain the reference is invalid.
(->crash).

By the way, there is another question concerning managed c++ Dlls.
The documentation recommends not to implement a Dll-Entry point.
Having static instances of unmanaged classes requires to call
the CRT-initialization. The recommended way is to call
__crt_dll_initialize and __crt_dll_terminate at initialization time (but
not from within Dll-Entry point).

How should I do this in an ASP.NET environment. The methods
Application_Start/Application_End (global.asax) are called more then
once. Every time I touch web.config/machine.config these methods are
called. But the managec c++ dll is loaded just once.
Is it allowed to call __crt_dll_initialize and __crt_dll_terminate more
than once?

What is the recommended point in ASP.NET application to initialize
managed c++ Dlls?

The article I read:
http://support.microsoft.com/default...&product=vcNET

Thanks in advance,
Hans-Jürgen.
Steven Cheng[MSFT] wrote:
Hi Hans,

As for the problem you mentioned, do you means that currently if we call
that MC++ component after the asp.net appdomain unload and reload, the
application will crash?

As for the kb article you mentioned, I'll consult some further experts to
confirm this and I think we can do some further tests currrently:

Try creating another simple MC++ assemlby( reference a unamaged dll, but
don't be too complex) and use it in the asp.net web application and do the
same test to see whether the problem reoccur. That'll help confirm whehter
the problem is only specified to the particular Mc++ component.

I'll update you about the kb article's info as soon as possible, and if you
have any other findings , please also feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Nov 18 '05 #3
Hi Hans,

Thanks for the followup. Yes, I think your thought on the app crash is
reasonable. If so, is it ok that we provide some protected code to catch
such exception and reinitialize the static reference when it is called
after the application reloaded.

Also, as for the further question you mentioned, I'll do some furthe
research and will update you as soon as I got any information. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #4
Hello Steven,

What do you mean by "provide some protected code" ?
Do you mean in the next Version of the runtime or some code in my
managed c++ assembly.

Thanks for your help,
Hans-Jürgen

Steven Cheng[MSFT] wrote:
Hi Hans,

Thanks for the followup. Yes, I think your thought on the app crash is
reasonable. If so, is it ok that we provide some protected code to catch
such exception and reinitialize the static reference when it is called
after the application reloaded.

Also, as for the further question you mentioned, I'll do some furthe
research and will update you as soon as I got any information. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Nov 18 '05 #5
Hi Hans,

Thanks for the followup. The protected code I mentioned means some thing in
our own code ( in that mc++ assmelby).
Also, I've got some suggestions from some other experts, here are their
original description regarding on your two question:

==========================
For the first issue, this should be fixed in 2003. The issue was with the
inability to retain the appdomain for virtual function calls across a
transition boundary. In 2003, VC++ and the CLR implemented support to
retain the originating appdomain across the transition boundary. You will
need to rebuild everything C++ that you built with /clr with the 2003
compiler and then use the 2003 CLR to get the fix.

For the second issue, it is safe to call __crt_dll_initialize/terminate
multiple times. You definitely do not want to call either of these
functions while you thread has obtained the loader lock. One such occasion
is during a call to a DllMain (or something that DllMain calls such as
global object constructors).
============================

Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #6

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

Similar topics

1
by: BillyO | last post by:
In the attached code fragment I have a buffer overflow and a memory access violation. When I run the code .Net fails to verify the IL because of the buffer overflow and I get an exception as...
0
by: Francis Tsui | last post by:
We have a COM object implemented using managed C++. When the COM object is loaded from unmanaged code it works fines. However when we try to use the managed COM object through some unmanged code...
2
by: cameron | last post by:
I have a page that needs to process aproximately 7000 LDAP entries and does 10-12 SQL DB calls per entry. It is a busy, busy page. I have it schedualed to run every night at 4 am server time. It...
1
by: Charles Herring | last post by:
In my asp.net application I upload a video file and then need to get the duration and other properties. I get "Attempted to access an unloaded AppDomain" when I instanciate the Video object. I am...
0
by: news | last post by:
Hello, We are having the following scenario: A ASP.NET Application (C#) is using a managed C++ assembly, which is a thin wrapper around an old C++ lib. (We are using .NET framework 1.1) In...
5
by: jake | last post by:
I've seen many articles that claim the files in the App_Code directory are treated in the same way as aspx-files in ASP 1.1. Well, this does not seem to be the case, at least in Web Developer 2005...
1
by: Basti | last post by:
Hello, I'm working with .Net 2.0. I tried to get attributes of an unloaded assembly. So, the sole way I know is to create a new child application domain, in this app domain I load the assembly,...
0
by: draskin | last post by:
Hello, We have a situation where a managed C++ assembly links with native C++ dll. There is a callback mechanism which calls back into the managed code asynchronously. Since native classes...
2
by: Dave Burns | last post by:
Hello, We have a situation where a managed C++ assembly links with native C++ dll. There is a callback mechanism which calls back into the managed code asynchronously. Since native classes...
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
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
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
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...
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,...
0
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...

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.