I have been having some problems with some variables inside a class being deleted lately.
Here is how I declare:
MyClass *myClass;
-(void)viewDidLoad {
myClass = [MyClass new];
//something using my class
then when I call a function later that uses an array and a dictionary inside the class.
[myClass doSomething];
Here is the code I get in the console.
-[CFArray objectAtIndex:]: message sent to deallocated instance 0x5d1f370
Here is how I declare the area in the Class
NSArray *myArray;
-(void)doSomething{
myArray = [NSArray array];
Then later I just perform a valid objectAtIndex on the array. Someone please help me.
Looks like you aren’t retaining the objects, so by the time you actually use them they are deallocated, since they are auto-released. Either use synthesized properties or add a retain message to each initialization, i.e.,
[[NSArray array] retain]and[[MyClass new] retain], be sure toreleasewhen appropriate. I recommend using properties instead though, it’s cleaner.EDIT: Thanks to imaginaryboy: the
[NSArray array]call releases an autoreleased object, so you have to retain that, by changing it to[NSArray new]for example. Leave[MyClass new]as it already returns a retained object.