Skip to content

Commit 342de5f

Browse files
committed
Use weak reference in the exception handler
1 parent 6ae6fda commit 342de5f

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/AztecExceptionHandler.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import org.wordpress.android.util.AppLog
66
import org.wordpress.aztec.exceptions.DynamicLayoutGetBlockIndexOutOfBoundsException
77
import org.wordpress.aztec.util.AztecLog
88
import java.lang.Thread.UncaughtExceptionHandler
9+
import java.lang.ref.WeakReference
910

10-
class AztecExceptionHandler(private var logHelper: ExceptionHandlerHelper?, private var visualEditor: AztecText?) : UncaughtExceptionHandler {
11+
class AztecExceptionHandler(private var logHelper: WeakReference<ExceptionHandlerHelper>, private var visualEditor: WeakReference<AztecText>) : UncaughtExceptionHandler {
12+
constructor(logHelper: ExceptionHandlerHelper, visualEditor: AztecText): this(WeakReference(logHelper), WeakReference(visualEditor))
1113

1214
interface ExceptionHandlerHelper {
1315
fun shouldLog(ex: Throwable) : Boolean
@@ -25,7 +27,7 @@ class AztecExceptionHandler(private var logHelper: ExceptionHandlerHelper?, priv
2527
// Check if we should log the content or not
2628
var shouldLog = true
2729
try {
28-
shouldLog = logHelper?.shouldLog(ex) ?: true
30+
shouldLog = logHelper.get()?.shouldLog(ex) ?: true
2931
} catch (e: Throwable) {
3032
AppLog.w(AppLog.T.EDITOR, "There was an exception in the Logger Helper. Set the logging to true")
3133
}
@@ -34,12 +36,12 @@ class AztecExceptionHandler(private var logHelper: ExceptionHandlerHelper?, priv
3436
// Try to report the HTML code of the content, the spans details, but do not report exceptions that can occur logging the content
3537
try {
3638
AppLog.e(AppLog.T.EDITOR, "HTML content of Aztec Editor before the crash:")
37-
AppLog.e(AppLog.T.EDITOR, visualEditor?.toPlainHtml(false) ?: "Editor was cleared")
39+
AppLog.e(AppLog.T.EDITOR, visualEditor.get()?.toPlainHtml(false) ?: "Editor was cleared")
3840
} catch (e: Throwable) {
3941
AppLog.e(AppLog.T.EDITOR, "Oops! There was an error logging the HTML code.")
4042
}
4143
try {
42-
visualEditor?.let {
44+
visualEditor.get()?.let {
4345
AztecLog.logContentDetails(it)
4446
}
4547
} catch (e: Throwable) {
@@ -60,16 +62,16 @@ class AztecExceptionHandler(private var logHelper: ExceptionHandlerHelper?, priv
6062
detected = true
6163
}
6264
if (detected) {
63-
visualEditor?.externalLogger?.logException(DynamicLayoutGetBlockIndexOutOfBoundsException("Error #8828", ex))
65+
visualEditor.get()?.externalLogger?.logException(DynamicLayoutGetBlockIndexOutOfBoundsException("Error #8828", ex))
6466
}
6567
}
6668

6769
rootHandler?.uncaughtException(thread, ex)
6870
}
6971

7072
fun restoreDefaultHandler() {
71-
visualEditor = null
72-
logHelper = null
73+
visualEditor.clear()
74+
logHelper.clear()
7375
Thread.setDefaultUncaughtExceptionHandler(rootHandler)
7476
}
7577
}

0 commit comments

Comments
 (0)