dotnet / dotnet/docs

No mention on memory handling of strings in return positions

Open
#29,207 2 comments 0 reactions 0 assignees View on GitHub
dotnet-framework/svc
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
19h 10m
Merged PRs (30d)
268

Description

I'm writing a code that calls native libraries written in Rust and I'm missing a section on how is the memory that's backing a string content handled in case the string is used in *return* position.

An example:

```cs
[DllImport("target/release/libraw_csharp")]
[return: MarshalAs(UnmanagedType.LPUTF8Str)]
static extern string reverse([MarshalAs(UnmanagedType.LPUTF8Str)] string text);

var quote = "« All that we see or seem is but a dream within a dream. » EAP";
var p = reverse(quote);
```

My question is the following: will .NET runtime take ownership of the returned string (`p` here) and free it when GC runs?

Thanks for your time!

---
#### Document Details

⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*

* ID: 25d350b8-a514-6c13-a091-0952a8fc0610
* Version Independent ID: 3ee9bbfd-2060-d97f-c2f1-12867899034a
* Content: [Default Marshalling for Strings - .NET Framework](https://docs.microsoft.com/en-us/dotnet/framework/interop/default-marshalling-for-strings)
* Content Source: [docs/framework/interop/default-marshalling-for-strings.md](https://github.com/dotnet/docs/blob/main/docs/framework/interop/default-marshalling-for-strings.md)
* Product: **dotnet-framework**
* GitHub Login: @gewarren
* Microsoft Alias: **dotnetcontent**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.