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?

3 Answers 3

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. 
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

4 Comments

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).
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.
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.
It's possible you're correct. How would you determine what needs to be retained and what doesn't?
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]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.