0
votes

What I am doing is

    //ClassB.h
    @property (strong, nonatomic) NSString  *name;

and

 //ClassA.h
 @interface ClassA : NSObject
 +(ClassA*)methodA:(NSData*)data;
 -(id)initWithData:(NSData*)data;

    @property (nonatomic, strong) NSMutableArray *arr; 
    @property (nonatomic, strong) RXMLElement    *rxmlRoot; 

    @end

    //ClassA.m
    @implementation ClassA
    @synthesize arr;
    @synthesize rxmlRoot;

    +(ClassA*)methodA:(NSData*)data {
        return [[ClassA alloc] initWithData:data];
    }

    -(id)initWithData:(NSData*)data {
        self = [super init];
        if (self) {
            arr      = [NSMutableArray array];
            rxmlRoot = [RXMLElement elementFromXMLData:data];

             /*****edit : just been added to make codes clear*****/  
            NSString    *node   =   @"players.player";
            [rxmlRoot iterate:node with:^(RXMLElement *e){
                 ClassB   *classB  =   [[[ClassB alloc] init] autorelease];
                 [classB setName:       [e attribute:@"name"]];

                 // adding ClassB into arr
                 [arr addObject:classB];
            }];

        }
        return self;
    }
    @end

So now I am having ClassA object whose arr contains ClassB

Question : later on, when I try to access an particular property of ClassB like

((ClassB*)[classA.arr objectAtIndex:0]).name

and I am getting EXC_BAD_ACCESS at above line..

Please advice me on this issue and how to correct the error. Any comments are welcomed here

Thanks

3

3 Answers

1
votes

This line

[arr addObject:ClassB]

makes no sense. Is your intention to put an instance of ClassB into that array, or the class itself (i.e. [ClassB class])? Presumably you must intend to put an instance of ClassB in there, otherwise trying to access its properties later on (e.g. firstName) would make no sense. Also, does your ClassB even have a firstName property, because the piece of ClassB's interface that you show us only mentions a name property.

Update:

Since you are using manual memory management, you need to retain the objects (arr, rxmlRoot) you create in your initializer using convenience constructors, which return autoreleased objects. For example, the code should be

arr = [[NSMutableArray array] retain];
0
votes

Post your ClassB.m . Are you making the @synthesize name?

Also make the Alloc for arr.

This line is so wrong:

((ClassB*)[classA.arr objectAtIndex:0]).firstName

Your string is called name , not firstName. It should be :

((ClassB*)[classA.arr objectAtIndex:0]).name
0
votes

The code in the question has changed substantially, so my previous answer now makes no sense and I have removed it. Given revised code, the first thing to do is to log what's going on.

NSLog(@"classA: %@", classA);
NSLog(@"classA.arr: %@", classA.arr);

((ClassB*)[classA.arr objectAtIndex:0]).name

If it blows up on the first log statement, things are really bad. But then at least you know that classA is pointing to something rotten and you can work back from there.

You can achieve the same thing in the debugger, by setting a break point ahead of the line and inspecting. Given that you are getting an EXC_BAD_ACCESS one of the pointers is pointing to a dodgy object, e.g. one that has been released. It looks as if you are using ARC (because you have strong in your property), which should help manage the memory - but then again, you have an autorelease in there, so maybe not.