iOS糟糕的访问无法找到

Amn*_*sia 7 iphone cocoa-touch exc-bad-access objective-c ios

因为这么多个小时,我被困在一个愚蠢的坏访问.我完全找不到它.我希望你们中的一些人能够向我展示答案.在下面的代码中它出现在行上: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;
}
Run Code Online (Sandbox Code Playgroud)

编辑:

我做了你说的但我无法理解日志,我没有收到信息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) 
Run Code Online (Sandbox Code Playgroud)

cho*_*own 16

在调试器中设置NSZombieEnabled,MallocStackLoggingguard malloc.然后,当您的应用程序崩溃时,在gdb控制台中键入:

(gdb) info malloc-history 0x543216
Run Code Online (Sandbox Code Playgroud)

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

有关更详细的说明,请参阅此文章.


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


Amn*_*sia 1

请注意,体重、身高和 fls 是双精度值,但您却使用了 %d(整数)占位符。您将在该 xml 中获得有趣的值。\xe2\x80\x93 岩浆 45 分钟前

\n\n

我确认只需将 %d 替换为 %.2f 即可解决问题!非常感谢大家的帮助和时间!还有专门为他的眼睛准备的岩浆:) \xe2\x80\x93

\n