iOS bad access impossible to find

泪湿孤枕 提交于 2019-11-29 15:05:23

问题


I'm stuck with a stupid bad access since so many hours. I'm totally unable to find it. I hope some of you will be able to show me the answer into the light. In the code bellow it appear on the line : NSString * stringCallVisit = [[NSString alloc]initWithFormat:..... I can't understand, all the objects are local of the method except the parameter theIntervention.

if i comment the method NSString * stringCallVisit = [[NSString alloc]initWithFormat:... the bad access don't appear even if i do id obj = callVisit.injectionby; instead of; So i suppose that the bad access is not from the callVisit object but certainly from the stringCallVisit object. But why i'm just instantiate it on the tine where the bad access appear.

Thanks for your help,

-(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;
}

EDIT:

I did what you said but i'mm unable to understand the log and i don't received an adress to to the info malloc. I'm a bit lost :)

EDIT Here is the log. But it,s strange but know the app crash at an other place.

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) 

回答1:


Set NSZombieEnabled, MallocStackLogging, and guard malloc in the debugger. Then, when your App crashes, type this in the gdb console:

(gdb) info malloc-history 0x543216

Replace 0x543216 with the address of the object that caused the crash, and you will get a much more useful stack trace and it should help you pinpoint the exact line in your code that is causing the problem.

See this article for more detailed instructions.


Edit: It looks like you might be running out of memory. Do you have a didReceiveMemoryWarning method implemented? If so, put a NSLog in it to find out if you are lowon memory.




回答2:


Are you using XCode 3 or 4? If you are using 3, I HIGHLY recommend upgrading to 4.

If you are using 4, press CMD-I to run it in Instruments, and select the "Zombies" profile. Then go through your app execution to the crash point, and Instruments will catch it. You will get a grey popup callout with a link, click the link it will take you to the allocation history. On the far right is the calling module, where you can double-click to see the highlighted line in the source code where the memory was allocated.

My suspicion is your callVisits array, which is returned not by an init or copy method - you should not explicitly release unless you are using init or copy/mutableCopy methods, or unless you explicitly retain.




回答3:


I'm guessing that one of your callVisit components isn't an object, but a scalar (int, float, char, etc). Note that EVERY component of callVisit named in the format statement must be a valid object reference, given the way it's coded.




回答4:


note that weight, height and fls are double values, and yet you're using a %d (integer) placeholder for them. you're going to get funny values in that xml. – magma 45 mins ago

I confirm Just replace %d by %.2f solve the problem ! Big thanks all for your help and your time ! And specially magma for his eyes :) –



来源:https://stackoverflow.com/questions/7829808/ios-bad-access-impossible-to-find

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!