@@ -74,17 +74,22 @@ protected MemoryPoolBlock2()
74
74
/// </summary>
75
75
public MemoryPoolBlock2 Next { get ; set ; }
76
76
77
+ /// <summary>
78
+ /// Finalizes this instance.
79
+ /// </summary>
77
80
~ MemoryPoolBlock2 ( )
78
81
{
79
- Debug . Assert ( ! _pinHandle . IsAllocated , "Ad-hoc memory block wasn't unpinned" ) ;
80
- // Debug.Assert(Slab == null || !Slab.IsActive, "Block being garbage collected instead of returned to pool");
81
-
82
- if ( _pinHandle . IsAllocated )
82
+ if ( _pinHandle != null )
83
83
{
84
- // if this is a one-time-use block, ensure that the GCHandle does not leak
85
- _pinHandle . Free ( ) ;
86
- }
84
+ Debug . Assert ( ! _pinHandle . IsAllocated , "One-time-use memory is unpinned" ) ;
87
85
86
+ if ( _pinHandle . IsAllocated )
87
+ {
88
+ // TODO: Must be logged somehow.
89
+ _pinHandle . Free ( ) ;
90
+ }
91
+ }
92
+
88
93
if ( Slab != null && Slab . IsActive )
89
94
{
90
95
Pool . Return ( new MemoryPoolBlock2
@@ -104,17 +109,23 @@ protected MemoryPoolBlock2()
104
109
/// </summary>
105
110
/// <returns></returns>
106
111
public IntPtr Pin ( )
107
- {
108
- Debug . Assert ( ! _pinHandle . IsAllocated ) ;
109
-
112
+ {
110
113
if ( _dataArrayPtr != IntPtr . Zero )
111
114
{
112
- // this is a slab managed block - use the native address of the slab which is always locked
115
+ // This is a slab managed block - use the native address of the slab which is always locked.
113
116
return _dataArrayPtr ;
114
117
}
115
118
else
116
119
{
117
- // this is one-time-use memory - lock the managed memory until Unpin is called
120
+ // This block uses one-time-use memory.
121
+
122
+ if ( _pinHandle != null )
123
+ {
124
+ // This block was pinned at least once.
125
+
126
+ Debug . Assert ( ! _pinHandle . IsAllocated , "One-time-use memory is unpinned" ) ;
127
+ }
128
+
118
129
_pinHandle = GCHandle . Alloc ( Data . Array , GCHandleType . Pinned ) ;
119
130
return _pinHandle . AddrOfPinnedObject ( ) ;
120
131
}
@@ -124,8 +135,10 @@ public void Unpin()
124
135
{
125
136
if ( _dataArrayPtr == IntPtr . Zero )
126
137
{
127
- // this is one-time-use memory - unlock the managed memory
128
- Debug . Assert ( _pinHandle . IsAllocated ) ;
138
+ // This block uses one-time-use memory.
139
+
140
+ Debug . Assert ( _pinHandle != null && _pinHandle . IsAllocated , "One-time-use memory is pinned" ) ;
141
+
129
142
_pinHandle . Free ( ) ;
130
143
}
131
144
}
@@ -148,7 +161,7 @@ public static MemoryPoolBlock2 Create(
148
161
}
149
162
150
163
/// <summary>
151
- /// called when the block is returned to the pool. mutable values are re-assigned to their guaranteed initialized state.
164
+ /// Called when the block is returned to the pool. mutable values are re-assigned to their guaranteed initialized state.
152
165
/// </summary>
153
166
public void Reset ( )
154
167
{
@@ -167,7 +180,7 @@ public override string ToString()
167
180
}
168
181
169
182
/// <summary>
170
- /// acquires a cursor pointing into this block at the Start of "active" byte information
183
+ /// Acquires a cursor pointing into this block at the Start of "active" byte information
171
184
/// </summary>
172
185
/// <returns></returns>
173
186
public MemoryPoolIterator2 GetIterator ( )
0 commit comments