BUG: AppDomainUnloaded ????? ?? ????? ???? ???????? ????? C++ ????? ?? ??? ?????????

???? ?????? ???? ??????
???? ID: 309694 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

?????

?? ?????? ???? ?? ?? ?? ??? ???? ?? ??? ????????? ??? ?? ???????? ???, ???? unmanaged-???-???????? ??? ???? ??? DLL ??? ????? ???? ?? ??? ??? ?????? ?? ???, ?? ?????? ?? ???? ?? ?? MEC + +/ IJW asp.NET ??? ????? ???? ??? ?? ?????????? ASP.NET, ??? ??????? ????? ????????? ??? ?? ??? AppDomain reloaded ?? ???? ?? ???? ???? ??? ?? ????? ?? ??? MEC ++ ??? ?? IJW ?? ????????? ???, ???? ??????? ?? ???? ?? ???AppDomainUnloadException?????? ?????:.

????

IJW ?? ??????????? ?? ??? ?? ??? ??? C++ ??????? ?? ????? ?? ???? ?? ???? ???????? DLL ??? ?? ???? ??, ?? ?????? ????? ??????? ?? ??? thunks ????????? ??? ?? ??? ?? ???????? ???? ?? ???? ?? thunks AppDomain DLL ?? ??? ???? ?? ??? ??? ?????? ????? ??? ?????? ???? re-create ?? thunks DLL ??? ?? ??? ???? ???; ??, ?????? ?????? ???? ?????? ?? ??? AppDomain unloads ?? ???? AppDomain ??? DLL ??? ?? ???? ???

???? ?? ????????? ?? ?? ??????? ????????? ??? ?? ???????? ???, ?????? AppDomain ???????? ??? ?? ????? ?? ??? ?????? ????????? ?? ????? ???? ?? ???? ??? ??? AppDomain ??? ?? ??? ??, ?? ?? ??? ?????? ???? ?? ???? ????? ????? ??????? ????? AppDomain ???? ?? ??? ??????? ????

????? ?????????

????? ???????? ?? ?? ???????? ?? ?????? ???????? ???:
  • ?? DLL ?? ??? ??? ?? ???????? ???? ?? ??? ????????? ??? ?? ??? ???????
  • ???? DLL ??? ??? ?? ???????? ???? ?? ??? ????????? ??? ?? ??? ???????

???????? ??? ??????? ?? DLL ????? ???? ?? ??? ????????? ???

?? ??????? ?????? ?? ?? ???? ?? ??? ?? ?? ????? ??????? ?? ????? ?????

1 ???????? ??

ASP.NET ???????, ?? ??? trapping ?? ?? ???? ???? ?? ??? asp.NET ????? ????????? ?? ?????? ???DomainUnload?????? ???????, ?????? ???? ?????? ???? ?? ??? ?? ?????????DomainUnload?????? ??? ???? ?? ???, ????? ????? ?? ???? ????::
  1. ?????Application_StartASP.NET ?? ?????????????????, ????????? ??? ?? ???? ?? ?? AppDomain unloads ???? ?? ??? ??? ??????? ?????
  2. ??????? ????????? ??? ?? ???? Aspnet_wp.exe ??????????
???:?? ?????? ??? asp.NET ????????? ?? ???????? ?? ???????? ???? ?? ??? ???? ??; ??? ?? ??????????? ?? ??? ???-????????? ???? ?????? ???? ?? ???? ??? ??? ??????? ????? ???? (IIS) 6.0 Windows 2003 Server ??? ??? ???????? ??, ?? ????????? ??? ?? ????? ?? ????????? ?? ??? ????????? ?? ???????? ?? ???? ???? Configuing ????????? pools ?? ???? ??? ???? ??????? ?? ??? ????? Microsoft ??? ???? ?? ????:
HTTP://www.Microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/2a231dcb-d786-4b6d-b3ba-bda04061a5e7.mspx?mfr=TRUE
//Register a delegate to be called when the DomainUnload event fires.
protected void Application_Start(Object sender, EventArgs e)
{
     AppDomain.CurrentDomain.DomainUnload+= new EventHandler(ADUnloading);
}

