Anton K
Anton K

Reputation: 561

GDI+ LockBits()/UnLockBits() Exception

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.

Upvotes: 6

Views: 3102

Answers (2)

Esme Povirk
Esme Povirk

Reputation: 3164

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

Upvotes: 4

Anton K
Anton K

Reputation: 561

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.

Upvotes: 1

Related Questions