Skip to content

Conversation

@stephen-hawley
Copy link
Contributor

fix all pinvokes in the Network package that use strings

Guiding principle for this PR: be unobtrusive

This meant making the changes as minor as possible

  • if the pinvoke gets called once and doesn't return string, change the string args to IntPtr and inline the string -> TransientString conversion.
  • if the pinvoke is used in an expression method body (public int SomeFunc (string s) => SomePInvoke (s);), then I wrote a wrapper function for it that keeps the original signature
  • if the function returns a string, I renamed the pinvoke with Ptr or _ptr on the end, changed it to return IntPtr, then wrote an adapter that uses calls is, allocates a new string and frees the original using TransientString.ToStringAndFree

@stephen-hawley stephen-hawley added the dotnet An issue or pull request related to .NET (6) label Dec 15, 2022
@vs-mobiletools-engineering-service2

This comment has been minimized.

Comment on lines 77 to 78
if (ptr != IntPtr.Zero)
free (ptr);
Copy link
Member

Choose a reason for hiding this comment

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

You can call Marshal.FreeHGlobal here (and no need for the IntPtr.Zero check).

Unfortunately the documentation doesn't say what Marshal.FreeHGlobal will do on non-Windows platforms, but according to the source code, Marshal.FreeHGlobal will call NativeMemory.Free, and NativeMemory.Free is documented to call free: "This method is a thin wrapper over the C free API."

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ API diff for current PR / commit

Legacy Xamarin (No breaking changes)
  • iOS (no change detected)
  • tvOS (no change detected)
  • watchOS (no change detected)
  • macOS (no change detected)
NET (empty diffs)
  • iOS: (empty diff detected)
  • tvOS: (empty diff detected)
  • MacCatalyst: (empty diff detected)
  • macOS: (empty diff detected)

✅ API diff vs stable

Legacy Xamarin (No breaking changes)
.NET (No breaking changes)
Legacy Xamarin (stable) vs .NET

✅ Generator diff

Generator diff is empty

Pipeline on Agent
Hash: bb5f2047f3ff5268ba1c95d64c39274078d1c29c [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

📚 [PR Build] Artifacts 📚

Artifacts were not provided.

Pipeline on Agent XAMBOT-1164.Monterey'
Hash: bb5f2047f3ff5268ba1c95d64c39274078d1c29c [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [PR Build] Tests on macOS M1 - Mac Big Sur (11.5) passed 💻

All tests on macOS M1 - Mac Big Sur (11.5) passed.

Pipeline on Agent
Hash: bb5f2047f3ff5268ba1c95d64c39274078d1c29c [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

🚀 [CI Build] Test results 🚀

Test results

✅ All tests passed on VSTS: simulator tests.

🎉 All 223 tests passed 🎉

Tests counts

✅ bcl: All 69 tests passed. Html Report (VSDrops) Download
✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests: All 1 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 7 tests passed. Html Report (VSDrops) Download
✅ framework: All 8 tests passed. Html Report (VSDrops) Download
✅ generator: All 2 tests passed. Html Report (VSDrops) Download
✅ interdependent_binding_projects: All 7 tests passed. Html Report (VSDrops) Download
✅ install_source: All 1 tests passed. Html Report (VSDrops) Download
✅ introspection: All 8 tests passed. Html Report (VSDrops) Download
✅ linker: All 65 tests passed. Html Report (VSDrops) Download
✅ mac_binding_project: All 1 tests passed. Html Report (VSDrops) Download
✅ mmp: All 2 tests passed. Html Report (VSDrops) Download
✅ mononative: All 12 tests passed. Html Report (VSDrops) Download
✅ monotouch: All 23 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ mtouch: All 1 tests passed. Html Report (VSDrops) Download
✅ xammac: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 8 tests passed. Html Report (VSDrops) Download
✅ xtro: All 2 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: bb5f2047f3ff5268ba1c95d64c39274078d1c29c [PR build]

@stephen-hawley stephen-hawley merged commit 8e0bb5d into dotnet:main Jan 3, 2023
@stephen-hawley stephen-hawley deleted the network-pinvoke-string branch January 3, 2023 20:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dotnet An issue or pull request related to .NET (6)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants