Objc Class NSObject dealloc - sonkoni/Koni-Wiki GitHub Wiki
[Objc]] ](/sonkoni/Koni-Wiki/wiki/[[NSObject|Objc-Class-NSObject)
dealloc
- (void)dealloc;
// ๋ฆฌ์๋ฒ๋ฅผ ๋ฉ๋ชจ๋ฆฌ์์ ํด์ ์ํจ๋ค.
๋ฆฌ์๋ฒ์ ๋ํ ํ์ ๋ฉ์์ง๋ ํด์ ๋ ๊ฐ์ฒด์ ๋ฉ์์ง๊ฐ ์ ์ก๋์์์ ๋ํ๋ด๋ ์ค๋ฅ๋ฅผ ์์ฑํ ์ ์๋ค. ์ด ๋ฉ์๋๋ฅผ ์ฌ์ ์ํ์ฌ ๊ฐ์ฒด์ ์ธ์คํด์ค๊ฐ ์๋ ๋ค๋ฅธ ๋ฆฌ์์ค๋ฅผ ์ ๊ฑฐํ๋ค. ์๋ฅผ ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
- (void)dealloc {
free(myBigBlockOfMemory);
}
dealloc์ ์ด์ฉํ์ฌ ํ์ผ์คํฌ๋ฆฝํฐ ๊ฐ์ ์ ํ๋ ๋ฆฌ์์ค์ lifetime์ ๊ด๋ฆฌํ๋ ๊ฒ์ ํผํด์ผํ๋ค.
๊ทธ๋ฆฌ๊ณ ์ง์ ์ ์ผ๋ก - dealloc ๋ฉ์์ง๋ฅผ ํธ์ถํด์๋ ์๋๋ค. ๋ฐํ์์์ ๊ฐ์ฒด์ - dealloc์ด ์๋์ผ๋ก ํธ์ถ๋๋ค. ์์ธํ ๋ด์ฉ์ Advanced Memory Management Programming Guide๋ฅผ ์ฐธ๊ณ ํ๋ผ.
dealloc ์ฌ์ ์
ARC๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ retain / release / autorelease ์ ๋ชจ๋ ์ฌ์ฉํด์๋ ์๋๋ค. ์ฆ, ์ฝ๋ ์์ ์ถํํด์๋ ์๋๋ค. ๋จ, dealloc๋ ํ๋ก๊ทธ๋๋จธ๊ฐ ํธ์ถํ๋ ๊ฒ์ ์๋์ง๋ง, ์ฌ์ ์๋ ๊ฐ๋ฅํ๋ค.
โ ๏ธ ์ฃผ์ MRC์์์ ์ฑ๊ฒฉ๊ณผ๋ ๋ค๋ฅด๋ค. MRC์์๋ ์์ ๊ฐ์ฒด๋ฅผ releaseํ๊ณ [super dealloc];์ ํธ์ถํ์ง๋ง, ARC์์๋ ๋๋ค ํด์๋ ์๋๋ค. ๋จ์ง ํ ๋น ํด์ ๋์์ ๋ ์ด๋ค ํ ์ผ ๋๋ ์๋ฆผ์ ๋๋ก ๋ณด๋ฉด๋๋ค. ๋ฆด๋ฆฌ์ฆ์ ๊ด๋ จ๋ ์ผ์ ARC๊ฐ ์์์ ํ๋ค.
- MRC์์์ dealloc์ฌ์ ์ : ์์ ๊ฐ์ฒด์
release-> ์ํผํด๋์ค์dealloc๋ฉ์๋ ํธ์ถ. - ARC์์์ dealloc์ฌ์ ์ : ํด๋น ๊ฐ์ฒด๊ฐ ํด์ ๋์์ ๋, ํ๋ก๊ทธ๋๋จธ๊ฐ ํ๊ณ ์ถ์ ํ์.
์์์ฝ๋ : ARC์์์ dealloc์ฌ์ ์
- (void)dealloc {
NSLog(@"dealloc ๋๋ถ๋ ๊ตฌ๋ง. ์บฌ์บฌ์บฌ");
// ARC ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ด๋ฆฌํ๋ฏ๋ก release๋ฉ์๋ ํธ์ถ์ด๋, [super dealloc];์ ํด์๋ ์๋๋ค.
}
@end
์์์ฝ๋ : MRC์์์ dealloc์ฌ์ ์
- (void)dealloc {
[_myDate release]; // ์์ ํ ๊ฐ์ฒด๋ฅผ ๋ฆด๋ฆฌ์ฆํ๋ค.
[super dealloc]; // ์์ ํ ๊ฐ์ฒด๋ฅผ ๋ฆด๋ฆฌ์ฆํ๋ค ์ํผํด๋์ค์ dealloc๋ฅผ ํธ์ถํ๋ค!! ๋ฐ๋์!!
}
@end
์ฐธ๊ณ
- MRC
- ARC