BUG: AppDomainUnloadedException is thrown when you call a virtual destructor on a __nogc class during an AppDomain unload (837317)
The information in this article applies to:
- Microsoft Visual C++ .NET (2003)
- Microsoft Visual C++ .NET (2002)
- Microsoft .NET Framework 1.1
- Microsoft .NET Framework 1.0
- Microsoft Common Language Runtime (included with the .NET Framework 1.1)
- Microsoft Common Language Runtime (included with the .NET Framework) 1.0
SUMMARYWhen you try to unload an application domain (AppDomain) after you create an instance of an unmanaged C++ class, you may receive an AppDomainUnloadedException exception if the class has a virtual destructor declared in it. SYMPTOMS When you try to unload an application domain, you may
receive an AppDomainUnloadedException exception or your program may behave
unpredictably. You may notice this behavior when an instance of an unmanaged
C++ class has a virtual destructor declared in it, and the class is destroyed
while the class unloads ts application domain.CAUSEAs part of the implementation of the It Just Works (IJW)
mechanism, when a managed DLL that you created by using the Visual C++ .NET compiler loads,
the runtime creates thunks for transitions from unmanaged code to managed code.
These thunks contain a reference to the AppDomain where the DLL loads. The
runtime does not re-create these thunks if the DLL loads again. Also, the
runtime does not update the reference when the original AppDomain unloads, and
the DLL loads in another AppDomain. When the unloading of an AppDomain
starts, any transitions to that AppDomain from native code are not permitted
and the AppDomainUnloadedException exception is thrown. This behavior occurs for the following reasons:
- To make sure that incoming calls to the
AppDomain are correctly executed because the finalization of types in the AppDomain is
started.
- To make sure that forward progress is made in the unload
process.
You receive an AppDomainUnloadedException exception because of a
"double thunking" issue that causes the common language runtime to move
to native code and then back to managed code to call the virtual destructor
function. RESOLUTIONTo work around this problem, use one of the following methods: - Change the C++ class definition and its usage from
unmanaged code (__nogc) to managed code (__gc).
- Do not use a virtual destructor in the __nogc
class.
- Use the #pragma unmanaged pragma directive around the __nogc
class to override the /clr setting to force the class to be compiled in
native code.
STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are
listed at the beginning of this article.
REFERENCES
For additional information about AppDomainUnloadedException exception in managed classes, click the following article number to view the article in the Microsoft Knowledge Base:
309694
BUG: AppDomainUnloaded exception when you use Managed Extensions for C++ components
Modification Type: | Major | Last Reviewed: | 4/1/2004 |
---|
Keywords: | kbManaged kbdomain kbDLL kbThunks kbinterop kbGarbageCollect kbConsole kberrmsg kbcode kbbug KB837317 kbAudDeveloper |
---|
|