Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a memory leak at String creation in swift?

Tags:

The Leak is a Root Leak, In this image is being caused several times on the same line, but there is another below that is called single time and also produces a leak.

enter image description here

This is the call stack after calling the line of code stated before.

enter image description here

This is the class where the leak is located by Instruments:

class Item {  var id: String!  var name: String!   internal init(name: String) {     self.name = name     self.id = name  }   var description: String {     return "(\(id)) \(name)"  } } 

Leak is detected at line of computed variable description containing return "(\(id)) \(name)" and it gets solved after changing description into:

var description: String {     return "(" + id + ") " + name } 

Update:

or

var description: String {     if let id = self.id as? String, let name = self.name as? String {         return "(\(id)) \(name)"     }     return "NO AVAILABLE DESCRIPTION" } 

The last one emits a "Conditional cast from 'String!' to String always succeeds".

So, even this looks like a hack.

Why is this causing a leak?

like image 402
Hugo Alonso Avatar asked May 04 '15 20:05

Hugo Alonso


People also ask

What causes swift memory leak?

Like other high-level programming languages, Swift has graced us with Automatic Reference Counting. ARC is a built-in process that automatically finds unused objects and deletes them to free up memory. In most cases, ARC does its job well. However, leaks still happen in iOS by accident.

What is the main cause of memory leaks?

A memory leak starts when a program requests a chunk of memory from the operating system for itself and its data. As a program operates, it sometimes needs more memory and makes an additional request.

What is memory leak in Swift?

A memory leak occurs in an iOS app when memory that is no longer in use is not properly cleared and continues taking up space. This can hurt app performance, and eventually, when the app runs out of available memory, will cause a crash.

What causes memory leaks in iOS?

A memory leak occurs when allocated memory becomes unreachable and the app can't deallocate it. Allowing an allocated-memory pointer to go out of scope without freeing the memory can cause a memory leak. A retain cycle in your app's object graph can also cause a memory leak.


1 Answers

I tested your code and gone through few threads and my understanding is that you have to optional binding if let clause, when using string interpolation instead of using optional variables directly. For String concatenation, if we use optionals directly there is no problem. The problem is with interpolation.

var description: String {     if let id = self.id, let name = self.name {         return "(\(id)) \(name)"     }     return "NO AVAILABLE DESCRIPTION" } 

You may get more info here memory leak in Swift String interpolation. Seems like a bug and may be in future release it will be solved.

like image 144
Amit89 Avatar answered Oct 02 '22 08:10

Amit89