Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically display iPhone/iPad UDID

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!

like image 1000
Liam Avatar asked Dec 08 '22 02:12

Liam


2 Answers

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!

like image 60
Dan Carter Avatar answered Jan 04 '23 15:01

Dan Carter


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

like image 40
nacho4d Avatar answered Jan 04 '23 16:01

nacho4d