views:

246

answers:

2

I have an iPad app in which I get the device's UDID by the following code in the viewDidLoad method.

uid = [[UIDevice currentDevice] uniqueIdentifier];
uid = [uid stringByReplacingOccurrencesOfString:@"-" withString:@""];

I wanted to remove the dashes in the string.

Later, in another method call, I try to access this uid string (which is a property of this class),

NSLog("Loading request with UDID: %@", uid);

and I get the following in the console window when I try to print it out.

Loading request with UDID: (
    <WebView: 0x4b0fb90>
)

Why is it printing the memory address, instead of the string itself? Thanks!

A: 

NSLog("...") should be NSLog(@"...")

nacho4d
by the way you don't need to do [uid stringByReplacingOccurrencesOfString:@"-" withString:@""];the original string does not contain any "-"
nacho4d
Hmmm, i may have confused myself with some other code that needed string replacement. And yes, i made a mistake typing NSLog(@"...");
Liam
Actually, I just checked again. It DOES have dashes.
Liam
+1  A: 

The issue you're having has to do with memory management. I've had exactly this problem before.

When you NSLog the uid, what you get is the address for a WebView object. Why would that happen when uid is an NSString??? Let me take you on a guided tour of the magic of memory management :D

When you set your uid variable on this line:

uid = [uid stringByReplacingOccurrencesOfString:@"-" withString:@""];

What you did was assign an autoreleased variable to uid. That means it will be released and that memory location will be up for grabs. Between this function ending and the next time you access it, it has been released and something else was stored there.

How do you fix this? When you assign something to a property like uid, ALWAYS do it through the setter methods created by the @property declaration. Use either self.uid = string or [self setUid:string. This will properly release the old string and retain the new one.

This is a problem that has cost me MANY hours trying to find the problematic line. Another symptom that can happen is the program crashing when you try to send a message to a released object. These can be VERY hard to track down. Hopefully my reply helps you and you don't have to endure that frustration :)

Good luck!

Dan Carter
Thank you sooo much! I can't believe it never occurred to me to use self. And, yes this was a very difficult error to track down. I couldn't for the life of me figure out why I was printing a memory address instead of the value.
Liam