views:

630

answers:

1
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?

+3  A: 

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.

Jason Coco
The assertions pass, which is what is weirding me out. The assertions pass yet XCode is telling me the pointer is pointing at 0x0. If I get the size of the image it works fine.. it just doesn't seem to have the data associated with it. If I run it in the simulator the inspector shows me a valid address (not 0x0). On the device, though, it's not considering it nil (ie: assertions pass, and if I compare it to nil it comes back false)
Is it being retained?
Jason Coco
It is.. as I explained it you've answered the question correctly. Turns out it was a different issue, and the quirkiness in the debugger was throwing me off. I'm still not sure why the debugger was telling me the image was pointing to 0x0 in the device, and valid address in the sim. I debug wrote out the address and it definitely wasn't 0 Sounds like some weirdness going on with XCode. Either way, your answer is perfect for what I asked, and the assignment is in fact working just fine.