Closed Louis-Simon22 closed 1 year ago
Thanks for the bug report! I encountered this myself before when working with libclang
. If you wish to have caching still the workaround is to call FreeString
or one the similar functions.
In the next version of C2CS I will add an option to disable string caching.
Would Marshal.PtrToStringAnsi
be called every time then?
I think calling every time is kind of unavoidable if the native strings can be moved
Okay. I'm concerned about the implicit/explicit operator causes allocation of memory; perhaps I'll remove them if caching can be disabled. What do you think?
Explicit operators doing allocation does not bother me, but implicit can be more tricky because people might not be aware or know it happens. I didn't even think that there was a cache until I encountered this bug.
We might be able to keep the cache if the key is the pointer and the hash, but it might not be worth it performance-wise.
Discussion on flecs
discord, in the next version of C2CS:
unsafe
" C# code, but multiple people have requested for this.Sounds good, thank you.
If anyone encounters this in the current version, you can manually edit the Runtime.CStrings.String function for something like this:
public static string String(CString value)
{
if (value.IsNull)
{
return string.Empty;
}
// calls ASM/C/C++ functions to calculate length and then "FastAllocate" the string with the GC
// https://mattwarren.org/2016/05/31/Strings-and-the-CLR-a-Special-Relationship/
var result = Marshal.PtrToStringAnsi(value._pointer);
if (string.IsNullOrEmpty(result))
{
return string.Empty;
}
return result;
}
Remember to do the same for the StringWide if needed
fixed and released under v4.0.2
Hello,
Using the following code in the Runtime.CString, if the native strings are moved in memory, but their values are not changed, then it either complains about duplicate values in PointersToStrings or it points to garbage data.
If I manually disable the cache, the problems go away. I think there should be a way to disable this caching and a warning about its requirements in the documentation.