//This method is called when the AppDomain is about to unload.
public void ADUnloading(object s, EventArgs e)
{
     System.Environment.Exit(0);
}
				

?????? 2

????????? ??? ?? ??? ?? ???????? ???? ?? ??? ??? ??????? ????????? ?????? ???? ?? ????????? (?? AppDomain ??????? ??) ?? ????? ?? ????? ???? ?????? ??? ?? ???????????? ????? ????????? ?? marshaled ????????? ??? ?? ??? ??????????? Invocation ?????? (?? ????? ??P/??????? ????) IJW ?? ????? ???

????????? ???? ?? ????????? DLL ?? ???? ?? "???? ???????" ??? ??? ????? ???? ???, ?? ??? ????? ????? ??? ?? ?? ????? ???? ????P/??????? ?????????? ???? ?? ????? ????:
using namespace System::Runtime::InteropServices; // For DllImportAttribute.

namespace ManagedLib
{
        //Managed delegate declaration.
	public __delegate int ManagedFuncDelg();
	public __gc class Class1
	{
		
	public:
		[DllImport("Win32.dll", EntryPoint="callback")]//Assumes that you have a DEF file for the exports.
		static int Mgdcallback(ManagedFuncDelg*);
                //This method is called from the unmanaged DLL.
		static int InternalMethod()
		{
			return 123;
		}

		static ManagedFuncDelg* pd = new ManagedFuncDelg(0, &ManagedLib::Class1::InternalMethod);

                //This method is called by the client application.
		int Func()
		{
			int ret = ManagedLib::Class1::Mgdcallback(ManagedLib::Class1::pd);
			return ret;
		}
	};
}
				

????????? ??? ?? ?? ???? DLL ??? ??? ??????? ???????? ????

