0

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?

CodeGuy
  • 28,427
  • 76
  • 200
  • 317
  • Please show the output using NSLog(@"[%@]". conversions); It might be that the output you are seeing is the NSString text – ennuikiller Aug 09 '10 at 14:55
  • The output is the same except there are brackets at the beginning and at the end: [UITouch 0x131700> phase....] – CodeGuy Aug 09 '10 at 15:02
  • you have an over release problem, like the answer below says...The memory that the NSString points to is being reclaimed and reused (this time by some UITouch object) – Daniel Aug 09 '10 at 15:06

1 Answers1

2

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
  • 90,870
  • 19
  • 190
  • 224
  • This seems to be the problem. Why would it have been released too early and how do I prevent that from happening? – CodeGuy Aug 09 '10 at 15:08
  • 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 Aug 09 '10 at 15:16
  • Another good link is this one here on Stack Overflow: http://stackoverflow.com/questions/6578/understanding-reference-counting-with-cocoa-objective-c – DarkDust Aug 09 '10 at 15:26
  • 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? – CodeGuy Aug 09 '10 at 16:45
  • Then you need to use property assignment: `self.conversions = ...` – tc. Aug 09 '10 at 19:02