File tree Expand file tree Collapse file tree 3 files changed +40
-3
lines changed
src/ImageSharp/Memory/Allocators/Internals
tests/ImageSharp.Tests/Memory/Allocators Expand file tree Collapse file tree 3 files changed +40
-3
lines changed Original file line number Diff line number Diff line change @@ -24,7 +24,9 @@ public override unsafe MemoryHandle Pin(int elementIndex = 0)
2424 }
2525
2626 void * ptr = ( void * ) this . pinHandle . AddrOfPinnedObject ( ) ;
27- return new MemoryHandle ( ptr , this . pinHandle ) ;
27+
28+ // We should only pass pinnable:this, when GCHandle lifetime is managed by the MemoryManager<T> instance.
29+ return new MemoryHandle ( ptr , pinnable : this ) ;
2830 }
2931
3032 /// <inheritdoc />
@@ -42,4 +44,4 @@ public override void Unpin()
4244 /// <returns>The pinnable <see cref="object"/>.</returns>
4345 protected abstract object GetPinnableObject ( ) ;
4446 }
45- }
47+ }
Original file line number Diff line number Diff line change @@ -114,6 +114,23 @@ public void CleaningRequests_AreControlledByAllocationParameter_Clean(Allocation
114114 }
115115 }
116116
117+ [ Fact ]
118+ public unsafe void Allocate_MemoryIsPinnableMultipleTimes ( )
119+ {
120+ ArrayPoolMemoryAllocator allocator = this . LocalFixture . MemoryAllocator ;
121+ using IMemoryOwner < byte > memoryOwner = allocator . Allocate < byte > ( 100 ) ;
122+
123+ using ( MemoryHandle pin = memoryOwner . Memory . Pin ( ) )
124+ {
125+ Assert . NotEqual ( IntPtr . Zero , ( IntPtr ) pin . Pointer ) ;
126+ }
127+
128+ using ( MemoryHandle pin = memoryOwner . Memory . Pin ( ) )
129+ {
130+ Assert . NotEqual ( IntPtr . Zero , ( IntPtr ) pin . Pointer ) ;
131+ }
132+ }
133+
117134 [ Theory ]
118135 [ InlineData ( false ) ]
119136 [ InlineData ( true ) ]
Original file line number Diff line number Diff line change 22// Licensed under the Apache License, Version 2.0.
33
44using System ;
5+ using System . Buffers ;
56using System . Runtime . InteropServices ;
67using SixLabors . ImageSharp . Memory ;
78using Xunit ;
@@ -36,9 +37,26 @@ public void AllocateManagedByteBuffer_IncorrectAmount_ThrowsCorrect_ArgumentOutO
3637 Assert . Equal ( "length" , ex . ParamName ) ;
3738 }
3839
40+ [ Fact ]
41+ public unsafe void Allocate_MemoryIsPinnableMultipleTimes ( )
42+ {
43+ SimpleGcMemoryAllocator allocator = this . MemoryAllocator ;
44+ using IMemoryOwner < byte > memoryOwner = allocator . Allocate < byte > ( 100 ) ;
45+
46+ using ( MemoryHandle pin = memoryOwner . Memory . Pin ( ) )
47+ {
48+ Assert . NotEqual ( IntPtr . Zero , ( IntPtr ) pin . Pointer ) ;
49+ }
50+
51+ using ( MemoryHandle pin = memoryOwner . Memory . Pin ( ) )
52+ {
53+ Assert . NotEqual ( IntPtr . Zero , ( IntPtr ) pin . Pointer ) ;
54+ }
55+ }
56+
3957 [ StructLayout ( LayoutKind . Explicit , Size = 512 ) ]
4058 private struct BigStruct
4159 {
4260 }
4361 }
44- }
62+ }
You can’t perform that action at this time.
0 commit comments