microsoft / microsoft/CsWin32

Pointers to native classes should _not_ be dereferenced by C#

Open
#1,115 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

blocked bug metadata gem partner
Dominant language
C#
Stars
2.5k
Forks
124
Avg merge
1d 3h
Merged PRs (30d)
9

Description

Actual behavior

The GdipDeleteRegion method gets a friendly overload that takes ref GpRegion instead of GpRegion*.
This might be fine, except that GpRegion* should never be dereferenced, since it's a pointer to a class on the C++ side. The struct defined in the metadata has an IntPtr field, but unlike HWND where that is just a copyable opaque value, GpRegion (and other GDI+ structs) actually represent C++ classes, and the 'field' in these structs is misleading. What would really be at that memory location is the first field in the C++ class, so copying around the struct in C# would lead to data corruption as anyone that re-takes the pointer to the struct would expect the full class fields where the struct is, and they wouldn't be there.

Expected behavior

Any extern methods that take a pointer to a native object should not have friendly overloads that replace the pointer with ref or other modifiers.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing generation of the GdipDeleteRegion extern method and the metadata that describes GpRegion*. Check how pointer parameters are converted into friendly C# overloads. Done means native-object pointers no longer receive ref or other dereferencing overloads, with generated signatures or tests confirming the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.