Block memory management

In this link, Apple has a conceptual overview of Blocks objects in objective-c:

http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Blocks/Blocks.pdf

However, in reality this does not explain two topics that concern me and may concern other people. The first question is this: can I assign a block reference to zero? Or should I use NULL? Or can I use none of them?

The second problem is memory management. Let's say I declared such a method by creating a block object on the stack.

-(void)makeTheClass
{
    TheClass *object = [[TheClass alloc] init];

    object.blockReference = ^(void) { return nil; } 
}

This object, created within a certain scope, will be destroyed after it leaves it. But the TheClass object will actually store a link to this (almost destroyed) block:

typedef id (^WeirdBlockType)(void);

@interface TheClass {
    WeirdBlockType blockReference;   
}

? :

@property (nonatomic, retain) WeirdBlockType blockReference;
@property (nonatomic, copy)   WeirdBlockType blockReference; 

?

Apple , . , ? , makeTheClass?

+3
2

, . , :

. , , Block_copy ​​.

, - , , ; (alloc init) NSObject , - retain . , .

!

+2

? NULL?

nil " id", NULL ((void *)0). . , NSObject, nil.

nil, , NSObject ( , ..). NULL, .

? :

@property (nonatomic, retain) WeirdBlockType blockReference;
@property (nonatomic, copy)   WeirdBlockType blockReference; 

?

:

Objective-C, , ( ).

, , , retain, copy.

:

, ( ), , , .

0

Source: https://habr.com/ru/post/1774674/


All Articles