@@ -19,7 +19,7 @@ public sealed class PixelBuffer
19
19
20
20
private byte [ ] ? imageContainer ;
21
21
private int imageContainerSize ;
22
- private byte [ ] ? transcodedImageContainer = null ;
22
+ private byte [ ] ? transcodedImageContainer ;
23
23
private bool isValidTranscodedImage ;
24
24
private long timestampMicroseconds ;
25
25
private TranscodeFormats transcodeFormat ;
@@ -48,18 +48,25 @@ internal unsafe void CopyIn(
48
48
49
49
lock ( this )
50
50
{
51
- if ( this . imageContainer == null ||
52
- this . imageContainer . Length < totalSize )
51
+ var imageContainer = this . imageContainer ;
52
+
53
+ if ( imageContainer == null ||
54
+ imageContainer . Length < totalSize )
53
55
{
54
- Debug . WriteLine ( $ "Allocated: CurrentSize={ this . imageContainer ? . Length ?? 0 } , Size={ totalSize } ") ;
56
+ if ( imageContainer != null )
57
+ {
58
+ this . bufferPool . Return ( imageContainer ) ;
59
+ }
60
+ imageContainer = this . bufferPool . Rent ( totalSize , false ) ;
61
+ this . imageContainer = imageContainer ;
55
62
56
- this . imageContainer = this . bufferPool . Rent ( totalSize , false ) ;
63
+ Debug . WriteLine ( $ "Allocated: Size= { totalSize } " ) ;
57
64
}
58
65
59
66
this . imageContainerSize = totalSize ;
60
67
this . isValidTranscodedImage = false ;
61
68
62
- fixed ( byte * pImageContainer = this . imageContainer ! )
69
+ fixed ( byte * pImageContainer = imageContainer )
63
70
{
64
71
if ( pBih ->biCompression == NativeMethods . Compression . MJPG ||
65
72
pBih ->biCompression == NativeMethods . Compression . BI_JPEG ||
@@ -117,24 +124,28 @@ internal unsafe ArraySegment<byte> InternalExtractImage(BufferStrategies strateg
117
124
{
118
125
lock ( this )
119
126
{
120
- if ( this . imageContainer == null )
127
+ var imageContainer = this . imageContainer ;
128
+ if ( imageContainer == null )
121
129
{
122
130
throw new InvalidOperationException ( "Extracted before capture." ) ;
123
131
}
124
132
125
133
if ( this . transcodeFormat != TranscodeFormats . DoNotTranscode )
126
134
{
127
- if ( this . isValidTranscodedImage && this . transcodedImageContainer != null )
135
+ var transcodedImageContainer = this . transcodedImageContainer ;
136
+ if ( this . isValidTranscodedImage && transcodedImageContainer != null )
128
137
{
129
138
if ( strategy == BufferStrategies . ForceReuse )
130
139
{
131
- return new ArraySegment < byte > ( this . transcodedImageContainer ) ;
140
+ return new ArraySegment < byte > ( transcodedImageContainer ) ;
132
141
}
133
142
else
134
143
{
135
- var copied1 = this . bufferPool . Rent ( this . transcodedImageContainer . Length , true ) ;
136
- Array . Copy ( this . transcodedImageContainer , copied1 , copied1 . Length ) ;
137
- return new ArraySegment < byte > ( copied1 ) ;
144
+ var copiedImageContainer = new byte [ transcodedImageContainer . Length ] ;
145
+ Array . Copy ( transcodedImageContainer ,
146
+ copiedImageContainer , copiedImageContainer . Length ) ;
147
+
148
+ return new ArraySegment < byte > ( copiedImageContainer ) ;
138
149
}
139
150
}
140
151
@@ -154,6 +165,10 @@ internal unsafe ArraySegment<byte> InternalExtractImage(BufferStrategies strateg
154
165
if ( this . transcodedImageContainer == null ||
155
166
this . transcodedImageContainer . Length != totalSize )
156
167
{
168
+ if ( this . transcodedImageContainer != null )
169
+ {
170
+ this . bufferPool . Return ( this . transcodedImageContainer ) ;
171
+ }
157
172
this . transcodedImageContainer = this . bufferPool . Rent ( totalSize , true ) ;
158
173
}
159
174
@@ -200,9 +215,7 @@ internal unsafe ArraySegment<byte> InternalExtractImage(BufferStrategies strateg
200
215
}
201
216
else
202
217
{
203
- var copied1 = this . transcodedImageContainer ;
204
- this . transcodedImageContainer = null ;
205
- return new ArraySegment < byte > ( copied1 ) ;
218
+ return new ArraySegment < byte > ( this . transcodedImageContainer ) ;
206
219
}
207
220
}
208
221
}
@@ -211,19 +224,19 @@ internal unsafe ArraySegment<byte> InternalExtractImage(BufferStrategies strateg
211
224
switch ( strategy )
212
225
{
213
226
case BufferStrategies . ForceReuse :
214
- return new ArraySegment < byte > ( this . imageContainer , 0 , this . imageContainerSize ) ;
227
+ return new ArraySegment < byte > ( imageContainer , 0 , this . imageContainerSize ) ;
215
228
case BufferStrategies . CopyWhenDifferentSizeOrReuse :
216
- if ( this . imageContainer . Length == this . imageContainerSize )
229
+ if ( imageContainer . Length == this . imageContainerSize )
217
230
{
218
- return new ArraySegment < byte > ( this . imageContainer ) ;
231
+ return new ArraySegment < byte > ( imageContainer ) ;
219
232
}
220
233
break ;
221
234
}
222
235
223
236
var copied = this . bufferPool . Rent ( this . imageContainerSize , true ) ;
224
- Array . Copy ( this . imageContainer , copied , copied . Length ) ;
237
+ Array . Copy ( imageContainer , copied , copied . Length ) ;
225
238
226
- Debug . WriteLine ( $ "Copied: CurrentSize={ this . imageContainer . Length } , Size={ this . imageContainerSize } ") ;
239
+ Debug . WriteLine ( $ "Copied: CurrentSize={ imageContainer . Length } , Size={ this . imageContainerSize } ") ;
227
240
228
241
return new ArraySegment < byte > ( copied ) ;
229
242
}
0 commit comments