In my app, I have:
car.h
@interface car : NSObject
{
NSString *model;
NSString *price;
// others atributes
}
@property(nonatomic, retain) NSString *model;
@property(nonatomic, retain) NSString *price;
...
myshop.h
#import "car.h"
@interface myshop : UIViewController...
{
car *mycar;
}
@property(nonatomic, retain) car *mycar;
...
myshop.m
...
-(void) viewDidLoad
{
...
mycar = [[car alloc] init];
}
so, I have a method that shows a popover, where I can select a car from a tableview. This popover callback a method in the myshop.m, using delegate, where I assign a value to mycar.model, and call the method doA above, all of this works fine, and shows the value of mycar.model in Output:
-(void) doA
{
NSLog(@"car = %@", mycar.model );
...
}
But... now it is the problem: I have a buttom in the myshop view. When I press this button, the action shows an alert view (there is the delegate in .h). The return of this alert calls:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self doA];
...
}
And the program crash when doA tries to write mycar.model to Output. No error is showing in Output View of Xcode. In the code, it shows: Thread 1: Program received signal: EXEC_BAD_ACCESS
.
I can show as many cars I want from popover view, but when press the button, and the AlertView closes, the program crashes.
Just for test, I call [self doA] in other methods that runs from another class via delegate and always the app craches.
Any idea, what is wrong?
Completing the code
Is there any error here? (this is a method of car.m)
- (void) setValues: (NSDictionary *) data
{
model = [data objectForKey:@"model"];
price = [data objectForKey:@"price"];
...
I don't initialize the properties (model, price, ...) anywhere in code.
SOLVED !!!
It was a memory management problem!
I change
model = [data objectForKey:@"model"];
by
model = [[NSString alloc] initWithFormat:@"%@", [data objectForKey:@"model"]];
I hope that I am right now! At least the app seens to work well!
Thanks a lot friends !!!