iOS糟糕的访问无法找到

时间:2011-10-20 00:28:35

标签: iphone objective-c ios cocoa-touch exc-bad-access

由于这么多小时,我陷入了一次愚蠢的糟糕访问。我完全找不到它。 我希望你们中的一些人能够向我展示答案。 在下面的代码中,它显示在行上:NSString * stringCallVisit = [[NSString alloc]initWithFormat:..... 我无法理解,除参数theIntervention外,所有对象都是方法的本地对象。

如果我对方法NSString * stringCallVisit = [[NSString alloc]initWithFormat:...发表评论,即使我执行id obj = callVisit.injectionby;而不是,也不会出现错误访问;所以我认为不良访问不是来自callVisit对象,而是来自stringCallVisit对象。 但是为什么我只是在出现坏访问的时候实例化它。

感谢您的帮助,

-(NSString*)getCallVisitForIntervention:(Intervention*)theIntervention
{

NSManagedObjectContext *context = [iPad_TestAppDelegate mainContext];
NSError *error;

NSFetchRequest *requestCallVisit = [[NSFetchRequest alloc]init];
[requestCallVisit setEntity:[NSEntityDescription entityForName:@"CallVisit" inManagedObjectContext:context]];

NSPredicate *predicateInterventionID = [NSPredicate predicateWithFormat:@"intervention_id = %@",theIntervention.id];
[requestCallVisit setPredicate:predicateInterventionID];        

NSMutableArray *callVisits = [[context executeFetchRequest:requestCallVisit error:&error]mutableCopy];



NSString *xml  = @"<CallVisits>";

for(CallVisit *callVisit in callVisits) 
{

    NSString * stringCallVisit = [[NSString alloc]initWithFormat:
                                  @"<CallVisit>"
                                  "<id>%@</id>"
                                  "<injectionby>%@</injectionby>"
                                  "<injectionspot>%@</injectionspot>"
                                  "<intervention_id>%@</intervention_id>"
                                  "<fls>%d</fls>"
                                  "<weight>%d</weight>"
                                  "<height>%d</height>"
                                  "<painAtInjection>%d</painAtInjection>"
                                  "<created>%@</created>"
                                  "<siteReaction>%d</siteReaction>"
                                  "<technicalComplain>%d</technicalComplain>"
                                  "<field1>%d</field1>"
                                  "<field2>%d</field2>"
                                  "<riskCompliance>%d</riskCompliance>"
                                  "<reasonCompliance>%@</reasonCompliance>"
                                  "<placebo>%@</placebo>"
                                  "<needlereceived>%@</needlereceived>"
                                  "<compliance>%d</compliance>"
                                  "<psychologicalCondition>%d</psychologicalCondition>"
                                  "<keepsegment>%d</keepsegment>"
                                  "</CallVisit>",
                                  callVisit.id,
                                  callVisit.injectionby,
                                  callVisit.injectionspot,
                                  callVisit.intervention_id,
                                  [callVisit.fls doubleValue],
                                  [callVisit.weight doubleValue],
                                  [callVisit.height doubleValue],
                                  [callVisit.painAtInjection intValue],
                                  callVisit.created,
                                  [callVisit.siteReaction intValue],
                                  [callVisit.technicalComplain intValue],
                                  [callVisit.field1 intValue],
                                  [callVisit.field2 intValue],
                                  [callVisit.riskCompliance intValue],
                                  callVisit.reasonCompliance,
                                  callVisit.placebo,
                                  callVisit.needlereceived,
                                  [callVisit.compliance intValue],
                                  [callVisit.psychologicalCondition intValue],
                                  [callVisit.keepsegment intValue]];



    xml = [xml stringByAppendingString:stringCallVisit];
    [stringCallVisit release];

    id obj = callVisit;

}       
[callVisits release];
[requestCallVisit release];

xml = [xml stringByAppendingString:@"</CallVisits>"];

return xml;
}

修改

我做了你说的但是我无法理解日志,我没有收到信息malloc的地址。 我有点迷茫:))

修改 这是日志。但它很奇怪,但知道应用程序崩溃在另一个地方。

GuardMalloc[iPad Test-7405]: Failed to VM allocate 397648 bytes
GuardMalloc[iPad Test-7405]: Explicitly trapping into debugger!!!
sharedlibrary apply-load-rules all
Error in re-setting breakpoint 1:
Catchpoint 2 (throw)iPad Test(7405,0xaccab2c0) malloc: recording malloc stacks to disk using standard recorder
GuardMalloc[iPad Test-7405]: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
GuardMalloc[iPad Test-7405]: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
No memory available to program: call to malloc failed
Error in re-setting breakpoint 1:
Error in re-setting breakpoint 1:
GuardMalloc[iPad Test-7405]: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Current language:  auto; currently objective-c
GuardMalloc[iPad Test-7405]: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
No memory available to program: call to malloc failed
GuardMalloc[iPad Test-7405]: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
No memory available to program: call to malloc failed
GuardMalloc[iPad Test-7405]: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
No memory available to program: call to malloc failed
(gdb) 

4 个答案:

答案 0 :(得分:16)

在调试器中设置NSZombieEnabledMallocStackLoggingguard malloc。然后,当您的应用程序崩溃时,请在gdb控制台中输入:

(gdb) info malloc-history 0x543216

0x543216替换为导致崩溃的对象的地址,您将获得更有用的堆栈跟踪,它可以帮助您查明代码中导致问题的确切行。

See this article for more detailed instructions.


编辑:看起来你的内存可能已经不足了。您是否实施了didReceiveMemoryWarning方法?如果是这样,请在其中放入一个NSLog,以确定您是否是低内存。

答案 1 :(得分:2)

您使用的是XCode 3还是4?如果您使用3,我强烈建议升级到4。

如果您使用4,请按CMD-I在乐器中运行它,然后选择“Zombies”配置文件。然后通过您的应用程序执行到崩溃点,仪器将捕获它。您将获得带有链接的灰色弹出标注,单击它将带您进入分配历史记录的链接。最右边是调用模块,您可以在其中双击以查看分配了内存的源代码中突出显示的行。

我的怀疑是你的callVisits数组,它不是由init或copy方法返回的 - 你不应该显式释放,除非你使用init或copy / mutableCopy方法,或者除非你明确保留。

答案 2 :(得分:1)

我猜你的一个callVisit组件不是一个对象,而是一个标量(int,float,char等)。请注意,格式语句中命名的callVisit的每个组件都必须是一个有效的对象引用,给定它的编码方式。

答案 3 :(得分:1)

请注意,weight,height和fls是double值,但是你正在使用%d(整数)占位符。你会在那个xml中得到有趣的值。 - 岩浆45分钟前

我确认只需用%.2f替换%d即可解决问题!非常感谢您的帮助和时间!特别是他眼中的岩浆:) -