Skip to content
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

Merged
merged 3 commits into from
May 6, 2015
Merged

WPF - Handle case where Bitmap in InteropBitmapInfo is null #1003

merged 3 commits into from
May 6, 2015

Conversation

jamespearce2006
Copy link
Contributor

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.

Because the OnPaint arrives on a thread other than the UI thread, it the
bitmap rendering process can occur after Dispose() has been called.
@jamespearce2006 jamespearce2006 changed the title WPF - Handle case where Bitmap in WpfBitmapInfo is IntPtr.Zero WPF - Handle case where Bitmap in WpfBitmapInfo is null May 5, 2015
}

public override BitmapSource CreateBitmap()
{
var stride = Width * BytesPerPixel;

Bitmap = (InteropBitmap)Imaging.CreateBitmapSourceFromMemorySection(FileMappingHandle, Width, Height, PixelFormat, stride, 0);
if (FileMappingHandle != IntPtr.Zero)
Copy link
Member

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();
Copy link
Member

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;
}

Copy link
Contributor Author

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.

Copy link
Member

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 👍

@amaitland amaitland added this to the 39.0.2 milestone May 5, 2015
@amaitland amaitland merged commit 6fa4b47 into cefsharp:master May 6, 2015
amaitland added a commit that referenced this pull request May 6, 2015
…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
@amaitland
Copy link
Member

Applied a similar change to WritableBitmapInfo see 561507b

@amaitland amaitland changed the title WPF - Handle case where Bitmap in WpfBitmapInfo is null WPF - Handle case where Bitmap in InteropBitmapInfo is null May 6, 2015
@amaitland amaitland added the wpf label May 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants