Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 6, 2012, 05:52 AM   #1
ramy1989
macrumors newbie
 
Join Date: Nov 2012
Problems understanding ARC and zombies

I started learning Cocoa when there still was the manual counting, but after few time I got the new OS update and ARC was available.
I know how to user retain-release if I don't use ARC, but the problem comes with ARC enabled.I always used strong references and everything was fine, never had leaks nor dangling pointers.
But I want to understand better how it works.

Now I am running a project for debugging, with zombies enabled.I tried do do this:
Code:
    __unsafe_unretained NSString* str;
    @autoreleasepool
    {
        str= @"Hello";
    }
    NSLog(@"%@",str);
I would except that str is zombie because @"Hello" should be autoreleased, and I print it outside the pool.But instead it just prints "Hello" and all goes fine.Why?

Also a tip, should I start making some projects with manual counting to understand how ARC works?
ramy1989 is offline   0 Reply With Quote
Old Dec 6, 2012, 07:50 AM   #2
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by ramy1989 View Post
I started learning Cocoa when there still was the manual counting, but after few time I got the new OS update and ARC was available.
I know how to user retain-release if I don't use ARC, but the problem comes with ARC enabled.I always used strong references and everything was fine, never had leaks nor dangling pointers.
But I want to understand better how it works.

Now I am running a project for debugging, with zombies enabled.I tried do do this:
Code:
    __unsafe_unretained NSString* str;
    @autoreleasepool
    {
        str= @"Hello";
    }
    NSLog(@"%@",str);
I would except that str is zombie because @"Hello" should be autoreleased, and I print it outside the pool.But instead it just prints "Hello" and all goes fine.Why?

Also a tip, should I start making some projects with manual counting to understand how ARC works?
Strings like @"Hello" are _never_ released and have never been released. Before ARC, their retain count returned some meaningless value, and retain / release / autorelease never had any effect on them.
gnasher729 is offline   0 Reply With Quote
Old Dec 9, 2012, 12:46 PM   #3
Madd the Sane
macrumors 6502
 
Join Date: Nov 2010
Location: Utah
To get what you're looking for, use
Code:
str = [NSString stringWithString:@"Hello"]
.

I think NSString has a stringWithString method, but I can't verify it right now.
Madd the Sane is offline   0 Reply With Quote
Old Dec 11, 2012, 12:49 AM   #4
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
Quote:
Originally Posted by Madd the Sane View Post
To get what you're looking for, use
Code:
str = [NSString stringWithString:@"Hello"]
.

I think NSString has a stringWithString method, but I can't verify it right now.
+stringWithString: will short-circuit to just using the argument directly in this case. -mutableCopy should get what he wants.
Catfish_Man is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Resolved: ARC, @property, alloc/init larswik iPhone/iPad Programming 5 Aug 25, 2013 05:36 PM
Resolved: Memory never released in ARC project? xArtx iPhone/iPad Programming 4 Feb 7, 2013 12:49 AM
Can ARC be used in iOS 4.3? mikezang iPhone/iPad Programming 3 Jan 16, 2013 06:17 AM
How to modify source in ARC? mikezang iPhone/iPad Programming 8 Jun 5, 2012 08:50 AM

Forum Jump

All times are GMT -5. The time now is 03:32 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC