I get a TargetInvocationException randomly when loading Resources in my .NET Compact Framework 3.5 project (running on Windows Mobile 6). They look similar to this stack trace:
FATAL 2012-11-13 14:17:00,657 [23768895] TargetInvocationException - mobileX.MIP.Post.Presentation.Program
System.Reflection.TargetInvocationException: TargetInvocationException ---> System.Exception: Exception
at Microsoft.AGL.Common.MISC.HandleAr(PAL_ERROR ar)
at System.Drawing.Bitmap._InitFromMemoryStream(MemoryStream mstream)
at System.Drawing.Bitmap..ctor(Stream stream)
at System.Reflection.RuntimeConstructorInfo.InternalInvoke(RuntimeConstructorInfo rtci, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
at System.Resources.ResourceReader.CreateResource(Type objType, Type[] ctorParamTypes, Object[] ctorParameters)
at System.Resources.ResourceReader.LoadBitmap(Int32 typeIndex)
at System.Resources.ResourceReader.LoadObjectV2(Int32 pos, ResourceTypeCode& typeCode)
at System.Resources.ResourceReader.LoadObject(Int32 pos, ResourceTypeCode& typeCode)
at System.Resources.RuntimeResourceSet.GetObject(String key, Boolean ignoreCase)
at System.Resources.ResourceManager.GetObject(String name, CultureInfo culture)
My guess for the reason of this exception is that there's some unmanaged resource which I forgot to clean up. However, I've got a lot of Forms and Resources in the project.
So here are my questions:
- Could a Form or Resource which has not been cleaned up be a reason for this exception?
- How can I trace the exact Form or Resource which wastes my memory?
Regarding 2: I already profiled my application with the CLR Profiler from the .NET Compact Framework Power Toys 3.5. A lot of memory goes to "NATIVE FUNCTION" / System.Windows.Forms.Control::_InternalWnProc Microsoft.AGL.Common.PAL_ERROR (Microsoft.AGL.Forms.WM int32 int32)
. However, I cannot see where these resources are used. How can I find out?