簡體   English   中英

Objective-C內存管理:如果我釋放存儲在數組中的字符串,它僅釋放該引用還是釋放數組中的對象?

[英]Objective-C memory management: if I release a string stored in an array, it only releases this reference or it releases the object in the array?

如果我有這個:

NSString *lastPushed = (NSString *)[tagStack objectAtIndex:[tagStack count]-1];
.//do something with the last pushed element locally
.//do more cstuff here
.//after doing all the stuff needed in the function

[lastPushed release];

其中tagStack是NSMutableArray

如果我釋放lastPushed,因為它沒有被復制或初始化,它會只釋放該引用還是會真正釋放mutableArray中的對象?

您沒有allocnewcopymutableCopy lastPushed ,那么為什么要釋放它呢?

由於lastPushed是指向數組中對象的指針,因此將其釋放。 當你的陣列認為它有一個有效的指針的對象,但你已經發布了它,它已經這可能會導致一個問題dealloc版。

因為lastPushed和數組都指向同一對象,所以它將釋放數組中的對象

與往常一樣,您應遵循管理規則。 請勿釋放尚未創建,保留或復制的任何內容。

您不應該釋放lastPushed,因為您沒有使用alloccopynew (就是這么簡單。)

如果要將其從可變數組中刪除,則應在NSMutableArray中使用適當的方法( removeObjectremoveObjectAtIndex等)。否則,您不清楚要執行的操作會在下周/月/年對您造成影響,等等

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM