0

I'm having trouble accessing a variable from one method and get the error : EXC_BAD_ACCESS.

In MyClass.h I declared the var as such :

@interface ListeHorairesController : UITableViewController <UITableViewDelegate,   UIGestureRecognizerDelegate/*, NSXMLParserDelegate*/>
{
NSString *dateSelected;

...

Then in MyClass.m I try to use like so :

- (id)initWithStyle:(UITableViewStyle)style Donnees:(NSArray *)_data HeureDebut:(NSString *)_hDeb Date:(NSString *)_date
{
    self = [super initWithStyle:style];
    if (self){
        dateSelected = _date;
        ...

Then in another method :

-(void)effectuerMajListes:(NSDictionary *)dictRes
{


    NSDateFormatter *formatter_now;
    NSString        *dateString;

    formatter_now = [[NSDateFormatter alloc] init];
    [formatter_now setDateFormat:@"yyyy-MM-dd"];

    dateString = [formatter_now stringFromDate:[NSDate date]];

    [formatter_now release];

    //NSLog(@"%@", selected_date);

    if(![dateSelected isEqualToString:dateString]){ // <== ERROR POPS HERE

        NSLog(@"Not today : we won't refresh the view.");
        return;

    }
    ...

Can someone explain me why I've got this error, and how to do this properly?

Thanks.

I've tried using self.dateSelected self->dateSelected I've tried defining it as a class property, and using @synthetize onto it without success.

1
  • 1
    You should start with some memory managment tutorials - malloc, free, reference count, MRC, ARC... it's pretty important to know what those things mean. Commented Mar 17, 2014 at 12:33

2 Answers 2

1

As you are using MRC, you need to retain the value.

The error shows that object does not exist.

Use :

dateSelected = [_date retain];

since _date is a parameter and you only assigned it to darteSelected and it(_date) got released in the end of method.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot sir! I'll accept this answer when I can. Please tell me, to release it I'll have to use [_date release] at the end of the method right?
NO _date release is not required as you never alloc/retain/new/copied it. However in dealloc method you need to release dateSelected.
Also plz read memory management and now you have ARC, where all this are handled by compiler itself. then why not to take advantage of this new feature, but You must know the basic memory management.
1

It seems likely you're using manual memory management. Rather than fix this particular problem, you should use ARC (automatic retain counting) instead, which means you don't need to worry about these kinds of issues.

1 Comment

This is good idea, but MRC is must to know for at least till 2014. still many projects are running on MRC and you can't target for new projects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.