2

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 !!!

2 Answers2

1

You have got a memory management bug somewhere in your code. I don't know where the bug is, it almost certainly isn't in the code you posted (it all looks perfect to me).

Unless you post more code, we can't help you fix this one.

I strongly recommend you enable ARC. It's fairly new, but nowadays it's old enough everyone should start using it.

There's a very good chance your bug will simply go away if you turn ARC on.

Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110
  • I tried do enable ARC... but I'm using SBJson, and it shows a lot of errors when compiling... – Wagner Vieira Dec 18 '11 at 21:06
  • You can disable ARC for all of the `*.m` files in SBJson by adding the `-fno-objc-arc` compiler flag: http://stackoverflow.com/questions/6646052/how-can-i-disable-arc-for-a-single-file-in-a-project – Abhi Beckert Dec 18 '11 at 21:22
0

The code you posted looks fine, but somewhere else you must be over-releasing mycar. Profile it with the "zombies" instrument, and it will tell you where it is being released.

whooops
  • 935
  • 6
  • 11