ARC, non-ARC, and inheritance

I have not used ARC yet, except to deal with it when it launches it in a project through third-party code. I read all the ARC docs but did not answer this question:

If I have a class defined in a module compiled with -fobjc-arc, can I get a new class from this module that does not support ARC?

In my opinion, it should work fine until the derived class tries to touch any ivars in the root class. It seems to me that even when using the dealloc method that calls [super dealloc], the derived class will be fine.

And what about the other way? Can I get an ARC class from a class other than ARC? Should it work fine, right?

Reward Points: Are there any bugs when mixing ARC and non-ARC code that I should be aware of?

+5
source share
4 answers

There are no problems that I know of. You should understand that ARC is a bit of a source code preprocessor, adding memory management calls at compile time. When you go to the linking step, you cannot really specify the ARC code from a code other than ARC. (This is probably oversimplification, but it should work for your purposes.) If your derived class has the right memory management and the superclass has the right memory management, the result will work fine.

, , weak. , , , ARC MRC .

+6

, , , , .

ARC ? ( ) - , . -, ARC ivars ARC, weak, , . -, , dealloc. [super dealloc] ? .

, ARC, ARC - ? .

ARC , ARC? , ?

, , . NSObject, .

0

, -ARC- ARC ARC , ARC.

, ARC - , , , [release] [] . ( weak).

0

ARC , , ARC , , :

  • , ( retain)
  • , , ( release)
  • , , . ( ), , ( autorelease " release ".)
  • 1.
  • , .

, , . , ARC, ARC , . , . ARC , , , , ARC; , autorelease ( ARC , ).

, , . ARC , . .

? . A NSString * a CFStringRef , ARC CF-, , ARC NSString, CFString. ARC ARC, .

CFStringRef cfstr = ...;
NSString * nsstr = (__bridge_transfer NSString *)cfstr;
// NSString * nsstr = [(NSString *)cfstr autorelease];

"ARC, , CFString , ". , ; , cfstr , ARC , . :

NSString * nsstr = ...;
CFStringRef cfstr = (__bridge_retained CFStringRef)cftr;
// CFStringRef cfstr = (CFStringRef)[nsstr retain];

"ARC, , NSString, , ". , ! - CFRelease(cfstr), .

, (__bridge ...), , . , , . , ARC , CF-, ARC , , . :

doSomethingWithString((__bridge CFStringRef)nsstr); 

ARC nsstr , , , , , , , , ( , , , ARC , ).

, , , ARC- void *, API, :

- (void)doIt {
   NSDictionary myCallbackContext = ...;
   [obj doSomethingWithCallbackSelector:@selector(iAmDone:) 
        context:(__bridge_retained void *)myCallbackContext
    ];
    // Bridge cast above makes sure that ARC won't kill
    // myCallbackContext prior to returning from this method.
    // Think of:
    // [obj doSomethingWithCallbackSelector:@selector(iAmDone:) 
    //    context:(void *)[myCallbackContext retain]
    // ];
}

// ...

- (void)iAmDone:(void *)context {
    NSDictionary * contextDict = (__bridge_transfer NSDictionary *)context;
    // Use contextDict as you you like, ARC will release it
    // prior to returning from this method. Think of:
    // NSDictionary * contextDict = [(NSDictionary *)context autorelease];
}

, . , :

@implementation SomeObject {
    id _someIVAR;
}

- (void)someMethod {
    id someValue = ...;
    _someIVAR = someValue;
}

ARC ARC. ARC , ARC , :

@interface SomeObject
    @property (retain,nonatomic) id someIVAR;
@end

@implementation SomeObject

- (void)someMethod {
    id someValue = ...;
    self.someIVAR = someValue;
}

someValue , ! -ARC :

@interface SomeObject
    @property (assign,nonatomic) id someIVAR;
@end

@implementation SomeObject

- (void)someMethod {
    id someValue = ...;
    self.someIVAR = someValue;
}

, , ivar -ARC strong, weak, , ( ARC, __unsafe_unretained, ).

So, if you have code that uses ivars directly and does not use properties with setters / getters to access them, then switching from non-ARC to ARC can lead to code saving cycles that were used to manage reasonable memory. On the other hand, moving from ARC to non-ARC, such code can cause dangling pointers (pointers to old objects, but since the object has already died, these points and their use have unpredictable results anywhere), since the objects that were used to be alive before he can now die unexpectedly.

0
source

All Articles