针对Xamarin开发安卓异常退出的一种原因
经验一般,遇到一些低级错误,但有些坑就在那里,发现,分享,共勉:
App异常退出,抛出的调试信息如下:
Thread finished: #21
线程 0x15 已退出,返回值为 0 (0x0)。
06-14 19:04:02.645 D/Mono ( 8831): GC_BRIDGE waiting for bridge processing to finish
06-14 19:04:02.705 D/dalvikvm( 8831): GC_EXPLICIT freed 31433K, 89% free 4387K/36868K, paused 3ms+5ms, total 40ms
06-14 19:04:02.745 D/Mono ( 8831): GC_TAR_BRIDGE bridges 6014 objects 6015 opaque 0 colors 6014 colors-bridged 6014 colors-visible 6014 xref 0 cache-hit 0 cache-semihit 0 cache-miss 0 setup 1.36ms tarjan 6.81ms scc-setup 2.99ms gather-xref 0.34ms xref-setup 0.07ms cleanup 2.39ms
06-14 19:04:02.745 D/Mono ( 8831): GC_BRIDGE: Complete, was running for 97.91ms
06-14 19:04:02.745 D/Mono ( 8831): GC_MINOR: (Nursery full) time 71.56ms, stw 73.31ms promoted 808K major size: 1616K in use: 902K los size: 1024K in use: 351K
In mgmain JNI_OnLoad
06-14 19:04:02.825 E/mono-rt ( 8831): [ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidOperationException: Handle is not initialized.
06-14 19:04:02.825 E/mono-rt ( 8831): at System.Runtime.InteropServices.GCHandle.Free () [0x00021] in <657aa8fea4454dc898a9e5f379c58734>:0
06-14 19:04:02.825 E/mono-rt ( 8831): at R91Range.RangeKernel.RcmProtocal.Finalize () [0x0001b] in E:\LCY\Temp\NJAWS\R91Range\RangeKernel\RcmProtocal\RcmProtocal.cs:16
通过分析,不难发现,是最后一句有异常,顺藤摸瓜
最终锁定为RcmProtocal.cs的16行为“HandleGC.Free();”
源代码如下:
public class RcmProtocal
{
~RcmProtocal()
{
if (_ReciveCacellation != null)
_ReciveCacellation.Cancel();
HandleGC.Free();
BoardHandleGC.Free();
}
………………
}
将其修改后
~RcmProtocal()
{
if (_ReciveCacellation != null)
_ReciveCacellation.Cancel();
try
{
HandleGC.Free();
BoardHandleGC.Free();
}
catch(Exception e)
{
Console.WriteLine("捕获到长期致命异常:"+e.Message);
}
}
通过异常捕获机制终于发现
捕获到长期致命异常:Handle is not initialized.
06-14 19:26:08.065 I/mono-stdout(26806): 捕获到长期致命异常:Handle is not initialized.
显然这就是释放资源抛出的异常,一切正常了。
2018年6月9日23:12:31
Dawn
本文分析了一种Xamarin开发的Android应用出现异常退出的问题,并详细记录了从异常调试信息出发定位问题根源的过程,最终通过修改析构函数中的资源释放代码解决了问题。

767

被折叠的 条评论
为什么被折叠?



