|
-
July 2nd, 2009, 06:13 AM
#1
Access violation exception when Form gets focus back after a while
Hi all,
I am developing a camera application, using an Axis Media Control object. This object has a method GetCurrentImage() which can fill a buffer with bitmap data containing the current image of the shown MPEG4-stream. I wrote code to capture this image and display it into a PictureBox. This works great.
However, when I unfocus the Form (in runtime ofcourse) and select it quite a while later, then I get an error that the application caused a problem and had to close.
Below you'll find the exact trace message and the exception:
---
Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Source="System.Drawing"
StackTrace:
at System.Drawing.SafeNativeMethods.Gdip.GdipDrawImageRectI(HandleRef graphics, HandleRef image, Int32 x, Int32 y, Int32 width, Int32 height)
at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)
at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect)
at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsApplication1.Program.Main() in F:\Cameras\AxisTest\AxisTest\WindowsApplication1\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
---
Here is my code:
---
Object Buf = null;
int t = 0;
IntPtr pBuf;
BitmapData imgData = new BitmapData();
axAxisMediaControl1.GetCurrentImage(1, out Buf, out t);
pBuf = Marshal.UnsafeAddrOfPinnedArrayElement((Array)Buf, 0);
imgData.Height = Marshal.ReadInt32(pBuf, 8);
imgData.Width = Marshal.ReadInt32(pBuf, 4);
imgData.Stride = (imgData.Width * 3);
imgData.Scan0 = (IntPtr)((int)pBuf + 40);
imgData.PixelFormat = PixelFormat.Format24bppRgb;
Bitmap test = new Bitmap(imgData.Width, imgData.Height, imgData.Stride, imgData.PixelFormat, imgData.Scan0);
pictureBox1.Image = test;
---
I think it's the garbage collector or Windows Memory Management that somehow thinks the memory isn't used anymore. I tried to make the Bitmap and the BitmapData global variables instead of local function variables, to prevent them from begin garbage-collected, but that didn't help.
Any help on this one would be great, because I dont know what could be the reason and how to solve it... Thank you!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|