2

I have 2 view controllers connected with a segue (I will call these view controller 1 and view controller 2), where I pass an entity called Lead from view controller 1 to view controller 2 using the prepare. I use this same mechanism to pass this same lead to other view controllers and I also use view controller 2 from other view controllers and works fine.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    UIViewController *destinationVC = segue.destinationViewController;
    if([destinationVC isKindOfClass:[UINavigationController class]])
        destinationVC = ((UINavigationController *)destinationVC).topViewController;

    if([destinationVC respondsToSelector:@selector(setLead:)]) {
        [destinationVC performSelectorOnMainThread:@selector(setLead:) withObject:self.lead waitUntilDone:YES];
    }

However in this particular case, when from view controller 2, I try to access this lead property from anywhere (in this case first accessed in the viewDidLoad, I get EXC_BAD_ACCESS code=1 with (lldb) in the console and no other help whatsoever. Again, I use this same code in this view controller 2 from other view controller which also pass the Lead entity here and works fine.

if([self.lead.category isEqualToNumber:@(Buyer)])
    outputImage = [BackgroundGradient blueGradientWithRect:self.tableView.bounds];

Next thing I did was to create a new property... just copy and paste of the property declaration from lead to lead2 and pass the data to that one instead... that was it worked and I am getting to view controller 2 without a crash and am able to see and change the data no problem, however when I hit back to go back to view controller 1, I am getting another EXC_BAD_ACCESS on the @implementation line of view controller 2 with .ccx_destruct message in the trace and the app freezes.

I have no idea what's going on and it's driving me insane now. I've tried using the NSZombieEnabled and instruments however no luck. Any help would be greatly appreciated! I have been fighting with this issue for two days now. I am on iOS7 debugging on an iPhone 5s running with Xcode 5 on Mavericks. I am using Storyboards with ARC for a project which should also deploy to iOS6.

Thanks!

ChrisBorg
  • 1,418
  • 17
  • 27
  • the property (lead )which you are trying to access from viewDidLoad, is it retained ? – Kunal Balani Oct 27 '13 at 21:40
  • also why are you using performSelectorOnMainThread, prepareForSegue always has a main thread. – Kunal Balani Oct 27 '13 at 21:44
  • I am using arc and the property is declared with (nonatomic, strong) and also tried retain. When I print the description of the field I do get the address of the memory (however does not print the data of properties). – ChrisBorg Oct 27 '13 at 21:45
  • I have tried that line just in case but it was originally with the normal performselector:withobject: - thanks for your help – ChrisBorg Oct 27 '13 at 21:46
  • Did you tried overriding description method. seems to be lead points to garbage not real data. – Kunal Balani Oct 27 '13 at 21:46
  • could you add you viewDidLoad code where crash occurs. http://blog.manbolo.com/2012/01/23/xcode-tips-1-break-on-exceptions . This might help you. – Kunal Balani Oct 27 '13 at 21:48
  • you're right I am getting garbage... not sure what to do I should be passing it properly. btw this works fine on the simulators - this happens on devices – ChrisBorg Oct 27 '13 at 21:50

2 Answers2

0

Seems to be you have a shallow copy of property 'lead' which is causing crash. Make a deep copy and pass it to Viewcontroller 2.

Set

@property(nonatomic,copy)LeadObject *lead;

and implement copyWithZone for you LeadObject. Make sure its sub properties are copied as well. i.e, its sub properties should also have copyWithZone in order to make deep copy If Lead is retained that doesnt mean all its sub-properties are retained as well.

// EDIT : Using ARC is not equivalent to GC. You have to guide the ARC and indicate till what time you want to retain a object. Your objects are autoreleased before you access them and that time is different on phone and simulator.

Community
  • 1
  • 1
Kunal Balani
  • 4,739
  • 4
  • 36
  • 73
  • isn't this a bit dangerous? this is an nsmanagedobject and I am using this property to save back to core data. this started happening since I added support to iOS7 btw it was working with iOS6. this also doesn't happen when I access from another view controller. do you still suggest this method? thanks a lot! – ChrisBorg Oct 27 '13 at 22:02
  • No. NSManagedObject does not conform to the NSCopying protocol. If you want to create a new record with the same data, just insert a new object and assign the values from the first object to the second object. – Kunal Balani Oct 27 '13 at 22:04
  • Great thanks! This is happening when I pass the lead instance from core data that needs to be edited - view controller 2 is there to make the edit on the lead property that is causing the crash. it's strange that if I declare a new property with simply a different name (ex @property (nonatomic, strong) lead2) instead, it works perfectly. But then problem 2 arises like I said in the question above – ChrisBorg Oct 27 '13 at 22:06
  • it's as if view controller 2 is being released (or releasing it's objects) while it is still being in use – ChrisBorg Oct 27 '13 at 22:09
0

Solved it myself! Turns out that in such weired cases, it's best to Product>Analyze and fix everything there is flagged, including warnings!

Turns out that I was using a field in view controller 2 which had a particular name - apparently the iOS7 SDK added this field name that I was using as part of the SDK and I was overriding it without actually knowing it - so who knows what I was actually doing. Anyway, renamed and field and problem solved. Took me 2 days to get there :)

ChrisBorg
  • 1,418
  • 17
  • 27