Reputation: 4835
I have got I have got two methods both in different classes. One is class method and other is instance method. i am calling class method from instance method. When instance method finishes it gives runtime error "EXC_BAD_ACCESS".
#import "xmlObject.h"
#import "textmeAppDelegate.h"
@implementation Class1
- (void)method1 {
textmeAppDelegate *del = (textmeAppDelegate *)[[UIApplication sharedApplication] delegate];
NSArray *bgColor = [[NSArray alloc] initWithArray:[xmlObject fetchImmediateChildrenValues:[del.navigationbarStyle objectForKey:@"backgroundcolor"]]];
UIColor *color = [UIColor colorWithRed:[[bgColor objectAtIndex:3] floatValue] green:[[bgColor objectAtIndex:2] floatValue] blue:[[bgColor objectAtIndex:1] floatValue] alpha:[[bgColor objectAtIndex:0] floatValue]];
CGContextSetFillColor(context, CGColorGetComponents([color CGColor]));
CGContextFillRect(context, rect);
[bgColor release];
}
@end
@implementation xmlObject
+ (NSArray *) fetchImmediateChildrenValues:(NSMutableDictionary *) node {
NSMutableDictionary *tmp = [[node objectForKey:@"children"] retain];
NSArray *keys = [[NSArray alloc] initWithArray:[tmp allKeys]];
keys = [keys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSMutableArray *pushArr = [[[NSMutableArray alloc] init] autorelease];
NSString *val = [[NSString alloc] init];
for(NSString *str in keys) {
val = (NSString *)[[tmp objectForKey:str] objectForKey:@"innertext"];
[pushArr addObject:val];
}
[val release];
[keys release];
return [NSArray arrayWithArray:pushArr];
}
@end
What is wrong with the code? Also app is crashing for this line of code application is crashing if i include this line
NSArray *bgColor = [[NSArray alloc] initWithArray:[xmlObject fetchImmediateChildrenValues:[del.navigationbarStyle objectForKey:@"backgroundcolor"]]];
If I remove it application runs smoothly.
Upvotes: 0
Views: 360
Reputation: 77291
You are releasing objects when you should not be.
In fetchImmediateChildrenValues you release val
, but it is not the same val
that you alloc'd, it is the val
returned inside the for loop there. Remove the alloc and release for val
.
///NSString *val = ...
for(NSString *str in keys) {
NSString* val = (NSString *)[[tmp objectForKey:str] objectForKey:@"innertext"];
[pushArr addObject:val];
}
///[val release];
You make the same error with keys. You initialize keys to something that you own (you alloc'd it) then you replace that (causing a memory leak) with something you don't own (it is autorelease), then you release keys which releases the autoreleased object. So you over release it.
Read the rules on memory management again. See here and here.
Upvotes: 1
Reputation: 36092
I believe the issue is related with
keys = [keys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
in the description of sortedArrayUsingSelector it says it returns references to the original array however you are effectively overwriting that with the assignment. To be on the safe side assign instead to a new variable (and avoid ev. mem leak)
Upvotes: 0