I had an interview, and I was asked to create a memory leak with Objective-C and Swift.So how to create a memory leak with Objective-C and Swift?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Using if let syntax in switch statement
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Enum with associated value conforming to CaseItera
You just need to create a reference cycle. Obj-C:
The same applies to Swift:
Reason:
a
holds a strong reference tob
, andb
holds a strong reference toa
. ARC doesn't have any garbage collection in runtime. It just tracks the reference count. In this situation, even if we don't have any references toa
orb
in code, their reference count will never reach0
, since they reference each other (unless we'll break this cycle manually).UPD (kudos to @Sulthan): you don't actually even need two objects, a strong self reference is enough:
The block example:
The fix:
The malloc example:
If CGImageRef objected created and not released, then ARC can't release these objects.