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

Implement CriticalFinalizerObject #881

Closed
amaitland opened this issue Mar 16, 2015 · 14 comments
Closed

Implement CriticalFinalizerObject #881

amaitland opened this issue Mar 16, 2015 · 14 comments

Comments

@amaitland
Copy link
Member

So I'm thinking some of our classes may need to implement CriticalFinalizerObject to grantee that their resources are disposed of correctly.

https://msdn.microsoft.com/en-us/library/system.runtime.constrainedexecution.criticalfinalizerobject%28v=vs.110%29.aspx

Implementing IDisposable on a CriticalFinalizerObject-derived type is tricky because void Dispose(bool disposing) is virtual, yet it must run within a Constrained Execution Region. This may require an explicit call to RuntimeHelpers.PrepareMethod.

http://www.codeproject.com/Articles/29534/IDisposable-What-Your-Mother-Never-Told-You-About

Anyone have past experience implementing CriticalFinalizerObject? Is it worth doing?

@amaitland
Copy link
Member Author

I'm wondering if this might resolve issues like #800 (Cef Shutdown is fine under normal circumstances, crashes when rebooting/shutting down windows).

@rassilon
Copy link
Contributor

Interesting. I sure appreciate the codeproject URL, I ended up using that pattern for wrapping power change notifications (AC <-> DC transitions, battery level, etc..) for my project.

I'm not quite sure how this would apply to CefSharp yet though.

Bill

@amaitland
Copy link
Member Author

I'm not quite sure how this would apply to CefSharp yet though.

It was just an idea, something I stumbled across. Searching for ideas on how to make sure the managed resources are always disposed. When shutting down/restarting when the WPF app is open causes an error. Closing the app normal is fine. Likely due to the OSR, which is the major different between WinForms and WPF. (Problem doesn't happen on WinForms).

So if you've got any thoughts/suggestions?

@amaitland
Copy link
Member Author

Potentially look at using a SafeHandle for the filehandle used by WPF

@rassilon
Copy link
Contributor

rassilon commented Jun 9, 2015

Maybe I'm being dense, but what filehandle is used by WPF?

@amaitland
Copy link
Member Author

Really just a note to myself to look into something further. Need to make sure those unmanaged resources we allocate get handled cleanly. When abruptly exiting e.g. Environment.Exit(), I think there's a chance that some of the cleanup code doesn't get executed.

https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Core/Internals/RenderClientAdapter.h#L146

https://msdn.microsoft.com/en-us/library/windows/desktop/aa366537%28v=vs.85%29.aspx

CreateFileMapping creates a file mapping object of a specified size that is backed by the system paging file instead of by a file in the file system.

@amaitland
Copy link
Member Author

If hFile is INVALID_HANDLE_VALUE, the calling process must also specify a size for the file mapping object in the dwMaximumSizeHigh and dwMaximumSizeLow parameters. In this scenario, CreateFileMapping creates a file mapping object of a specified size that is backed by the system paging file instead of by a file in the file system.

I should have included the first part for clarity.

@amaitland
Copy link
Member Author

https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.safehandle%28v=vs.100%29.aspx

Has a SafeHandleDemo that shows CloseHandle, which is used. Also referenced UnmapViewOfFile which is also used. So probably need one SafeHandler for each handle.

Ideally keep this all in c++ and use native calls if possible.

@amaitland
Copy link
Member Author

@rassilon Have you ever implemented a SafeHandle? Current cleanup code looks like https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Core/Internals/RenderClientAdapter.h#L181

@amaitland
Copy link
Member Author

@amaitland
Copy link
Member Author

@amaitland
Copy link
Member Author

Resolved with #2237

Have no switched to a totally managed solution using build in classes that implement CriticalFinalizerObject

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants