@@ -53,10 +53,6 @@ static SilkMarshal()
53
53
// This means that the GlobalMemory is only freed when the user calls Free.
54
54
private static readonly ConcurrentDictionary < nint , GlobalMemory > _marshalledMemory = new ( ) ;
55
55
56
- // In addition, we should keep track of the memory we allocate dedicated to string arrays. If we don't, we won't
57
- // know to free the individual strings allocated within memory.
58
- private static readonly ConcurrentDictionary < GlobalMemory , int > _stringArrays = new ( ) ;
59
-
60
56
// Other kinds of GCHandle-pinned pointers may be passed into Free, like delegate pointers for example which
61
57
// must have GCHandles allocated on older runtimes to avoid an ExecutionEngineException.
62
58
// We should keep track of those.
@@ -96,15 +92,6 @@ public static bool Free(nint ptr)
96
92
return ret ;
97
93
}
98
94
99
- if ( _stringArrays . TryRemove ( val , out var numStrings ) )
100
- {
101
- var span = val . AsSpan < nint > ( ) ;
102
- for ( var i = 0 ; i < numStrings ; i ++ )
103
- {
104
- Free ( span [ i ] ) ;
105
- }
106
- }
107
-
108
95
val . Dispose ( ) ;
109
96
return ret ;
110
97
}
@@ -364,7 +351,7 @@ public static GlobalMemory StringArrayToMemory
364
351
NativeStringEncoding e = NativeStringEncoding . Ansi
365
352
)
366
353
{
367
- var memory = GlobalMemory . Allocate ( input . Count * IntPtr . Size ) ;
354
+ var memory = GlobalMemory . AllocateForStringArray ( input . Count * IntPtr . Size , input . Count ) ;
368
355
var span = memory . AsSpan < nint > ( ) ;
369
356
for ( var i = 0 ; i < input . Count ; i ++ )
370
357
{
@@ -386,7 +373,7 @@ public static GlobalMemory StringArrayToMemory
386
373
Func < string , nint > customStringMarshaller
387
374
)
388
375
{
389
- var memory = GlobalMemory . Allocate ( input . Count * IntPtr . Size ) ;
376
+ var memory = GlobalMemory . AllocateForStringArray ( input . Count * IntPtr . Size , input . Count ) ;
390
377
var span = memory . AsSpan < nint > ( ) ;
391
378
for ( var i = 0 ; i < input . Count ; i ++ )
392
379
{
@@ -409,7 +396,6 @@ public static nint StringArrayToPtr
409
396
)
410
397
{
411
398
var memory = StringArrayToMemory ( input , encoding ) ;
412
- _stringArrays . TryAdd ( memory , input . Count ) ;
413
399
return RegisterMemory ( memory ) ;
414
400
}
415
401
@@ -426,7 +412,6 @@ Func<string, nint> customStringMarshaller
426
412
)
427
413
{
428
414
var memory = StringArrayToMemory ( input , customStringMarshaller ) ;
429
- _stringArrays . TryAdd ( memory , input . Count ) ;
430
415
return RegisterMemory ( memory ) ;
431
416
}
432
417
0 commit comments