在我们目前针对Android的Xamarin项目中,我们在单声道rt中看到了很多用于异步任务的SIGSEGV。以下是一个这样的示例代码,它产生大约20%的SIGSEGV次数。似乎我在这里做了一些根本性的错误,或者Xamarin for Android存在一些严重的问题。有人可以帮我指出生成这个SIGSEGV的代码有什么问题吗?
这与https://bugzilla.xamarin.com/show_bug.cgi?id=13707相关吗?
编辑:我现在正在整个应用程序中看到这种行为。这是log file,其他错误。所有崩溃都发生在异步任务代码上。这里真的有点不对劲。任何帮助表示赞赏。public async Task GetDataAsync(string downloadUri) {
using (WebClient downloader = new WebClient ()) {
downloader.Headers.Add ("x-bz-appId", "android");
downloader.Headers.Add ("x-bz-authToken", Comman.AuthToken);
downloader.Headers.Add(HttpRequestHeader.ContentType, "application/json");
var t = await downloader.DownloadStringTaskAsync(new Uri(downloadUri)).ContinueWith(downloadTask => {
if (downloadTask.Status == TaskStatus.RanToCompletion) {
JObject response = null;
response = JObject.Parse(downloadTask.Result);
return response;
} else {
if (downloadTask.Exception != null) {
throw downloadTask.Exception;
} else {
throw new Exception(downloadTask.Status.ToString());
}
}
});
return t;
}
}
[mono-rt] Stacktrace:
[mono-rt]
[mono-rt] at <0xffffffff>
[mono-rt] at (wrapper managed-to-native) object.__icall_wrapper_mono_array_new_specific (intptr,int)
[mono-rt] at System.Array.Resize (T[]&,int)
[mono-rt] at System.Collections.Generic.List`1.set_Capacity (int)
[mono-rt] at System.Collections.Generic.List`1.GrowIfNeeded (int)
[mono-rt] at System.Collections.Generic.List`1.Insert (int,T)
[mono-rt] at Newtonsoft.Json.Linq.JContainer.InsertItem (int,Newtonsoft.Json.Linq.JToken,bool)
[mono-rt] at Newtonsoft.Json.Linq.JProperty.InsertItem (int,Newtonsoft.Json.Linq.JToken,bool)
[mono-rt] at Newtonsoft.Json.Linq.JContainer.AddInternal (int,object,bool)
[mono-rt] at Newtonsoft.Json.Linq.JContainer.Add (object)
[mono-rt] at Newtonsoft.Json.Linq.JContainer.ReadContentFrom (Newtonsoft.Json.JsonReader)
[mono-rt] at Newtonsoft.Json.Linq.JContainer.ReadTokenFrom (Newtonsoft.Json.JsonReader)
[mono-rt] at Newtonsoft.Json.Linq.JObject.Load (Newtonsoft.Json.JsonReader)
[mono-rt] at Newtonsoft.Json.Linq.JObject.Parse (string)
[mono-rt] at Bloomz.Core.APIClient/c__async0/c__AnonStorey56.<>m__0 (System.Threading.Tasks.Task`1) [0x0004a] in /Users/hponnu/Projects/bloomz.native.android.2/Bloomz.Core/HelperClass/APIClient.cs:122
[mono-rt] at System.Threading.Tasks.TaskActionInvoker/FuncTaskInvoke`2.Invoke (System.Threading.Tasks.Task,object,System.Threading.Tasks.Task)
[mono-rt] at System.Threading.Tasks.Task.InnerInvoke ()
[mono-rt] at System.Threading.Tasks.Task.ThreadStart ()
[mono-rt] at System.Threading.Tasks.Task.Execute ()
[mono-rt] at System.Threading.Tasks.TaskScheduler.TryExecuteTask (System.Threading.Tasks.Task)
[mono-rt] at System.Threading.Tasks.SynchronizationContextScheduler.TaskLaunchWrapper (object)
[mono-rt] at Android.App.SyncContext/c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/1131/2a7b6821/source/monodroid/src/Mono.Android/src/Android.App/SyncContext.cs:18
[mono-rt] at Java.Lang.Thread/RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/1131/2a7b6821/source/monodroid/src/Mono.Android/src/Java.Lang/Thread.cs:36
[mono-rt] at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) [0x00009] in /Users/builder/data/lanes/1131/2a7b6821/source/monodroid/src/Mono.Android/platforms/android-12/src/generated/Java.Lang.IRunnable.cs:71
[mono-rt] at (wrapper dynamic-method) object.18db936a-f8a6-4eb0-838d-1159fbb9846c (intptr,intptr)
[mono-rt] at (wrapper native-to-managed) object.18db936a-f8a6-4eb0-838d-1159fbb9846c (intptr,intptr)
[mono-rt]
[mono-rt] =================================================================
[mono-rt] Got a SIGSEGV while executing native code. This usually indicates
[mono-rt] a fatal error in the mono runtime or one of the native libraries
[mono-rt] used by your application.
[mono-rt] =================================================================
[mono-rt]
答案 0 :(得分:1)
对于初学者来说,尝试重构代码以便正确使用异步语法,然后您可能会看到异常的来源。 你想要做的似乎是:
public async Task<JToken> GetDataAsync(string downloadUri) {
using (WebClient downloader = new WebClient ()) {
downloader.Headers.Add ("x-bz-appId", "android");
downloader.Headers.Add ("x-bz-authToken", Comman.AuthToken);
downloader.Headers.Add(HttpRequestHeader.ContentType, "application/json");
string unParsedResponse = null;
try{
unParsedResponse = await downloader.DownloadStringTaskAsync(new Uri(downloadUri));
}
cath(Exception ex){
throw;
}
JObject response = null;
response = JObject.Parse(unParsedResponse);
return response;
}
}
P.S。你真的不需要那里的try-catch块,因为等待一个任务会拦截它上面的任何异常,因为除了抛出它之外你什么都不做,尝试捕获在这里是徒劳的。
希望它有所帮助。