Does ARC need to set nil for all strong members when unloading the viewcontroller? It is necessary?

Suppose I have a controller of the form:

@interface ControllerA : viewcontroller{
    NSString * __strong a;
}

@end

and in the function viewDidLoadI set

a = [[NSSString alloc] init];

And in another controller B

{
    ControllerA * controllerA = [[ControllerA alloc] init];
}

Will the controllerA element be released?

+5
source share
4 answers

Yes, the line pointed ato will be freed upon release controllerA. You do not need to install it on your nilown.

ARC ARC. , , , -dealloc, - , , .

+5

nil ARC, (.. [ myObject] , ARC). iVars , .

+4

iOS 5 ARC Zombies, ivars: Q & A QA1758.

, ivars .

+2

2:

, , , ivars, iOS 5 , , ARC, iOS 5 , , , Calin , . Q & A QA1758. Calin!

https://github.com/robertmryan/arc-problem-project

Update:

, , vars properties , nil. . ARC, , , , . , , ivar , .

, - . , ( , push-, , ), . ( ) , . , , . , https://github.com/robertmryan/arc-problem-project. , - , . , ( , ), , ARC dealloc .

, , ivars . , , .

//  ArcTestViewController.m

#import "ArcTestViewController.h"

#pragma mark - IvarObject

@interface IvarObject : NSObject
@end

@implementation IvarObject

- (void)dealloc
{
    NSLog(@"%s", __FUNCTION__);
}

@end

#pragma mark - ArcTestViewController

@interface ArcTestViewController ()
{
    IvarObject __strong *_ivarObject;
}
@end


@implementation ArcTestViewController

- (void)dealloc
{
    NSLog(@"%s", __FUNCTION__);

//    _ivarObject = nil;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    _ivarObject = [[IvarObject alloc] init];
}

@end

=nil dealloc, :

2012-07-12 10: 33: 33.644 test4 [36045: 11903] - [ArcSimpleTestViewController dealloc]

, =nil dealloc, :

2012-07-12 10: 35: 23.117 test4 [36083: 11903] - [ArcSimpleTestViewController dealloc]

2012-07-12 10: 35: 23.119 test4 [36083: 11903] - [IvarObject2 dealloc]

, , , deallocs .

Rob Napier provided the sample code below (I appreciate it and others taking my time), but even the code above works fine when I put it in another project, but in my current project it is not freed up properly!


Update from Rob Napier: A simpler example (all in one file) demonstrating how this works. Put this in a file and compile as indicated:

//  arc.m
//  Compile with:
//      clang -fobjc-arc -framework Foundation -o arc arc.m

#import <Foundation/Foundation.h>

#pragma mark - IvarObject

@interface IvarObject : NSObject
@end

@implementation IvarObject

- (void)dealloc
{
    NSLog(@"%s", __FUNCTION__);
}

@end

#pragma mark - PropertyObject

@interface PropertyObject : NSObject
@end

@implementation PropertyObject

- (void)dealloc
{
    NSLog(@"%s", __FUNCTION__);
}

@end


#pragma mark - ArcTest

@interface ArcTest : NSObject
{
    IvarObject __strong *_ivarObject;
}

@property (nonatomic, strong) PropertyObject *propertyObject;

@end


@implementation ArcTest

@synthesize propertyObject = _propertyObject;

- (void)dealloc
{
    NSLog(@"%s", __FUNCTION__);

//    _ivarObject = nil;
//    _propertyObject = nil;
}

- (void)assignStuff
{
    _ivarObject = [[IvarObject alloc] init];
    self.propertyObject = [[PropertyObject alloc] init];
}

@end

int main () {
    ArcTest *test = [[ArcTest alloc] init];
    [test assignStuff];
    test = nil;
    NSLog(@"End program");
}
+1
source

All Articles