@@ -19,7 +19,7 @@ public sealed class PixelBuffer
1919
2020 private byte [ ] ? imageContainer ;
2121 private int imageContainerSize ;
22- private byte [ ] ? transcodedImageContainer = null ;
22+ private byte [ ] ? transcodedImageContainer ;
2323 private bool isValidTranscodedImage ;
2424 private long timestampMicroseconds ;
2525 private TranscodeFormats transcodeFormat ;
@@ -48,18 +48,25 @@ internal unsafe void CopyIn(
4848
4949 lock ( this )
5050 {
51- if ( this . imageContainer == null ||
52- this . imageContainer . Length < totalSize )
51+ var imageContainer = this . imageContainer ;
52+
53+ if ( imageContainer == null ||
54+ imageContainer . Length < totalSize )
5355 {
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 ;
5562
56- this . imageContainer = this . bufferPool . Rent ( totalSize , false ) ;
63+ Debug . WriteLine ( $ "Allocated: Size= { totalSize } " ) ;
5764 }
5865
5966 this . imageContainerSize = totalSize ;
6067 this . isValidTranscodedImage = false ;
6168
62- fixed ( byte * pImageContainer = this . imageContainer ! )
69+ fixed ( byte * pImageContainer = imageContainer )
6370 {
6471 if ( pBih ->biCompression == NativeMethods . Compression . MJPG ||
6572 pBih ->biCompression == NativeMethods . Compression . BI_JPEG ||
@@ -117,24 +124,28 @@ internal unsafe ArraySegment<byte> InternalExtractImage(BufferStrategies strateg
117124 {
118125 lock ( this )
119126 {
120- if ( this . imageContainer == null )
127+ var imageContainer = this . imageContainer ;
128+ if ( imageContainer == null )
121129 {
122130 throw new InvalidOperationException ( "Extracted before capture." ) ;
123131 }
124132
125133 if ( this . transcodeFormat != TranscodeFormats . DoNotTranscode )
126134 {
127- if ( this . isValidTranscodedImage && this . transcodedImageContainer != null )
135+ var transcodedImageContainer = this . transcodedImageContainer ;
136+ if ( this . isValidTranscodedImage && transcodedImageContainer != null )
128137 {
129138 if ( strategy == BufferStrategies . ForceReuse )
130139 {
131- return new ArraySegment < byte > ( this . transcodedImageContainer ) ;
140+ return new ArraySegment < byte > ( transcodedImageContainer ) ;
132141 }
133142 else
134143 {
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 ) ;
138149 }
139150 }
140151
@@ -154,6 +165,10 @@ internal unsafe ArraySegment<byte> InternalExtractImage(BufferStrategies strateg
154165 if ( this . transcodedImageContainer == null ||
155166 this . transcodedImageContainer . Length != totalSize )
156167 {
168+ if ( this . transcodedImageContainer != null )
169+ {
170+ this . bufferPool . Return ( this . transcodedImageContainer ) ;
171+ }
157172 this . transcodedImageContainer = this . bufferPool . Rent ( totalSize , true ) ;
158173 }
159174
@@ -200,9 +215,7 @@ internal unsafe ArraySegment<byte> InternalExtractImage(BufferStrategies strateg
200215 }
201216 else
202217 {
203- var copied1 = this . transcodedImageContainer ;
204- this . transcodedImageContainer = null ;
205- return new ArraySegment < byte > ( copied1 ) ;
218+ return new ArraySegment < byte > ( this . transcodedImageContainer ) ;
206219 }
207220 }
208221 }
@@ -211,19 +224,19 @@ internal unsafe ArraySegment<byte> InternalExtractImage(BufferStrategies strateg
211224 switch ( strategy )
212225 {
213226 case BufferStrategies . ForceReuse :
214- return new ArraySegment < byte > ( this . imageContainer , 0 , this . imageContainerSize ) ;
227+ return new ArraySegment < byte > ( imageContainer , 0 , this . imageContainerSize ) ;
215228 case BufferStrategies . CopyWhenDifferentSizeOrReuse :
216- if ( this . imageContainer . Length == this . imageContainerSize )
229+ if ( imageContainer . Length == this . imageContainerSize )
217230 {
218- return new ArraySegment < byte > ( this . imageContainer ) ;
231+ return new ArraySegment < byte > ( imageContainer ) ;
219232 }
220233 break ;
221234 }
222235
223236 var copied = this . bufferPool . Rent ( this . imageContainerSize , true ) ;
224- Array . Copy ( this . imageContainer , copied , copied . Length ) ;
237+ Array . Copy ( imageContainer , copied , copied . Length ) ;
225238
226- Debug . WriteLine ( $ "Copied: CurrentSize={ this . imageContainer . Length } , Size={ this . imageContainerSize } ") ;
239+ Debug . WriteLine ( $ "Copied: CurrentSize={ imageContainer . Length } , Size={ this . imageContainerSize } ") ;
227240
228241 return new ArraySegment < byte > ( copied ) ;
229242 }
0 commit comments