Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-22-2011, 03:33 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 2
ivanHvar is on a distinguished road
Angry retain count

I'm just learning ObjectiveC....
And for last two days I can not find any explanation why this code gives always a retainCount of -1 (negative value)... in all cases !!!

NSString *test1 = @"Test string";
NSLog(@"test1 retain count is: %d", [test1 retainCount]);
[test1 retain];
NSLog(@"test1 retain count is: %d", [test1 retainCount]);
[test1 release];
NSLog(@"test1 retain count is: %d", [test1 retainCount]);

NSString *test2 = [[NSString alloc] initWithString:@"Test string"];
NSLog(@"test 2 retain count is: %d", [test2 retainCount]);
[test2 retain];
NSLog(@"test 2 retain count is: %d", [test2 retainCount]);
[test2 release];
NSLog(@"test 2 retain count is: %d", [test2 retainCount]);


Can someone explain ?
ivanHvar is offline   Reply With Quote
Old 02-25-2011, 09:51 AM   #2 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 4
Geoff31858 is on a distinguished road
Default result I got

I copied your code and pasted it into a program I'm writing and got this result...

Code:
2011-02-25 10:44:32.226 SILife_01[11666:207] test1 retain count is: 2147483647
2011-02-25 10:44:32.271 SILife_01[11666:207] test1 retain count is: 2147483647
2011-02-25 10:44:32.272 SILife_01[11666:207] test1 retain count is: 2147483647
2011-02-25 10:44:32.273 SILife_01[11666:207] test 2 retain count is: 2147483647
2011-02-25 10:44:32.273 SILife_01[11666:207] test 2 retain count is: 2147483647
2011-02-25 10:44:32.274 SILife_01[11666:207] test 2 retain count is: 2147483647
I didn't get -1. Don't know why.
Geoff31858 is offline   Reply With Quote
Old 02-26-2011, 02:45 AM   #3 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 2
ivanHvar is on a distinguished road
Default

I also found out that most people get 2147483647 instead of -1 .... But nevertheless even in your case the retaincount stays the same ... What machine do you have and what version of xcode ?
ivanHvar is offline   Reply With Quote
Old 02-26-2011, 08:22 PM   #4 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 247
linkmx is on a distinguished road
Default

When you declare NSStrings like that you don't need to release them.

Quote:
Does the NSString returned by @"" need to be released, or is it autoreleased?
Neither. @""-strings are of class NSConstantString?, and thus act like atoms in lisp; they hang around. That is, if you use @"cow" in two separate places in your code, they will be referencing the very same object.

I don't think -release or -autorelease does anything to either of them.

Over-releasing one might be bad. You should essentially treat constant strings as if they were autoreleased objects, and balance retains/releases appropriately.

Storage-wise, I think it makes sense to think of @"" literals the same way as "" literals: they are in memory the entirety of your program's lifespan. And that's the way it should be. If you "released" a string constant, the next time the code that uses that literal gets called, the object wouldn't be there anymore! You wouldn't "release" an integer constant, or a float constant, for example, if you had some function "int f(int x) { return x+1; }", it wouldn't make any sense to "free" the "1" part of the code! It's part of your program image! A string literal really isn't any different. Of course since NSString is an NSObject you can call retain on it, but I like to think of that as only making the API consistent with other NSStrings? which may be allocated dynamically.
CocoaDev: NSString
linkmx is offline   Reply With Quote
Reply

Bookmarks

Tags
retaincount

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 488
16 members and 472 guests
7twenty7, AlanFloyd, AppsBlogger, David-T, iAppDeveloper, imac74, Jaxen66, lovoyl, Music Man, mutantskin, Paul Slocum, Sami Gh, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:13 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0