NSMutableArray EXC_BAD_ACCESS when i try read from it - objective-c

I want update position of Particles for store this information I use NSMutableArray. I want do update 1 time per 2 seconds for example. For this I try use this code.
//ParticlesLayer.h
#interface ParticlesLayer : CCLayer {
CCQuadParticleSystem *particle;
NSMutableArray *partilesCoordinates;
}
#property (assign, readwrite) NSMutableArray *partilesCoordinates;
-(void)partilesMove:(ccTime)dt;
-(void)particleInit;
//ParticlesLayer.m
#implementation ParticlesLayer
#synthesize partilesCoordinates;
-(id) init {
self = [super init];
partilesCoordinates = [NSMutableArray arrayWithCapacity:5];
[partilesCoordinates addObject: [NSValue valueWithCGPoint:ccp(67,152)]];
[partilesCoordinates addObject: [NSValue valueWithCGPoint:ccp(140,147)]];
[partilesCoordinates addObject: [NSValue valueWithCGPoint:ccp(198,100)]];
[partilesCoordinates addObject: [NSValue valueWithCGPoint:ccp(261,126)]];
[partilesCoordinates addObject: [NSValue valueWithCGPoint:ccp(364,135)]];
// bla bla bla
//...
//bla bla bla
[self schedule:#selector(partilesMove:) interval:2];
return self;
}
-(void)emittersMove:(ccTime)dt{
NSMutableArray *newParticlesCoordinates = [NSMutableArray arrayWithCapacity:5];
CGPoint newPoint;
for (int i = 0; i < 5; ++i) {
[self particleInit];
NSLog(#"%#", [particlesCoordinates objectAtIndex:i]); // on this part of code i have problem
particle.position = [[particlesCoordinates objectAtIndex:i] CGPointValue];
[self addChild:emitter];
newPoint = emitter.position;
newPoint.x += 15;
newPoint.y += 15;
[newParticlesCoordinates addObject:[NSValue valueWithCGPoint:newPoint]];
}
[partilesCoordinates removeAllObjects];
[partilesCoordinates newParticlesCoordinates];
[newParticlesCoordinates removeAllObjects];
}
But when i run it on first calling function and reading from particlesCoordinates array i have console message : Program received signal: "EXC_BAD_ACCESS". How I can make normal-working schedule?

[NSMutableArray arrayWithCapacity:5] returns an autoreleased instance, and that gets cleaned up some time after leaving the initializer. Now the pointer partilesCoordinates points to undefined memory. You need to do:
partilesCoordinates = [[NSMutableArray arrayWithCapacity:5] retain];
or
partilesCoordinates = [[NSMutableArray alloc] initWithCapacity:5];

Change this:
#property (assign, readwrite) NSMutableArray *partilesCoordinates;
to this:
#property (nonatomic, retain) NSMutableArray *partilesCoordinates;
and change this:
partilesCoordinates = [NSMutableArray arrayWithCapacity:5];
to:
self.partilesCoordinates = [NSMutableArray arrayWithCapacity:5];
When you synthesize a setter for an "assign" property, the accessor won't retain the value you pass it.

Related

objective-c multi-dimensional array

I want to use a variable-sized multi-dimensional array in my app to save data. The data structure I want to use is as below, the first element in each row is followed by corresponding multiple values.
array = { {a, a_val1, a_val2, a_val3}.
{b, b_val1},
{c, c_val1, c_val2, c_val3, c_val4, c_val5}
}
Any idea how I can implement in objective-c?
use NSMutableArray like so
NSMutableArray *curRow; /* use to access the row while loading with objects */
NSMutableArray *array = [[NSMutableArray alloc] init]; /* your main multidim array */
curRow = [NSMutableArray array];
[curRow addObject:/* what you want here */];
[curRow addObject:/* what you want here */];
[curRow addObject:/* what you want here */];
[array addObject:curRow]; /* first row is added */
/* rinse and repeat */
curRow = [NSMutableArray array];
[curRow addObject:/* what you want here */];
[curRow addObject:/* what you want here */];
[curRow addObject:/* what you want here */];
[array addObject:curRow];
use NSMutableArray
Below is the example for your understanding ...
NSMutableArray * multiArray = [[NSMutableArray alloc] initWithCapacity:5];
[multiArray addObject:[NSArray arrayWithObjects:a,a_val1,a_val2]];
[multiArray addObject:[NSArray arrayWithObjects:a,a_val1,a_val2,a_val3,a_val4]];
[multiArray addObject:[NSArray arrayWithObjects:a,a_val1,a_val5]];
[multiArray addObject:[NSArray arrayWithObjects:a,a_val1,a_val2,a_val3,a_val4,a_val5,a_val6]];
And Don't forget to release to multiArray array because we have alloced it ...
Objective-C does not have a real 2 dimensional array type but you can implement it with the
following codes..
in your header file --- yourheader.h
#import <Foundation/Foundation.h>
#interface yourheader : NSObject{
NSMutableDictionary* DictionaryArrayType;
NSMutableArray* MultiArrayType;
NSArray* CaptionTitle;
NSArray* ObjectValue;
}
#property (strong, nonatomic) NSMutableDictionary* DictionaryArrayType;
#property (strong, nonatomic) NSArray* CaptionTitle;
#property (strong, nonatomic) NSArray* ObjectValue;
#property (strong, nonatomic) NSMutableArray* MultiArrayType;
-(id) AddArrayObjects:(NSString*)_Name : (NSString*)_Surname :(NSString*)_Age;
-(id) AddArrayDictionaryObject:(NSArray*)_ArrayObject : (NSArray*)_ArrayKey;
-(id) AddMultiArrayType:(id)_ArrayObject;
-(void) ShowMultiArrayType:(id)_ArrayObject;
#end
Now add to your objective-c file ---- objective-c.m
#import "yourheader.h"
#implimentation yourheader
#synthesize DictionaryArrayType;
#synthesize CaptionTitle;
#synthesize ObjectValue;
#synthesize MultiArrayType;
-(id)init {
if(self = [super init]){
NSString* const NAME = #"NAME";
NSString* const SURNAME = #"SURNAME";
NSString* const AGE = #"AGE";
//Adding fixed content to CaptionTitle Array
[self setCaptionTitle:[NSArray arrayWithObjects:NAME, SURNAME, AGE, nil]];
//add values to ObjectValue array
[self AddArrayObjects:#"Bob" :#"Obi" :#"200"];
//add values to dictionary
[self AddDictionaryArrayType:ObjectValue :CaptionTitle];
//Add to the Multi dimensional array [][]
[self AddMultiArrayType:DictionaryArrayType];
//add the second row values to ObjectValue array
[self AddArrayObjects:#"Barack" :#"Obama" :#"50"];
//add values to dictionary
[self AddDictionaryArrayType:ObjectValue :CaptionTitle];
//Add to the Multi dimensional array [][]
[self AddMultiArrayType:DictionaryArrayType];
//display the 2d Array
[self ShowMultiArrayType:MultiArrayType];
}
return self;
}
-(id)AddArrayObjects:(NSString *)_name :(NSString *)_surname :(NSString *)_age {
//Set the Array Objects;
[self setObjectValue:[NSArray arrayWithObjects:_name, _surname, _age, nil]];
return self;
}
-(id)AddDictionaryArrayType:(NSArray *)_ArrayObject :(NSArray*)_ArrayKey {
if(!DictionaryArrayType) {
//initialize disctionary
[self setDictionaryArrayType:[NSMutableDictionary dictionary]];
}
//add array obeject and Fixed Key decleared in CaptionTitle array
DictionaryArrayType = [NSMutableDictionary dictionaryWithObjects:_ArrayObject forKeys:_ArrayKey];
return self;
}
-(id) AddMultiArrayType:(id)_ArrayObject {
if(!MultiArrayType) {
[self setMultiArrayType:[NSMutableArray array]];
}
[MultiArrayType addObject:_ArrayObject];
return self;
}
-(void)ShowMultiArrayType:_ArrayObject {
for(id objects in _ArrayObject ) {
for(id key in objects) {
NSLog(#"%# key = : object = %#", key, [objects objectForKey:key]);
}
}
}
#end;
To finish add this to your appdelegate.m file inside the app
yourclassname* _yourclasspointer = [[yourclassname alloc] init];
[_youclasspointer ShowMultiArrayType:[_yourclasspointer MultiArrayType]];
You should see it in you console.

NSArray "out of scope"

.h
#property (nonatomic, retain) NSArray *m_plistData;
.m
#synthesize m_plistData;
- (void)viewDidLoad
{
NSArray *array = [[NSArray alloc]initWithObjects:#"0",#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14", nil];
m_plistData = array;
NSArray *nn = m_plistData;
[super viewDidLoad];
}
I use breakpoint and found array is normal,but m_plistData has no values,shows "out of scope",I can't understand why nn can get normal values
array is a local reference variable. It just lasts until viewDidLoad method. Now,
m_plistData = array;
The above statement doesn't make a deep copy. It is just a shallow copy.
- (void)viewDidLoad
{
NSArray *array = [[NSArray alloc]initWithObjects:#"0",#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14", nil];
m_plistData = array;
NSArray *nn = m_plistData;
[super viewDidLoad];
} // Both the array, nn references cease to exist after this point.
// So having references to it leads to run-time exception if used else where.
If your objective is have elements in m_plistData, directly do -
m_plistData = [[NSArray alloc]initWithObjects:#"0",#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14", nil];
Try something like:
m_plistData = [array copy];
Do it like this as you have a NSAarray:
if(array)
{
m_plistData = [NSArray arrayWithArray:array];
}

Issue accessing member property of a class that is a property of another class

Here's the first class:
#interface ClassA : NSObject {
NSMutableArray* array1;
NSArray* array2;
}
#property (nonatomic, assign)NSMutableArray* array1;
#property (nonatomic, assign)NSArray* array2;
-(id) initWithData:(NSString*)string1 andWords:(NSString*)string2;
#end
#implementation ClassA
#synthesize array1;
#synthesize array2;
-(id) initWithData:(NSString*)string1 andWords:(NSString*)string2
{
if ((self = [super init]))
{
array1 = [[NSMutableArray alloc] initWithCapacity:[string1 length]];
for (int i=0; i < [array1 length]; i++)
[array1 addObject:[NSString stringWithFormat:#"%c", [string1 characterAtIndex:i]]];
array2 = [string2 componentsSeparatedByString:#";"];
NSLog(#"array1 = %#", array1);
NSLog(#"array2 = %#", array2);
}
return self;
}
- (void) dealloc
{
[array1 release];
[array2 release];
[super dealloc];
}
#end
Here's 2nd Class:
#interface ClassB : NSObject {
ClassA* classA;
}
#property (nonatomic, assign)ClassA* classA;
Im having issues gaining access to the properties of ClassA, that are stored in ClassB.
For instance, I want to do something like:
// print out the arrays stored in Class B
NSLog(#"%#",[[classB classA] array1]);
NSLog(#"%#",[[classB classA] array2]);
array1 prints out okay, but array2 throws and EXC_BAD_ACCESS error. I'm guessing because
array2 = [string2 componentsSeparatedByString:#";"];
is not allocating properly.
It's much easier to let Apple handle the memory management for you: change your array1 and array2 properties from assign to retain and then use the following in your init:
NSMutableArray* newArray1 = [NSMutableArray arrayWithCapacity:[string1 length]];
for (int i=0; i < [array1 length]; i++)
[array1 addObject:[NSString stringWithFormat:#"%c", [string1 characterAtIndex:i]]];
[self setArray1:newArray1];
[self setArray2:[string2 componentsSeparatedByString:#";"]];
This way, your #synthesized setter methods will handle retaining the properties for you.
I believe that array2 is being autoreleased because you are creating it with one of NSString's convenience methods rather then explicitly allocating the array. Try retaining the results instead of just setting your iVar to the autoreleased string.
I resolved this issue by first calling alloc:
array2 = [NSArray alloc];
array2 = [string2 componentsSeparatedByString:#";"];

iOS - Storing groups of UILabels into a NSMutableArray

I'm creating UILabels dynamically in a for each loop. Every loop that is run creates 1-4 UILabels.
What I want is that I put these UILabels into my NSMutableArray and being able later to easy retrieve the data.
My original thought was to put these UILabels into a NSDictionary and use [dictGroupLabels setValue:uiLabel1 forKey:#"uiLabel1"] and then [dictGroupLabels setValue:uiLabel2 forKey:#"uiLabel2"] and so on. And then put this dictionary into my NSMutableArray for each loop. Later on I could access the values like UILabel *label = [[myArray objectAtIndex:0] valueForKey:#"uiLabel1"] BUT that unfortunately doesn't work since UILabels don't conform to the NSCopying protocol.
So with this in mind how would you solve this?
this question provided more information on what you are trying to accomplish. Since you know for a fact, the possible set of labels you are trying to create in each case, I would highly recommend using mutable dictionaries instead of arrays.
To illustrate, given the following hypothetical class definition:
#interface MyClass: NSObject {
NSMutableDictionary * _labelDict;
}
#property (nonatomic, retain) NSMutableDictionary * labelDict;
- ( void )methodA;
- ( void )methodB;
- (NSMutableDictionary *) labelsForRunLoop: (NSUInteger) loopIdx;
#end
You would have the following, hypothetical, class implementation:
#implementation MyClass
#synthesize labelDict = _labelDict;
- ( id ) init {
if( ( self = [ super init ] ) ) {
[self setLabelDict: [NSMutableDictionary dictionaryWithCapacity: 8]];
}
}
- ( void ) dealloc {
[ self.labelDict release ];
[ super dealloc ];
}
- ( void ) methodA {
for(NSUInteger i = 0; i < some index; i++) {
[self.labelDict setObject: [self labelsForRunLoop: i] forKey: [NSString stringWithFormat: #"%d", i]];
}
}
- ( void ) methodB {
// Locate the label you need to work with. Example based on this crude pseudo code
NSMutableDictionary * subDict = (NSMutableDictionary *) [self.labelDict objectForKey: #"0"];
UILabel * theLabel = (UILabel * ) [subDict objectForKey: #"UILabel.Z"];
theLabel.text = #"Label 1";
}
- (NSMutableDictionary *) labelsForRunLoop: (NSUInteger) loopIdx {
NSMutableDictionary * dictionary = [NSMutableDictionary dictionaryWithCapacity: 4] ;
[dictionary setObject: create-w-label forKey: #"UILabel.W"];
[dictionary setObject: create-x-label forKey: #"UILabel.X"];
[dictionary setObject: create-y-label forKey: #"UILabel.Y"];
[dictionary setObject: create-z-label forKey: #"UILabel.Z"];
return [dictionary retain];
}
#end
This is basically pseudo code and will not successfully compile. However it will serve as a good starting point. You probably want to store each label dictionary under some key that makes sense, instead of just using the loop's index. Hope this helps.
They don’t need to adhere to NSCopying to be added to an array. It sounds like you just need to do something like this:
NSMutableArray *mainArray = [NSMutableArray array];
for(int i = 0; i < 5; i++)
{
NSMutableArray *subArray = [[NSMutableArray alloc] initWithCapacity:5];
for(int j = 0; j < 4; j++)
{
UILabel *label = [[UILabel alloc] init];
// etc.
[subArray addObject:label];
[label release];
}
[mainArray addObject:subArray];
[subArray release];
}
// then, to get one of the labels:
UILabel *someSpecificLabel = [[mainArray objectAtIndex:2] objectAtIndex:1];

Storing Sorted Arrays Causing EXC_BAD_ACCESS Error

Given a basic key/value array, I'm wanting to store two sorted arrays based on the original array: one array will be sorted by name, and the other by age.
The arrays seem to be sorting correctly when I output them to the log; however, when I try to access them elsewhere in the code, I'm receiving a EXC_BAD_ACCESS error.
Here's what I have so far:
// MyController.h
#interface MyController : UIViewController {
NSMutableArray *originalArray;
NSMutableArray *nameArray;
NSMutableArray *ageArray;
}
#property (nonatomic, retain) NSMutableArray *originalArray;
#property (nonatomic, retain) NSMutableArray *nameArray;
#property (nonatomic, retain) NSMutableArray *ageArray;
-(void)someRandomMethod;
#end
// MyController.m
#import "MyController.h"
#implementation MyController
#synthesize originalArray;
#synthesize nameArray;
#synthesize ageArray;
-(void)viewDidLoad {
// originalArray = (
// {
// "name" = "Sally";
// "age" = 18;
// },
// {
// "name" = "Chad";
// "age" = 26;
// },
// {
// "name" = "Carla";
// "age" = 24;
// },
// )
// sort by name
NSSortDescriptor *sortByNameDescriptor;
sortByNameDescriptor = [[[NSSortDescriptor alloc]
initWithKey:#"name"
ascending:NO] autorelease];
NSArray *sortByNameDescriptors = [NSArray arrayWithObject:sortByNameDescriptor];
nameArray = [originalArray sortedArrayUsingDescriptors:sortByNameDescriptors];
// sort by age
NSSortDescriptor *sortByAgeDescriptor;
sortByAgeDescriptor = [[[NSSortDescriptor alloc]
initWithKey:#"age"
ascending:NO] autorelease];
NSArray *sortAgeDescriptors = [NSArray arrayWithObject:sortByAgeDescriptor];
ageArray = [originalArray sortedArrayUsingDescriptors:sortByAgeDescriptors];
[super viewDidLoad];
}
-(void)someRandomMethod {
// whenever I try to access the sorted arrays, I receive the EXC_BAD_ACCESS error
[[nameArray objectAtIndex:0] valueForKey:#"name"];
[[ageArray objectAtIndex:0] valueForKey:#"age"];
}
-(void)viewDidUnload {
self.originalArray = nil;
self.nameArray = nil;
self.ageArray = nil;
[super viewDidUnload];
}
- (void)dealloc {
[originalArray release];
[nameArray release];
[ageArray release];
[super dealloc];
}
#end
Any ideas?
UPDATE: Thanks to #robin, by changing the code above to the code below, everything works great:
// sort by name
NSSortDescriptor *sortByNameDescriptor;
sortByNameDescriptor = [[[NSSortDescriptor alloc]
initWithKey:#"name"
ascending:NO] autorelease];
NSArray *sortByNameDescriptors = [NSArray arrayWithObject:sortByNameDescriptor];
nameArray = [[NSMutableArray alloc] initWithArray:[originalArray sortedArrayUsingDescriptors:sortByNameDescriptors]];
// sort by age
NSSortDescriptor *sortByAgeDescriptor;
sortByAgeDescriptor = [[[NSSortDescriptor alloc]
initWithKey:#"age"
ascending:NO] autorelease];
NSArray *sortAgeDescriptors = [NSArray arrayWithObject:sortByAgeDescriptor];
ageArray = [[NSMutableArray alloc] initWithArray:[originalArray sortedArrayUsingDescriptors:sortByAgeDescriptors]];
I dont think you know about this or not but when ever you create an object like string or array or dictionary, with init methods then the retain count gets incremented by 1
and if you create them like this
NSArray *anarray = [NSArray arrayWithArray:temp];
this will create an autorelease objects that will be released automatically after sometime.
So my advice don't use this type of code if you want to use the objects in more than 1 function. Always use init methods first to get the work done.
and if you are sure that the objects are not needed for the rest of the program than release them using release methode.