?? ???????? ??? ?? ???? ??? (?? ??????? ????????? ???????#pragma unmanaged) ??? ??????? ???????? ??? ???? DLL ??? ?? ?? ???? ?? "???? ???????" ??? ??? ?????? ??????? ?? ??????? ?????? ?? ?? ???? ?? ??? ?? ?? ????? ??????? ?? ????? ????:

1 ???????? ??

?? ????????? ?? ??? ??? ???? ?????? ???? ?????? ?? ?? ????????? ?? ????? ????? ??????? ?? ????? ???? ?? ????P/Invokeon the same DLL, create a wrapper DLL that helps you to marshal a delegate to a function pointer. This exported function returns the address of the delegate that is passed to it. The following sample code is an example of this workaround:
//This is the helper DLL that enables you to marshal the delegate.
//Helper.cpp.
//Helper DLL code.
extern "C" __declspec(dllexport) void* __stdcall FuncInUmDll(void* pv)
{
	return pv;
}
				
The following sample code is for the Managed Extensions for C++ assembly that uses the previous Helper.dll:
#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::InteropServices; // for DllImportAttribute

namespace ManagedLib
{
      //Forward declaration.
      __delegate int delg();
      int nativefunc(int(__stdcall*)());
      [System::Runtime::InteropServices::DllImport("helper.dll", EntryPoint="FuncInUmDll")] int FuncInUmDll(delg*);
      //Managed component.
      public __gc class Class1{
	public:
            static int InternalMethod()
            {
                 return 123;
            }

            //This method is called by the client application.
            int Func()
            {
                 delg* d= new delg(0, &Class1::InternalMethod);
                 int ret = nativefunc((int(__stdcall*)())FuncInUmDll(d));
                 return ret;
            }
      };


      #pragma unmanaged
     //Unmanaged function calling the managed delegate.
     int nativefunc(int(__stdcall*pf)())
     {
            return pf();
     }
}
				

?????? 2

Split your MEC++ assembly into two DLLs, one that contains only managed components and other that contains only unmanaged components, and then useP/Invokeas described in workaround 2 of scenario 1.

??????

Microsoft ?? ?????? ?? ?? Microsoft ??? ?? ?? ?? ?? ???? ?? ???? ??? ???????? ?????????

???? ???????

The problem is more prevalent if the host or client application must unload and reload AppDomains and the application contains transitions from unmanaged code to managed code that use MEC++ and IJW components. ASP.NET is one good example of such a host. In ASP.NET, the events that can trigger an AppDomain unload event can be anything from crossing a memory usage threshold to touching certain files (for example, Machine.config).

??????? ?? ???: ??????? ???? ?? ??? ???

In this example, you have an ASP.NET application that calls a MEC++ component, and this component has a function pointer that points to one of the methods in the MEC++ component. The component passes the function pointer to one of the following:
  • a function that was exported by an unmanaged DLL
  • an unmanaged method in the same DLL
The unmanaged code then makes a callback by using the function pointer that was received from the MEC++ component.

In the following scenarios, it is possible that, before the unmanaged function invokes the function pointer, the original AppDomain in which the DLL was loaded is unloaded. Under such conditions, the unmanaged code still references the original AppDomain and tries to call to managed code in that context.
  • Unmanaged Code to Managed Code Transition Across Two DLLs

    Call an exported unmanaged DLL function from a MEC++ assembly in one of the following ways:
    • by using IJW
    • by usingP/Invoke
    The following sample code uses an unmanaged DLL (Win32.dll) that exports a method.
    //Win32.h.
    #ifdef WIN32_EXPORTS
    #define WIN32_API __declspec(dllexport)
    #else
    #define WIN32_API __declspec(dllimport)
    #endif
    
    //Declaration.
    typedef int (__stdcall *funcptr)(void);
    WIN32_API int __stdcall  callback(funcptr ptr);
    
    //Win32.cpp Implementation.
    //This method is called by the Managed C++ component either by using P/Invoke
    //or by using IJW.
    WIN32_API int __stdcall callback(funcptr ptr)
    {
    	int rtn= ptr();
    	return rtn;
    }
    					
    A managed C++ assembly can call this method through IJW by linking to the Win32.lib and by including the appropriate header file. The following sample code shows how to use the exported method.
    #pragma once
    #include "win32.h"
    #using <mscorlib.dll>
    using namespace System;
    
    namespace ManagedLib
    {
    
    	public __gc class Class1
    	{
    		
    	public:
    		static int InternalMethod()
    		{
    			return 123;
    		}
    
                    //This method is called by the client application.
    		int Func()
    		{
    			int ret = callback((funcptr)Class1::InternalMethod);
    			return ret;
    		}
    	};
    }
    					
  • Unmanaged Code to Managed Code Transition in the Same DLL

    In this scenario, the MEC++ assembly contains unmanaged methods (marked with#pragma unmanaged) that call methods on a MEC++ component in the same DLL, as shown in the following sample code.
    #using <mscorlib.dll>
    using namespace System;
    namespace ManagedLib
    {
          //Forward declararion
          int nativefunc(int(__stdcall*)());
          //Managed component
          public __gc class Class1{
    
    	public:
    
                static int InternalMethod()
                {
                     return 123;
                }
                //This method is called by the client application.
                int Func()
                {
                     int ret = nativefunc((int(__stdcall*)())Class1::InternalMethod);
                     return ret;
                }
          };
    
          #pragma unmanaged
         //Unmanaged function calling the managed delegate.
         int nativefunc(int(__stdcall*pf)())
         {
                return pf();
         }
    }
    					

??????

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:
Application Domains
http://msdn2.microsoft.com/en-us/library/cxk374d9(vs.71).aspx?frame=true

AppDomain Class
http://msdn2.microsoft.com/en-us/library/system.appdomain(vs.71).aspx

AppDomainUnloadedException Class
http://msdn.microsoft.com/en-us/library/system.appdomainunloadedexception(VS.71).aspx

Managed Extensions for C++ Programming
http://msdn2.microsoft.com/en-us/library/aa712574(vs.71).aspx

Creating ASP.NET Web Applications
http://msdn.microsoft.com/en-us/library/aa719794.aspx

Worker Process Isolation Mode
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/34604f82-9175-4d48-b5ea-1e11f19db5f3.mspx?mfr=true

???

???? ID: 309694 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft Visual C++ .NET 2002 Standard Edition
  • Microsoft .NET Framework 1.0
??????: 
kbbug kbijw kbmanaged kbpending kbmt KB309694 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:309694

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com