views:

43

answers:

2

Hi,

When i try to initialize an object inside the tableView:cellForRowAtIndexPath the simulator crashes. The code i used is

Claimant *tempClaimant = [[Claimant alloc] init];
tempClaimant = [appDelegate.arrRetailClaims objectAtIndex:0];
NSLog(@"Claimant Name is: %@",tempClaimant.ClaimantName);

Is there anything wrong with this. The same code works fine when written in a viewDidLoad method. Please help..

Thanks in advance, Shibin

+3  A: 

First of all, there is no reason to allocate and initialize a new object if you just need a reference to an existing object that is in an array:

//Claimant *tempClaimant = [[Claimant alloc] init];
Claimant *tempClaimant = [appDelegate.arrRetailClaims objectAtIndex:0];

Next, make sure that the array and object exist as expected:

NSLog(@"array = %@ %d",appDelegate.arrRetailClaims,[appDelegate.arrRetailClaims count]);
NSLog(@"tempClaimant = %@",tempClaimant);
NSLog(@"Claimant Name is: %@",tempClaimant.ClaimantName);
gerry3
2010-02-12 17:36:57.414 VitalPoint[32667:20b] array = ( <Claimant: 0xd7da20>, <Claimant: 0xd80e20>) 22010-02-12 17:36:57.414 VitalPoint[32667:20b] tempClaimant = <Claimant: 0xd7da20>This is the output, and aftr it crashes...!!!
Shibin Moideen
when i try to nslog the attribute in the tempClaimant, it crashes.. Y???
Shibin Moideen
What is the crash you are getting? As leson suggests, it clearly has to do with the access of the property. Can you show how the string object is being allocated and assigned as the "ClaimantName" property?
gerry3
But the same code works fine in the viewDidLoad method. its having error when i try to implement it in the tableView:cellForRowAtIndexPath:
Shibin Moideen
You still haven't said what error you are seeing. If it's a memory error (EXEC_BAD_ACCESS) then probably you are not retaining one of the objects properly (the array or the name property). It would be difficult to give you further advice without more info. If it is an over-release memory error, you can try turning on `NSZombieEnabled` (search the net for details/tutorials).
gerry3
[Session started at 2010-02-15 11:02:40 +0530.]GNU gdb 6.3.50-20050815 (Apple version gdb-967) (Tue Jul 14 02:11:58 UTC 2009)Copyright 2004 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License, and you arewelcome to change it and/or distribute copies of it under certain conditions.Type "show copying" to see the conditions.There is absolutely no warranty for GDB. Type "show warranty" for details.This GDB was configured as "i386-apple-darwin".sharedlibrary apply-load-rules allAttaching to process 4510.This is what i get. it simply crashes...
Shibin Moideen
Also the property is @property(nonatomic, retain) NSString claimantName;And its same for all instance variables.
Shibin Moideen
Are you accessing the ivars as properties though? You have to use "self.propertyName" instead of "propertyName" if you want to get the benefit of the retain. Be careful not to over-retain the array if you are using alloc for it. Have you run the static analyzer? Have you set a breakpoint near the above code as well as added the suggested NSLog statements? If you want help, you must provide more info.
gerry3
A: 

It's possible that the property hasn't been allocated or has been released at some point to that you are messaging a un/deallocated instance.

leson
But the same code works fine in the viewDidLoad method. its having error when i try to implement it in the tableView:cellForRowAtIndexPath:
Shibin Moideen