Question

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:

  1. Could a Form or Resource which has not been cleaned up be a reason for this exception?
  2. 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?

Was it helpful?

Solution

  1. Yes
  2. I wish (And don't even try to claim the power toys profiler will help you)

You are going to have to review your code. Make sure you are calling Dispose on all Disposable objects including all GDI objects, bitmaps, brushes.

Secondly, if you ever call Font.ToHFont, this call is extremely dangerous because it requires you to p/invoke DeleteObject to clean up after it. (A managed call requires a p/invoke to not leak resources)

My only advice is that, for some reason, most of the time my AGL errors happen close to the source of the problem. I can't remember specific causes. At work, we call this the undocumented "Accelerated Grief Layer (AGL)"

Lastly, I have one more pointed question to make. Are you allocating a lot of bitmaps at once, then freeing them, and finally trying to create managed memory objects, perhaps buffers? Windows Mobile 6 is built off of Windows CE 5.0 and not Windows CE 6.0. Therefore, each process has a 32 MB limit for memory. If you allocate a lot of bitmaps at once, they grow the size of the unmanaged heap. When you dispose of the bitmaps, LocalFree is called and the heap decommit's but does not release the memory and .NET will never see it again. The only way to avoid this, other than avoiding allocating a lot of bitmaps at once, is to allocate bitmaps that are greater than or equal to 96 KB which reserves pages outside of the heap.

Probably doubtful that is your problem, but I'll mention it because it is nearly impossible to track down. I think you would end up with a crash or OutOfMemoryException in that case, however.

Anyhow, I would look into making sure you are not leaking resources or handles, and double check your stream is in the correct format.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top