Skip to content

[src] Transient strings must always be disposed.#24894

Merged
rolfbjarne merged 3 commits intomainfrom
dev/rolf/using-transient-strings
Mar 16, 2026
Merged

[src] Transient strings must always be disposed.#24894
rolfbjarne merged 3 commits intomainfrom
dev/rolf/using-transient-strings

Conversation

@rolfbjarne
Copy link
Copy Markdown
Member

Fix a couple of places where transient strings weren't disposed.

And to make sure this doesn't happen again, add a new Roslyn analyzer (RBI0042) that
reports an error when transient strings are declared as local variables without the
'using' keyword.

Fix a couple of places where transient strings weren't disposed.

And to make sure this doesn't happen again, add a new Roslyn analyzer (RBI0042) that
reports an error when transient strings are declared as local variables without the
'using' keyword.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes missing disposal of transient interop string wrappers in the runtime, and adds a new Roslyn analyzer (RBI0042) in rgen to prevent future occurrences by enforcing using on local declarations of transient disposable types.

Changes:

  • Fix undisposed TransientString/TransientCFString locals by converting them to using var declarations.
  • Add analyzer TransientDisposableAnalyzer that reports RBI0042 for transient locals missing using.
  • Add tests + diagnostic resources/release notes for RBI0042.

Reviewed changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/rgen/Microsoft.Macios.Bindings.Analyzer.Tests/TransientDisposableAnalyzerTests.cs Adds analyzer test coverage for RBI0042 scenarios.
src/rgen/Microsoft.Macios.Bindings.Analyzer/TransientDisposableAnalyzer.cs Introduces the new RBI0042 analyzer enforcing using for transient locals.
src/rgen/Microsoft.Macios.Bindings.Analyzer/RgenDiagnostics.cs Registers the RBI0042 DiagnosticDescriptor.
src/rgen/Microsoft.Macios.Bindings.Analyzer/Resources.resx Adds localized strings for RBI0042 title/message/description.
src/rgen/Microsoft.Macios.Bindings.Analyzer/Resources.Designer.cs Updates generated resource accessors for RBI0042.
src/rgen/Microsoft.Macios.Bindings.Analyzer/AnalyzerReleases.Unshipped.md Documents the newly added analyzer rule RBI0042.
src/ObjCRuntime/Protocol.cs Disposes TransientString via using var before calling objc_getProtocol.
src/CoreGraphics/CGFont.cs Disposes TransientCFString via using var before calling CGFontCreateWithFontName.
Files not reviewed (1)
  • src/rgen/Microsoft.Macios.Bindings.Analyzer/Resources.Designer.cs: Language not supported

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +566 to +569
<value>Transient types (TransientString, TransientCFString, TransientCFObject) allocate native memory and must be disposed. Use 'using var' to ensure proper cleanup.</value>
</data>
<data name="RBI0042MessageFormat" xml:space="preserve">
<value>Variable '{0}' of type '{1}' must be declared with 'using var' to ensure proper disposal of native resources</value>
Comment on lines +20 to +54
static readonly ImmutableHashSet<string> transientTypeNames = ImmutableHashSet.Create (
"TransientString",
"TransientCFString",
"TransientCFObject"
);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create (RBI0042);

public override void Initialize (AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis (GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution ();
context.RegisterSyntaxNodeAction (AnalyzeNode, SyntaxKind.LocalDeclarationStatement);
}

void AnalyzeNode (SyntaxNodeAnalysisContext context)
{
if (context.Node is not LocalDeclarationStatementSyntax localDeclaration)
return;

// If the declaration already has 'using', it's fine.
if (localDeclaration.UsingKeyword != default)
return;

foreach (var variable in localDeclaration.Declaration.Variables) {
var symbol = context.SemanticModel.GetDeclaredSymbol (variable);
if (symbol is not ILocalSymbol localSymbol)
continue;

var typeName = localSymbol.Type.Name;
if (!transientTypeNames.Contains (typeName))
continue;

var diagnostic = Diagnostic.Create (RBI0042, variable.GetLocation (), variable.Identifier.Text, typeName);
context.ReportDiagnostic (diagnostic);
Comment on lines +18 to +70
// TransientString without using
yield return [
"""
using System;

struct TransientString : IDisposable {
public void Dispose () { }
}

class Test
{
void Method ()
{
var s = new TransientString ();
}
}
"""];

// TransientCFString without using
yield return [
"""
using System;

ref struct TransientCFString {
public void Dispose () { }
}

class Test
{
void Method ()
{
TransientCFString s = new TransientCFString ();
}
}
"""];

// TransientCFObject without using
yield return [
"""
using System;

ref struct TransientCFObject {
public void Dispose () { }
}

class Test
{
void Method ()
{
var obj = new TransientCFObject ();
}
}
"""];
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

- Fix diagnostic text to say 'using' keyword instead of 'using var',
  since using declarations and using statements are both acceptable.
- Match transient types by fully-qualified name (ObjCRuntime.TransientString
  etc.) instead of simple name to avoid false positives on user-defined
  types with the same name.
- Move test type definitions into ObjCRuntime namespace to validate
  fully-qualified matching.
- Add negative test for user-defined TransientString outside ObjCRuntime
  namespace to ensure no false positives.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@rolfbjarne rolfbjarne enabled auto-merge (squash) March 16, 2026 18:09
@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [CI Build #8fae065] Build passed (Build packages) ✅

Pipeline on Agent
Hash: 8fae0657a83d537bad0f2950108940ba37e10968 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [PR Build #8fae065] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: 8fae0657a83d537bad0f2950108940ba37e10968 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)

✅ API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: 8fae0657a83d537bad0f2950108940ba37e10968 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [CI Build #8fae065] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: 8fae0657a83d537bad0f2950108940ba37e10968 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

🚀 [CI Build #8fae065] Test results 🚀

Test results

✅ All tests passed on VSTS: test results.

🎉 All 156 tests passed 🎉

Tests counts

✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 6 tests passed. Html Report (VSDrops) Download
✅ linker: All 44 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 11 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 15 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 12 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 11 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

✅ Tests on macOS Monterey (12): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Ventura (13): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sequoia (15): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Tahoe (26): All 5 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: 8fae0657a83d537bad0f2950108940ba37e10968 [PR build]

@rolfbjarne rolfbjarne merged commit 809b19e into main Mar 16, 2026
48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants