'memory'에 해당되는 글 1건

  1. 2010.06.24 Memory Management Programming Guide for Cocoa
아이폰어플개발정보2010. 6. 24. 14:13
Memory Management Programming Guide for Cocoa

- garbage collected environment: Mac OS X 10.5+

- reference counted environment: iPhone OS


Object ownership policy: responsibility for disposal


** policy **

- You own any object you create (alloc, new, copy)

- If you own an object, you are responsible for relinquishing ownership when you have finished with it.

- If you do not own an object (convenience constructor), you must not release it.


// typical good example

Thingamajig *thingamajig = [[Thingamajig alloc] init];

// ...

NSArray *sprockets = [thingamajig sprockets];

// ...

[thingamajig release];


// typical wrong example

// 1 - convenience method

Thingamajig *thingamajig = [Thingamajig thingamajig];

[thingamajig release]; // wrong


// 2 - function loses the chance to release

+ (Thingamajig *)thingamajig {

    id newThingamajig = [[Thingamajig alloc] init];

    return newThingamajig;

}


// 3 - no owner, disposed before returned

+ (Thingamajig *)thingamajig {

    id newThingamajig = [[Thingamajig alloc] init];

    [newThingamajig release];

    return newThingamajig; // newThingamajig is invalid here

}


autorelease - you declare that you don't want to own the object beyond the scope in which you sent autorelease.


– (NSArray *)sprockets {

    NSArray *array;

    array = [[NSArray alloc] initWithObjects:mainSprocket,

                               auxiliarySprocket, nil];

    return [array autorelease];

}


strong reference: pointer which retaining the object

weak reference: pointer that doesn't (usually to avoid mutual retaining each other) ex) table data sources,outlineview items,notification observers,and miscellaneous targets and delegates

** holder of weak reference SHOULD be notified when the objected is deallocated. **


** Don't piggy-back resource management on top of dealloc.

You should typically not manage scarce resources such as file descriptors,network connections,and buffers / caches in a dealloc method.Invocation of dealloc might be delayedor side stepped.


When you add an object to a collection such as an array,dictionary,or set,the collection takes ownership ofit.


** autorelease pools **

Anautorelease pool is an instance of NSAutoreleasePool that “contains” other objects that have receivedan autorelease message;when the autorelease poolis deallocated it sends a release message to eachof those objects. An object can be put into an autorelease pool several times, and receives a release messagefor each time it was put into the pool.


If you send an autorelease message when a poolis not available,Cocoa logs a suitable error message.


Autorelease pools are arranged in a stack,although they are commonly referred to as being "nested." When an object is sent an autorelease message, it is added to the current top mostpoolfor the current thread.


If you spawn a secondary thread,you must create your own autorelease poolas soon as the thread begins executing.


Anyautoreleasedobjectscreatedduringthelifetimeofthetaskarenotdisposedofuntilthetaskcompletes.


Ifyoureleaseanautoreleasepoolthatisnotthetopofthestack,thiscausesall(unreleased)autorelease poolsaboveitonthestacktobereleased,alongwithalltheirobjects.


in garbaged collected environment

- release: no-op

- drain of autorelease pool: triggers garbage collection (if the last collection > current threshold)


Youshouldusedrainratherthanreleaseto disposeofanautoreleasepool.


NSCopyObjectcreatesanexactshallowcopyofanobjectbycopyinginstancevariablevaluesbutnotthedatatheypointto.


Zonesarepage-alignedareasofmemorythatholdtheobjectsanddataallocatedbyanapplication. Thesystemassignseachapplicationa“default”zoneinitiallyandapplicationscancreateadditionalzoneslater.



출처 : http://blog.naver.com/PostView.nhn?blogId=gonagi&logNo=150052475165

'아이폰어플개발정보' 카테고리의 다른 글

iPhone Games Projects  (0) 2010.06.24
The iPhone Developers Cookbook  (0) 2010.06.24
Memory Management Programming Guide for Cocoa  (0) 2010.06.24
iPhone Development Guide  (0) 2010.06.24
Quartz2D로 한글 그리기..  (0) 2010.06.24
맥에서 화면 녹화하기  (0) 2010.06.24
Posted by 오늘마감

댓글을 달아 주세요