Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

System.Drawing - Simplify Pen.get_DashPattern P/Invoke code - #23958

Merged
mellinoe merged 5 commits into
dotnet:masterfrom
qmfrederik:drawing/pen-pinvoke
Sep 11, 2017
Merged

mellinoe merged 5 commits into
dotnet:masterfrom
qmfrederik:drawing/pen-pinvoke

Conversation

@qmfrederik

Copy link
Copy Markdown

Let the runtime marshal the float arrays instead of manually processing unmanaged memory.
Fixes libgdiplus compatibility.

@qmfrederik qmfrederik changed the title Simplify Pen.get_DashPattern P/Invoke code System.Drawing - Simplify Pen.get_DashPattern P/Invoke code Sep 11, 2017
@qmfrederik

Copy link
Copy Markdown
Author

/cc @mellinoe

else if (DashStyle == DashStyle.Solid && !this.dashStyleWasChanged)
{
Marshal.FreeHGlobal(buf);
// Most likely we're replicating an existing System.Drawing bug here, it doesn't make much sense tó

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an incorrect "ó" character on this line.

private bool _immutable;

// Tracks whether the dash style has been changed to something else than Solid during the lifetime of this object.
private bool dashStyleWasChanged;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefix this with an underscore. Also, could you rename this to something like "_dashStyleIsNotSolid" to better reflect the name.

An aside: I'm okay with doing this as an attempt to match behavior better, but it's not perfectly robust. It is legal to change the native properties of the Pen outside of the Pen class (through a PInvoke or otherwise), and this cached state will become stale. This is a common problem in the library, and not specific to this at all. I just wanted to point it out.


if (value != DashStyle.Solid)
{
this.dashStyleWasChanged = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be set back to false if the value is Solid? Or rather:

_dashStyleIsNotSolid = value != DashStyle.Solid;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DashStyle_Custom unit test best explains the behavior:
https://github.com/dotnet/corefx/pull/23797/files#diff-7e1bf1ca3f411d193feac1a3697b2db1R304

  • By default, the dash style is solid. If you execute get_DashPattern, you'll get an OutOfMemoryException.
  • If you change the dash style to a custom dash style, you can get/set DashPattern without issue
  • If you afterwards change the dash style back to Solid, calling get_DashPattern will not cause an OutOfMemoryException.

So the flag really tracks whether the dash patter is or has been different from Solid at any point or time. dashStyleIsNotSolid doesn't feel right because it's present tense; dashStyleIsOrWasNotSolid is the best I can come up with, let me know if you have a better idea for the name :).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, sounds like a reasonable description of what the field is tracking. Very weird behavior from GDI+ there...

@mellinoe

Copy link
Copy Markdown
Contributor

Looks like an okay change to enable better compatibility, but I left a few comments to be fixed.

@mellinoe

Copy link
Copy Markdown
Contributor

Test UWP CoreCLR x64 Debug Build (I don't really understand what went wrong)

@mellinoe
mellinoe merged commit 839f044 into dotnet:master Sep 11, 2017
@karelz karelz added this to the 2.1.0 milestone Oct 11, 2017
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…nvoke

System.Drawing - Simplify Pen.get_DashPattern P/Invoke code

Commit migrated from dotnet/corefx@839f044
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants