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)
24
24
}
25
25
26
26
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 ) ;
28
30
}
29
31
30
32
/// <inheritdoc />
@@ -42,4 +44,4 @@ public override void Unpin()
42
44
/// <returns>The pinnable <see cref="object"/>.</returns>
43
45
protected abstract object GetPinnableObject ( ) ;
44
46
}
45
- }
47
+ }
Original file line number Diff line number Diff line change @@ -114,6 +114,23 @@ public void CleaningRequests_AreControlledByAllocationParameter_Clean(Allocation
114
114
}
115
115
}
116
116
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
+
117
134
[ Theory ]
118
135
[ InlineData ( false ) ]
119
136
[ InlineData ( true ) ]
Original file line number Diff line number Diff line change 2
2
// Licensed under the Apache License, Version 2.0.
3
3
4
4
using System ;
5
+ using System . Buffers ;
5
6
using System . Runtime . InteropServices ;
6
7
using SixLabors . ImageSharp . Memory ;
7
8
using Xunit ;
@@ -36,9 +37,26 @@ public void AllocateManagedByteBuffer_IncorrectAmount_ThrowsCorrect_ArgumentOutO
36
37
Assert . Equal ( "length" , ex . ParamName ) ;
37
38
}
38
39
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
+
39
57
[ StructLayout ( LayoutKind . Explicit , Size = 512 ) ]
40
58
private struct BigStruct
41
59
{
42
60
}
43
61
}
44
- }
62
+ }
You can’t perform that action at this time.
0 commit comments