-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WPF - Handle case where Bitmap in InteropBitmapInfo is null #1003
Conversation
Because the OnPaint arrives on a thread other than the UI thread, it the bitmap rendering process can occur after Dispose() has been called.
} | ||
|
||
public override BitmapSource CreateBitmap() | ||
{ | ||
var stride = Width * BytesPerPixel; | ||
|
||
Bitmap = (InteropBitmap)Imaging.CreateBitmapSourceFromMemorySection(FileMappingHandle, Width, Height, PixelFormat, stride, 0); | ||
if (FileMappingHandle != IntPtr.Zero) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer to eval ==
first, flows better when reading code.
Bitmap = (InteropBitmap)Imaging.CreateBitmapSourceFromMemorySection(FileMappingHandle, Width, Height, PixelFormat, stride, 0); | ||
if (FileMappingHandle == IntPtr.Zero) | ||
{ | ||
ClearBitmap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CreateBitmap()
is usually called after the bitmap has already been cleared. In the scenario your trying to handle hear is it possible for bitmap != null
?
Ideally would fail fast, have a code block like this as the first operation.
// Unable to create bitmap without valid File Handle (Most likely control is being disposed)
if (FileMappingHandle == IntPtr.Zero)
{
return null;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think my update fulfils your request. Let me know.
On 5 May 2015 at 22:54, Alex Maitland notifications@github.com wrote:
In CefSharp.Wpf/Rendering/InteropBitmapInfo.cs
#1003 (comment):} public override BitmapSource CreateBitmap() { var stride = Width * BytesPerPixel;
Bitmap = (InteropBitmap)Imaging.CreateBitmapSourceFromMemorySection(FileMappingHandle, Width, Height, PixelFormat, stride, 0);
if (FileMappingHandle == IntPtr.Zero)
{
ClearBitmap();
CreateBitmap() is usually called after the bitmap has already been
cleared. In the scenario your trying to handle hear is it possible for bitmap
!= null?Ideally would fail fast, have a code block like this as the first
operation.// Unable to create bitmap without valid File Handle (Most likely control is being disposed)if (FileMappingHandle == IntPtr.Zero)
{
return null;
}—
Reply to this email directly or view it on GitHub
https://github.com/cefsharp/CefSharp/pull/1003/files#r29717350.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Close 😄 I'll remove the else (redundant) and move the stride calculation after the if
when it's eventually time to merge this 👍
…the rare case where it's been disposed of and the async OnPaint is called after the file handles have been released Related to #1003
Applied a similar change to |
The OnPaint call arrives on a thread other than the UI thread. This means it is possible that the InvokeRenderAsync method, which is dispatched back to the UI thread, will get called after the Dispose method. In this case, a NullReferenceException is thrown, because FileMappingHandle is no longer set.