菜鸟教程小白 发表于 2022-12-13 09:29:26

iphone - 带有 NSFetchedResultsController 的 EXC_BAD_ACCESS


                                            <p><p>试图追踪我在 <code>NSZombieEnabled</code> 时遇到的这个错误。</p>

<p>当获取的结果 Controller 尝试执行获取时,我得到一个 EXC_BAD_ACCESS。</p>

<p>调试器说:</p>

<pre><code>-: message sent to deallocated instance 0x6e88d10
</code></pre>

<p>回溯说:</p>

<pre><code>#00x014bbe1e in ___forwarding___ ()
#10x014bbce2 in __forwarding_prep_0___ ()
#20x0145c490 in CFRetain ()
#30x0147929a in CFNumberCreate ()
#40x00b221ed in - ()
#50x00b2216a in + ()
#60x010a549e in snapshot_get_value_as_object ()
#70x010a2a41 in _sharedIMPL_pvfk_core ()
#80x010a707d in - ()
#90x010df6f0 in _PF_Handler_Public_GetProperty ()
#10 0x010df61d in - ()
#11 0x00aea8d5 in - ()
#12 0x00b1ad5e in _NSSortFunctionMany ()
#13 0x0152bf3f in __CFMergeSortArray_block_invoke_0 ()
#14 0x014b19d3 in __CFSimpleMergeSort ()
#15 0x014b1969 in __CFSimpleMergeSort ()
#16 0x014b1969 in __CFSimpleMergeSort ()
#17 0x014b1828 in CFSortIndexes ()
#18 0x014eb7e9 in CFMergeSortArray ()
#19 0x00b1828e in _sortedObjectsUsingDescriptors ()
#20 0x00b17fd9 in - ()
#21 0x010853c6 in - ()
#22 0x0118fc8f in - ()
#23 0x000105c0 in - (self=0x6d4bc80, _cmd=0x15a28) at /Users/david/Dropbox/Xcode/MyApp/MyApp/RootViewController.m:513
#24 0x0000e549 in - (self=0x6d4bc80, _cmd=0x158d6) at /Users/david/Dropbox/Xcode/MyApp/MyApp/RootViewController.m:159
#25 0x01556ec9 in - ()
#26 0x001dc5c2 in - ()
#27 0x001dc55a in - ()
#28 0x00281b76 in - ()
#29 0x0028203f in - ()
#30 0x00281bab in - ()
#31 0x002cfa99 in - ()
#32 0x002d1407 in - ()
#33 0x0020193f in - ()
#34 0x00201c56 in - ()
#35 0x001e8384 in - ()
#36 0x001dbaa9 in _UIApplicationHandleEvent ()
#37 0x02442fa9 in PurpleEventCallback ()
#38 0x015291c5 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
#39 0x0148e022 in __CFRunLoopDoSource1 ()
#40 0x0148c90a in __CFRunLoopRun ()
#41 0x0148bdb4 in CFRunLoopRunSpecific ()
#42 0x0148bccb in CFRunLoopRunInMode ()
#43 0x02441879 in GSEventRunModal ()
#44 0x0244193e in GSEventRun ()
#45 0x001d9a9b in UIApplicationMain ()
#46 0x000020ed in main (argc=1, argv=0xbffff5e8) at   /Users/david/Dropbox/Xcode/MyApp/MyApp/main.m:14
</code></pre>

<p>这是NSFetchedResultsController中的相关代码:</p>

<pre><code>- (NSFetchedResultsController *)fetchedResultsController

{

    if (__fetchedResultsController != nil)
{
    return __fetchedResultsController;
}

NSFetchRequest *fetchRequest = [ init];
NSEntityDescription *entity = ;
;

;

// sort keys
NSSortDescriptor *sortDescriptor1 = [ initWithKey:kCategory ascending:YES];
NSSortDescriptor *sortDescriptor2 = [ initWithKey:kOrder ascending:YES]; // THIS IS AN INT 16 IN THE CORE DATA MODEL
NSArray *sortDescriptors = [ initWithObjects:sortDescriptor1, sortDescriptor2, nil];

;

//filter for males or females by tab index
//set the tabIndex so the fetchedResultsController knows which sex to filter
NSUInteger tabIndex = self.appDelegate.tabBarController.selectedIndex;
NSString *theSex = ;
if (tabIndex == 0)
    theSex = vFemale;
else
    theSex = vMale;

//determine if sitting or standing
NSInteger segmentIndex = self.segmentedControl.selectedSegmentIndex;
NSString *sittingOrStanding = ;
if (segmentIndex == 0)
    sittingOrStanding = vSitting;
else
    sittingOrStanding = vStanding;

NSPredicate *predicate = ;
;

NSFetchedResultsController *aFetchedResultsController = [ initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:kCategory cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

NSError *error = nil;
if (!) //THIS IS WHERE EXC_BAD_ACCESS HAPPENS
    {
    NSLog(@&#34;Unresolved error %@, %@&#34;, error, );
}
;
;
;
;
;

return __fetchedResultsController;

}   
</code></pre>

<p>我唯一能想到的是我的核心数据模型中的 <code>kOrder</code> 属性设置为 Int 16。但是,我不确定是什么过早地发布了这个 <code>CFNumber</code>这似乎是通过私有(private)方法保留的。非常感谢任何帮助。</p>

<p>Edit1:头文件:</p>

<pre><code>@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
</code></pre>

<p>然后在实现中:</p>

<pre><code>@synthesize fetchedResultsController=__fetchedResultsController;
</code></pre>

<p>Edit2:为了清楚起见,我的应用在 iOS 4 上运行良好,但在 iOS 5 上只崩溃。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>设置 <a href="http://www.cocoadev.com/index.pl?NSZombieEnabled" rel="noreferrer noopener nofollow">NSZombieEnabled</a> , <a href="http://www.cocoadev.com/index.pl?MallocStackLogging" rel="noreferrer noopener nofollow">MallocStackLogging</a> , 和 <a href="http://developer.apple.com/library/ios/ipad/#DOCUMENTATION/Performance/Conceptual/ManagingMemory/Articles/MallocDebug.html" rel="noreferrer noopener nofollow">guard malloc</a>在调试器中。然后,当您的应用程序崩溃时,在 gdb 控制台中输入以下内容:</p>

<pre><code>(gdb) info malloc-history 0x6e88d10
</code></pre>

<p>将 <code>0x6e88d10</code> 替换为导致崩溃的 <code>"message sent to deallocated instance"</code> 对象的地址,您将获得更有用的堆栈跟踪,它应该帮助您查明代码中导致问题的确切行。</p>

<p> <a href="http://www.codza.com/how-to-debug-exc_bad_access-on-iphone" rel="noreferrer noopener nofollow">See this article for more detailed instructions.</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 带有 NSFetchedResultsController 的 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/7845066/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/7845066/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 带有 NSFetchedResultsController 的 EXC_BAD_ACCESS