views:

67

answers:

1

I have an error with NSString. In one of my .m files I have a member called "conversions" of type NSString and within a method of that class the string is manipulated and added on to and etc. Well, when I try to Log that string in a different method that is later called, for some reason the string is printing as a UITouch object. Specifically, I try to log the string as follows

NSLog(@"%@", conversions);

and in my console it prints

<UITouch 0x131700> phase: Ended tap count: 1 window: <UIWindow: 0x136470; frame = (0 0: 320 480) opaque = NO.....(and more property type stuff)

Why has my NSString turned into what looks like a UITouch object? What is going on?

+1  A: 

A likely cause for problems like these is that "conversions" got released too early, thus got deallocated and you were still holding a reference to that memory location. Then another object got allocated at this location.

To debug this, set NSZombieEnabled. It doesn't deallocate objects but replaces them with "zombies". See for example the following link:

http://www.tomwhitson.co.uk/blog/2009/04/debugging-with-nszombiesenabled/

DarkDust
This seems to be the problem. Why would it have been released too early and how do I prevent that from happening?
Robert Eisinger
Well obviously you either called [conversions release] or [conversions autorelease] too often or you called [conversions retain] to few :-) Without context I can't tell. But have a look at this:http://blog.mikeweller.com/2009/07/retain-and-release-rules-in-objective-c.html
DarkDust
Another good link is this one here on Stack Overflow:http://stackoverflow.com/questions/6578/understanding-reference-counting-with-cocoa-objective-c
DarkDust
I never called conversions release or autorelease except for a release call in the dealloc method. I'm not familiar with retain. When declaring the string in the .h file, I make it a property with a nonatomic, retain property. Is this not enough?
Robert Eisinger
Then you need to use property assignment: `self.conversions = ...`
tc.