【问题标题】:Realm crashes while resolving ThreadSafeReference解决 ThreadSafeReference 时领域崩溃
【发布时间】:2020-08-24 11:56:24
【问题描述】:

在尝试从单独的线程取回结果时,我的应用程序只是停止工作并退出,我已经能够编写此代码

public async override Task<IQueryable<Entry>> FindAsync(string inputText)
{
    var resultingEntries = await Task.Run(() =>
    {
        // The heavy stuff starts here, in anew thread.
        using (var realm = Realm.GetInstance(DbConfig))
        {
            // All data
            var bgHaystack = realm.All<Entry>();

            // Because realm doesn't support some of the LINQ operations on not stored fields (Content)
            // the set of entries is converted to a IEnumerable.
            IEnumerable<Entry> subset = bgHaystack;

            // This is where the search gets actually done
            subset = subset.Where(entry => entry.Content.ToLower().StartsWith(inputText));

            // Extracts ids
            var foundEntryIds = ExtractIdsFromEntries(subset);

            // Select entries
            var foundEntries = FindingManyMatches(bgHaystack, foundEntryIds.ToArray());

            return ThreadSafeReference.Create(foundEntries);
        }
    });

    var results = Realm.GetInstance(DbConfig).ResolveReference(resultingEntries);
    return results;
}

除了倒数第二行我想得到结果对象外,一切正常,你知道这里出了什么问题以及如何解决这个问题吗?

堆栈跟踪:

Native Crash Reporting
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

No native Android stacktrace (see debuggerd output).

=================================================================
    Basic Fault Address Reporting
=================================================================
Memory around native instruction pointer (0x7cdcb67c70):0x7cdcb67c60  c0 03 5f d6 c8 02 80 52 e0 03 08 2a c0 03 5f d6  .._....R...*.._.
0x7cdcb67c70  08 00 40 79 09 3d 0e 53 08 3d 00 12 69 01 00 35  ..@y.=.S.=..i..5
0x7cdcb67c80  09 01 13 12 2a 01 00 32 0b fc 5f 48 7f 21 29 6b  ....*..2.._H.!)k
0x7cdcb67c90  a1 00 00 54 0a 7c 0b 48 8b ff ff 35 e0 03 1f 2a  ...T.|.H...5...*

================================================

05-08 23:09:47.037 D/Mono    (25933): Found as 'shared_realm_resolve_object_reference'.=================
    Managed Stacktrace:
=================================================================
      at <unknown> <0xffffffff>
      at NativeMethods:resolve_query_reference <0x00007>
      at Realms.SharedRealmHandle:ResolveReference <0x00243>
      at Realms.Realm:ResolveReference <0x0008f>
      at <FindAsync>d__9:MoveNext <0x0062f>
      at MoveNextRunner:InvokeMoveNext <0x000f3>
      at System.Threading.ExecutionContext:RunInternal <0x003af>
      at System.Threading.ExecutionContext:Run <0x0006b>
      at MoveNextRunner:Run <0x00193>
      at <>c:<.cctor>b__7_0 <0x0009b>
      at <>c__DisplayClass2_0:<Post>b__0 <0x00093>
      at RunnableImplementor:Run <0x000bb>
      at Java.Lang.IRunnableInvoker:n_Run <0x000c3>
      at Android.Runtime.DynamicMethodNameCounter:20 <0x000af>
      at Android.Runtime.DynamicMethodNameCounter:20 <0x000e3>
=================================================================

05-08 23:09:47.037 D/Mono    (25933): DllImport searching in: 'realm-wrappers' ('librealm-wrappers.so').
05-08 23:09:47.037 D/Mono    (25933): Searching for 'shared_realm_resolve_list_reference'.
05-08 23:09:47.037 D/Mono    (25933): Probing 'shared_realm_resolve_list_reference'.
05-08 23:09:47.037 D/Mono    (25933): Found as 'shared_realm_resolve_list_reference'.
05-08 23:09:47.037 D/Mono    (25933): DllImport searching in: 'realm-wrappers' ('librealm-wrappers.so').
05-08 23:09:47.037 D/Mono    (25933): Searching for 'shared_realm_resolve_query_reference'.
05-08 23:09:47.037 D/Mono    (25933): Probing 'shared_realm_resolve_query_reference'.
05-08 23:09:47.037 D/Mono    (25933): Found as 'shared_realm_resolve_query_reference'.
05-08 23:09:47.040 F/libc    (25933): Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x188 in tid 25933 (panyname.dosham), pid 25933 (panyname.dosham)
05-08 23:09:47.061 D/Mono    (25933): DllImport searching in: 'realm-wrappers' ('librealm-wrappers.so').
05-08 23:09:47.062 D/Mono    (25933): Searching for 'query_destroy'.
05-08 23:09:47.062 D/Mono    (25933): Probing 'query_destroy'.
05-08 23:09:47.062 D/Mono    (25933): Found as 'query_destroy'.
05-08 23:09:47.259 I/panyname.dosha(25933): ProcessProfilingInfo new_methods=0 is saved saved_to_disk=0 resolve_classes_delay=8000

【问题讨论】:

    标签: c# android asynchronous realm thread-safety


    【解决方案1】:

    好像是个bug,去掉

    using (var realm = Realm.GetInstance(DbConfig))
    

    解决了问题,即

                var resultingEntries = await Task.Run(() =>
                {
                    var allEntries = Realm.GetInstance(DbConfig).All<Entry>();
    
                    // Because realm doesn't support some of the LINQ operations on not stored fields (Content)
                    // the set of entries is converted to a IEnumerable.
                    IEnumerable<Entry> subset = allEntries;
    
                    // This is where the search gets actually done
                    subset = subset.Where(entry => entry.Content.ToLower().StartsWith(inputText));
    
                    // Extracts ids
                    var foundEntryIds = ExtractIdsFromEntries(subset);
    
                    // Select entries
                    var foundEntries = FindingManyMatches(allEntries, foundEntryIds.ToArray());
                    var foundEntriesCount = foundEntries.Count();
    
                    return ThreadSafeReference.Create(foundEntries);
                });
    
                var results = Realm.GetInstance(DbConfig).ResolveReference(resultingEntries);
                return results;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多