Alessandro
Alessandro

Reputation: 4100

exc_bad_access (code=1, address=0x2012

I am getting this error:

exc_bad_access (code=1, address=0x2012 

when I try to receive a value from a singleton in this way:

Draggable* sharedSingleton = [Draggable sharedManagerDraggable];
NSLog(@"%@", sharedSingleton.namePassedToDraggable);

The code above is placed in the Draggable.m of type UIImageView, which also has:

+ (Draggable *)sharedManagerDraggable
{
static Draggable *sharedManagerDraggable = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    sharedManagerDraggable = [[Draggable alloc] init];
});
return sharedManagerDraggable;
}

I assign a value to namePassedToDraggable in this way (from view controller.m):

#import Draggable.h

Draggable* sharedSingleton = [Draggable sharedManagerDraggable];
    sharedSingleton.namePassedToDraggable = txt.text;
        NSLog(@"%@", sharedSingleton.namePassedToDraggable);
    dragger.tag = img.tag;

And in Draggable.h I have:

+ (Draggable *) sharedManagerDraggable;

@property (nonatomic, assign) NSString* namePassedToDraggable;

Why do I get the error causing the app to crash? I use the same method in other viewcontorllers and it works just fine!

Upvotes: 1

Views: 5440

Answers (1)

user529758
user529758

Reputation:

@property (nonatomic, assign) NSString* namePassedToDraggable;

should be

@property (nonatomic, strong) NSString* namePassedToDraggable;

If the property is not strong, it's not retained and it's released at the end of the scope of the function in which you're assigning it.

Upvotes: 11

Related Questions