6

I started digging into new C#/.net core features called Span and Memory and so far they look very good. However, when I encountered MemoryMarshal.AsMemory method I found out the following interesting use case:

const string source1 = "immutable string";
const string source2 = "immutable string";

var memory = MemoryMarshal.AsMemory(source1.AsMemory());

ref char first = ref memory.Span[0];
first = 'X';

Console.WriteLine(source1);
Console.WriteLine(source2);

Output in both cases is Xmmutable string (tested on Windows 10 x64, .net471 and .netcore2.1). And as far as I can see any string that is interned can now be modified in one place and then all references to that string will use updated value.

Is there any way to prevent such behavior? And is it possible to "unintern" string?

2
  • 1
    The documentation states: "This method must be used with extreme caution. ReadOnlyMemory<T> is used to represent immutable data and other memory that is not meant to be written to. Memory<T> instances created by this method should not be written to. The purpose of this method is to allow variables typed as Memory<T> but only used for reading to store a ReadOnlyMemory<T>." so I take it this is expected behavior. You can shoot yourself in the foot with the and the recommendation is to not do that. Nov 6, 2018 at 7:12
  • 3
    This is not new, modifying a string is a standard pinvoke bug. It is only immutable by contract, not by design. A hilarious bug in 3.5 was modifying String.Empty :) Nov 6, 2018 at 10:28

1 Answer 1

2

This is just the way it works

MemoryMarshal.AsMemory(ReadOnlyMemory) Method

Creates a Memory instance from a ReadOnlyMemory.

Returns - Memory<T> A memory block that represetns the same memory as the ReadOnlyMemory .

Remarks

  • This method must be used with extreme caution. ReadOnlyMemory is used to represent immutable data and other memory that is not meant to be written to. Memory instances created by this method should not be written to. The purpose of this method is to allow variables typed as Memory but only used for reading to store a ReadOnlyMemory.

More things you shouldn't do

private const string source1 = "immutable string1";

private const string source2 = "immutable string2";

public unsafe static void Main()
{
   fixed(char* c = source1)
   {
      *c = 'f';
   }
   Console.WriteLine(source1);
   Console.WriteLine(source2);
   Console.ReadKey();
}

Output

fmmutable string1
immutable string2
2
  • 1
    I was aware of "safe" way and now I know "unsafe" way how to shoot myself in the foot. Thanks :) And I assume there's no way to prevent a .dll from modifying strings via MemoryMarshall. Right?
    – Alexander
    Nov 6, 2018 at 19:12
  • @Alexander no, only reallocate it, or copy it
    – TheGeneral
    Nov 6, 2018 at 21:53

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.