Wednesday 28 August 2013

objective-c struct properties are released before usage

objective-c struct properties are released before usage

I have a struct in my Objective-C code like this >
typedef struct {
__unsafe_unretained NSString *string1;
__unsafe_unretained NSString *string2;
__unsafe_unretained NSString *string3;
__unsafe_unretained NSString *string4;
__unsafe_unretained NSString *string5;
..
..
..
__unsafe_unretained NSString *string10;
} MyStruct;
In my model class I store these structs in an array declared
@property (nonatomic, strong) NSMutableArray *myStructArray;
I construct this in my .m file during runtime like this
NSMutableArray *myTempArray = [NSMutableArray array];
for (NSDictionary *jsonDict in jsonArray)
{
MyStruct stringsStruct = parseWithDictionary(jsonDict);
[myTempArray addObject:[NSValue value:&stringsStruct
withObjCType:@encode(MyStruct)]];
}
myObject.myStructArray = myTempArray;
The problem is when I try to access this after parsing / constructing the
inside objects are already deallocated & I received the error
-[CFURL length]: message sent to deallocated instance
Here is how I access the properties later by the way,
MyStruct myStruct;
[[myObject.myStructArray objectAtIndex:someIndex] getValue:&myStruct];
NSLog(@"%@",myStruct.string1); // << bam! crash
How do I fix this ? Is there a way to make sure the objects objects remain
intact without deallocing until i'm done with it ? I'm using ARC & cannot
remove with __unsafe_unretained hence.

No comments:

Post a Comment