GDI+ LockBits()/UnLockBits() Exception

偶尔善良 提交于 2019-12-23 12:29:04

问题


My program is written in C# and manipulates with bitmaps on low level. Everything works fine, but sometimes (very rarely, but stable) exception "Generic GDI+ exception" occurs, and it is very hard to reproduce such situations.

Exception happens on functions LockBits() and UnLockBits(). It contains error code "-2147467259". On the language of GDI+ it would be method GdipBitmapLockBits and return code 7 or 1.

What kind of reasons may cause such situations?

Any answers greatly appreciate.


回答1:


GDI+ objects are not thread-safe, and using them from multiple threads without synchronization could certainly cause crashes. For completeness, I want to add that it's possible to free the memory backing a GDI+ bitmap before freeing the bitmap, and in such cases you'd get an invalid access when attempting to use the bitmap. This can only happen if you allocate the pixel data yourself and use a constructor that takes a pointer to the data, such as this one: http://msdn.microsoft.com/en-us/library/zy1a2d14.aspx




回答2:


I've investigated this problem and it seems that it might be connected with threads. I don't know for sure what really causes that issues and have no proofs, but after setting up locks throughout the code the problem seems to have gone.



来源:https://stackoverflow.com/questions/7996484/gdi-lockbits-unlockbits-exception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!