I'm confused about the safety (sanity?) of PWSTR.From(string?):
|
public unsafe static PWSTR From(string? str) |
|
{ |
|
if (str == null) |
|
return Null; |
|
|
|
fixed (char* chars = str) |
|
{ |
|
return new PWSTR(chars); |
|
} |
|
} |
In particular the fixed block:
|
fixed (char* chars = str) |
|
{ |
|
return new PWSTR(chars); |
|
} |
The fixed statement:
prevents the garbage collector from relocating a moveable variable and declares a pointer to that variable.
but that variable is only prevented from relocating within the fixed statement.
Yet here you retain the pointer value and reuse outside the fixed statement.
How does this not "interact poorly" with the GC?
var s = PWSTR.From("foo");
// GC happens, causing `foo` to move in memory
// use `s`. "boom"?
(It's probably "fine" for string constants as those are often interned, but for the output of StringBuilder.ToString()…?)
Am I missing something, or is this as horrifying as I think it is?
I'm confused about the safety (sanity?) of
PWSTR.From(string?):DirectNAot/DirectN/Partials/PWSTR.partial..cs
Lines 24 to 33 in 7e1bbe4
In particular the
fixedblock:DirectNAot/DirectN/Partials/PWSTR.partial..cs
Lines 29 to 32 in 7e1bbe4
The
fixedstatement:but that variable is only prevented from relocating within the
fixedstatement.Yet here you retain the pointer value and reuse outside the
fixedstatement.How does this not "interact poorly" with the GC?
(It's probably "fine" for string constants as those are often interned, but for the output of
StringBuilder.ToString()…?)Am I missing something, or is this as horrifying as I think it is?