![](/img/trans.png)
[英]Objective-C releases C++ Object when passed to Swift. How do I prevent this?
[英]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中的對象?
您沒有alloc
, new
或copy
或mutableCopy
lastPushed
,那么為什么要釋放它呢?
由於lastPushed
是指向數組中對象的指針,因此將其釋放。 當你的陣列認為它有一個有效的指針的對象,但你已經發布了它,它已經這可能會導致一個問題dealloc
版。
因為lastPushed
和數組都指向同一對象,所以它將釋放數組中的對象 。
與往常一樣,您應遵循管理規則。 請勿釋放尚未創建,保留或復制的任何內容。
您不應該釋放lastPushed,因為您沒有使用alloc
, copy
或new
。 (就是這么簡單。)
如果要將其從可變數組中刪除,則應在NSMutableArray中使用適當的方法( removeObject
或removeObjectAtIndex
等)。否則,您不清楚要執行的操作會在下周/月/年對您造成影響,等等
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.