-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Finish migrating System.Drawing.Common to ComWrappers #54884
Conversation
This resolves all ILLink warnings in System.Drawing.Common, making it work in a trimmed application.
Tagging subscribers to this area: @safern, @tarekgh Issue DetailsThis resolves all ILLink warnings in System.Drawing.Common, making it work in a trimmed application. cc @kant2002
|
src/libraries/System.Drawing.Common/src/System/Drawing/DrawingComWrappers.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Drawing.Common/src/System/Drawing/Internal/GPStream.COMWrappers.cs
Show resolved
Hide resolved
src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/Metafile.Windows.COMWrappers.cs
Outdated
Show resolved
Hide resolved
src/libraries/Common/src/Interop/Windows/Ole32/Interop.IStream.COMWrappers.cs
Outdated
Show resolved
Hide resolved
src/libraries/Common/src/Interop/Windows/Ole32/Interop.IStream.COMWrappers.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Drawing.Common/src/System/Drawing/Bitmap.Windows.COMWrappers.cs
Outdated
Show resolved
Hide resolved
Thanks for the feedback. I believe I have addressed it all with the latest changes. PTAL. |
I believe this change is ready - any further feedback? |
Interop.HRESULT hr = (Interop.HRESULT)WriteToStream(pstm, b, read, &written); | ||
if (hr != Interop.HRESULT.S_OK) | ||
{ | ||
return hr; |
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.
do we need to return the buffer in this case?
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've been told by @GrabYourPitchforks that when an error occurs, it is usually best to not return buffers to the pool, since they could have corrupt/incomplete data in them.
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.
It depends on how common you expect the errors can be. We do not bother to return the buffer to the pool for very rare errors, like out-of-memory exceptions.
The error here may not be that rare and it is not perf critical path, so it would be reasonable to return the buffer in finally here. This is similar to Stream.CopyTo
where we do return in finally as well.
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.
Note - the existing, "built-in COM" code doesn't return the buffer if an exception happens (which my understanding is that a COMException will occur since IStream defines Write
as returning void
, so the marshalling code will check the HR and throw):
runtime/src/libraries/System.Drawing.Common/src/System/Drawing/Internal/GPStream.cs
Lines 63 to 89 in be80f67
byte[] buffer = ArrayPool<byte>.Shared.Rent(4096); | |
ulong remaining = cb; | |
ulong totalWritten = 0; | |
ulong totalRead = 0; | |
fixed (byte* b = buffer) | |
{ | |
while (remaining > 0) | |
{ | |
uint read = remaining < (ulong)buffer.Length ? (uint)remaining : (uint)buffer.Length; | |
Read(b, read, &read); | |
remaining -= read; | |
totalRead += read; | |
if (read == 0) | |
{ | |
break; | |
} | |
uint written; | |
pstm.Write(b, read, &written); | |
totalWritten += written; | |
} | |
} | |
ArrayPool<byte>.Shared.Return(buffer); |
@jkotas - do you think it is that common for an error to occur here?
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.
Probably not that common.
It is user supplied stream, so you can certainly constructs cases where the errors are common.
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 guess my preference is to leave it then, since that is the existing behavior and we haven't heard complaints.
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.
LGTM
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.
LGTM
As of dotnet/runtime#54884 no longer needed.
As of dotnet/runtime#54884 no longer needed.
After dotnet#54884 these tests started failing on Mono on Windows, see dotnet#55655.
This resolves all ILLink warnings in System.Drawing.Common, making it work in a trimmed application.
cc @kant2002