@@ -78,9 +78,9 @@ internal class CanvasStrategy(
7878 return @Runnable
7979 }
8080 val picture = unprocessedPictureRef.getAndSet(null ) ? : return @Runnable
81-
8281 try {
83- // Draw picture to the Surface for PixelCopy
82+ // It's safe to access the surface because the
83+ // surface release within close() is executed on the same background handler
8484 val surfaceCanvas = surface.lockHardwareCanvas()
8585 try {
8686 surfaceCanvas.drawColor(Color .BLACK , PorterDuff .Mode .CLEAR )
@@ -92,16 +92,27 @@ internal class CanvasStrategy(
9292 if (screenshot == null ) {
9393 screenshotLock.acquire().use {
9494 if (screenshot == null ) {
95- screenshot = Bitmap .createBitmap(picture.width, picture.height, Bitmap .Config .ARGB_8888 )
95+ screenshot =
96+ Bitmap .createBitmap(
97+ config.recordingWidth,
98+ config.recordingHeight,
99+ Bitmap .Config .ARGB_8888 ,
100+ )
96101 }
97102 }
98103 }
99104
100- // Trigger PixelCopy capture
101105 PixelCopy .request(
102106 surface,
103107 screenshot!! ,
104108 { result ->
109+ if (isClosed.get()) {
110+ options.logger.log(
111+ SentryLevel .DEBUG ,
112+ " CanvasStrategy is closed, ignoring capture result" ,
113+ )
114+ return @request
115+ }
105116 if (result == PixelCopy .SUCCESS ) {
106117 lastCaptureSuccessful.set(true )
107118 val bitmap = screenshot
@@ -119,7 +130,7 @@ internal class CanvasStrategy(
119130 executor.getBackgroundHandler(),
120131 )
121132 } catch (t: Throwable ) {
122- options.logger.log(SentryLevel .ERROR , " Canvas Strategy: picture render failed" )
133+ options.logger.log(SentryLevel .ERROR , " Canvas Strategy: picture render failed" , t )
123134 lastCaptureSuccessful.set(false )
124135 }
125136 }
@@ -153,8 +164,8 @@ internal class CanvasStrategy(
153164 override fun close () {
154165 isClosed.set(true )
155166 executor
156- .getExecutor ()
157- .submit (
167+ .getBackgroundHandler ()
168+ .post (
158169 ReplayRunnable (" CanvasStrategy.close" ) {
159170 screenshot?.let { synchronized(it) { if (! it.isRecycled) it.recycle() } }
160171 surface.release()
0 commit comments