Hello, there are two questions related to the garbage collection that I
have been investigating but I'm not capable of understanding.
1) With regards to local variables, according to the do***entation
__strong references are those whose life cycle is controlled by the
garbage collector. The compiler allows marking as __strong local
variables of fundamental types and local pointer to non-Objective-C
objects without warnings:
__strong int i=4; // No warning
__strong char* data = (char*)malloc(512); // No warning
__strong Point* p = [[Point alloc] init]; // No warning
That is, the garbage collector manages the life cycle of these
variables. However, it emits a warning when those variables are marked
as __weak:
__weak int i=4; // Warning
__weak char* data = (char*)malloc(512); // Warning
__weak Point* p = [[Point alloc] init]; // Warning
The warning is always: "__weak attribute cannot be specified on a local
object declaration"
Hence, how can we avoid that the garbage collector copes with the life
cycle of a local variable? Does it means that the memory related to
local variables must be always managed by the garbage collector when
the garbage collector is enabled, that is, it's not possible to make a
weak reference using a local variable? And, why the compiler allows
declaring int or char* types as __strong?
2) The compiler allows to create __strong and __weak global object
pointers without complaint, but in both cases the related object
receives the finalize method call when the variable is nilled, that is:
__weak Point* pp; // Global variable
int main (int argc, const char * argv[]) {
pp = [[Point alloc] initWithX:1 y:1];
pp = nil;
[[NSGarbageCollector defaultCollector] collectExhaustively]; //
Executes finalize over pp
return 0;
}
This is not congruent with the semantics of __weak that the
do***entation provides because the collector must not finalize __weak
pointers.


|