#1254 Pending exceptions are not discovered for multiple AppDomain

csharp (36)

When using SWIG generated code in systems containing multiple AppDomains (e.g. unittest frameworks or plugin based systems) the exception callbacks are in risk of being lost. The problem is that the unmanaged code does not know which AppDomain to make the callback into and the last used AppDomain is chosen.

The attached example generates a command line example of the problem. Has been tested for .NET 4.0.30319 and SWIG 2.0.7.

Output should be:
Saw 1st exception.
Saw 2nd exception.
Saw 3rd exception.

Instead it is:
Saw 1st exception.
Saw 2nd exception.
Did not catch 3rd exception.

First exception is generated in the default AppDomain and works. Second exception is generated in alternativeAppDomain, which will become the new target of the callbacks. Third exception is generated in the default AppDomain but the exception callback will set the pending exception in the alternativeAppDomain. When returning from the Unmanaged code the pending exceptions are checked in the default AppDomain, which does not have any indications of pending exceptions.

The consequence is that exceptions in general are uncaught unless they are on the last added AppDomain.


  • SWIG interface definition showing the problem.