Jonathan
Jonathan

Reputation:

Simple assignment not working with UIImage* / imageNamed

UIImage* test = [UIImage imageNamed:@"test.png"];
self.image_in_controller = test;

Later in the code when image_in_controller is used, I get EXC_BAD_ACCESS.

I set a break point around the time of the assignment. The variable test is getting set just fine.. after the assignment to selfimage_in_controller, test is still okay, but image_in_controller points to 0x0 (not nil).

If I run the same code in the simulator it works fine (self.image_in_controller has a valid point address).

Any ideas?

Upvotes: 0

Views: 1302

Answers (1)

Jason Coco
Jason Coco

Reputation: 78353

Is the property image_in_controller a retained property? If not, you will have to explicitly take ownership of the image with a retain message. So one of either:

@property(retain) UIImage* image_in_controller;

or

self.image_in_controller = [test retain];

should exist. The EXC_BAD_ACCESS is often caused by using an object that's been destroyed. Also, test to make sure that test is not actually nil. You can do this with an assertion:

NSParameterAssert(test);

just after test is assigned. It will let you know if UIImage is not returning a valid object for some reason on the device.

Finally, 0x0 is the memory address of nil, so you will often see that in the debugger and can (for all intents and purposes) be considered the same as nil, Nil, NULL and 0.

Upvotes: 3

Related Questions