2

I have this method which gets called from a different class:

-(void)artworkInfo:(NSNumber *)pos{
    [image setImage:(UIImage *)[[mainDelegate.mapAnnotations objectAtIndex:(NSUInteger)pos]image]];
    [pos release];
}

I put debugger stop-points on all three lines. When debugging, just after the method is called and before setImage is called, hovering the mouse over pos in the method definition and in the setImage line shows the correct value which was sent from the method call. But, when I advance in the debugger and the next line [image setImage...] gets run hovering over both poss shows "Out of scope", and the app, therefore, does not display the image it should. Why does this happen?

EDIT: Well, it seems my issue may just be in how the method is called in the class, because even a hardcoded value is not producing the image in the method, but if I copy and paste that line into viewDidLoad:, it does. Why would that line work in viewDidLoad:, but not when it is called in the method it's in now?

jscs
  • 63,694
  • 13
  • 151
  • 195
Ryan
  • 570
  • 9
  • 25

3 Answers3

1

You shouldn't be releasing an object passed as parameter into a method, it's bad practice. And you should't cast a NSNumber object into a NSUInteger type.

You may want something like...

[pos intValue]//This will return the integer value of pos.
avizzini
  • 789
  • 1
  • 7
  • 17
  • Thank you, I think that is what I want. Unfortunately it seems that wasn't really the problem :/ I edited my question with what is still wrong. – Ryan May 10 '11 at 17:16
1

A few observations:

  1. You should always retain objects that you receive.
  2. You shouldn't be casting NSNumber to NSUInteger. Use [pos intValue] to get the integer value.
  3. You shouldn't be releasing pos in a situation like this unless you've explicitly retained it.
csano
  • 13,266
  • 2
  • 28
  • 45
  • What do you mean "You should always retain objects that you receive"? I don't see anything that should obviously be retained here (though the `release` is surely wrong as you say). – Chuck May 10 '11 at 17:13
  • It could be a case of personal preference, but if a method receives an object reference, the first thing I do is retain it. I then release it at the end of the method scope. – csano May 10 '11 at 17:49
  • To be honest, that sounds like needless inefficiency to me. If you ever actually need to do it, that's symptomatic of a larger problem with your memory management. It's certainly not normal — look at any Apple sample code or open-source code from major Cocoa developers and you'll see it's not done. – Chuck May 10 '11 at 20:09
  • It's possible you're correct. How would you determine what needs to be retained and what doesn't? – csano May 11 '11 at 18:45
0

Maybe that image inside mapAnnotations was freed before. I don't know why are you releasing pos, since it is a parameter so it has an implicit autorelease. Try removing that [pos release]

Ruben Marin
  • 1,639
  • 14
  • 24