This project is read-only.

Native Exception in AlphaImage Draw method

Jun 17, 2008 at 12:11 PM


Thanks for sharing the controls!

I've found a problem in the controls which causes the test application to crash. Here’s how to reproduce it:
1) Run the AlphaMobileControls.TestApp, and click the "Alpha Form" button.
2) While you’re in the Alpha Form, open another window, for instance the File Explorer: [see pic].
3) Now close the File Explorer (or other window) to return to the AlphaMobileControls.TestApp, and you’ll get an exception:
[see pic].

I believe the problem might be related to releasing \ not releasing the Bitmaps between managed and unmanaged code.
Any help or insight on this issue will be much appreciated.

Jun 20, 2008 at 3:03 PM
Hi, these controls are very interessant, thanks !

I have the same problem. A native exception which crash my application.
As somebody an issue ti this problem ?


Ps: sorry for my english, I'm a french developper.
Jun 23, 2008 at 6:06 PM
The problem is because a managed array is passed to the COM object. Once the garbage collector decides to free/move this array all hell break loose.
Here's a fix:
In IImagingFactory.cs, replace:

// We need the MarshalAs attribute here to keep COM interop from sending the buffer down as a Safe Array.
uint CreateImageFromBuffer([MarshalAs(UnmanagedType.LPArray)] byte[] buffer, uint size, BufferDisposalFlag disposalFlag, out IImage image);


uint CreateImageFromBuffer(IntPtr buffer, uint size, BufferDisposalFlag disposalFlag, out IImage image);

In AlphaImage.cs add this in the top of the file:

using System.Runtime.InteropServices;

And replace:

factory.CreateImageFromBuffer(pbBuf, cbBuf, BufferDisposalFlag.BufferDisposalFlagNone, out alphaImage._image);


// Copy stream data into an unmanaged global buffer that will be freed by the factory once image is decoded
// note that we cannot pass pbBuf directly since it might be moved around or freed by the gc.
IntPtr p = Marshal.AllocHGlobal((int)cbBuf);
Marshal.Copy(pbBuf, 0, p, (int)cbBuf);
factory.CreateImageFromBuffer(p, cbBuf, BufferDisposalFlag.BufferDisposalFlagGlobalFree, out alphaImage._image);

Nov 29, 2008 at 5:15 AM

I just wanted to thank you for sharing these wonderfull controls and giving a fix to this problem.

Best regards
Jan 29, 2009 at 1:09 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Jan 18, 2010 at 6:55 PM

Thanks a lot!

This works and you saved my day!