iOS bad access impossible to find

Amnysia picture Amnysia · Oct 20, 2011 · Viewed 11.1k times · Source

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) 

Answer

chown picture chown · Oct 20, 2011

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.