Swift 2 and Xcode 7 have a memory leak in "switch" statement

Hi,


Doing some tests I have bumped into what could be a bug in Swift 2 and XCode 7 (beta 5). It doesn't happend in Swift 1 and XCode 6.4.

It is related with memory not released if I cast NSDictionay as [String:AnyObject] in a "switch" statemen.


By the way, is something that happends in "SwiftyJSON" utility library. Quite common to access JSON objects.



I had something like this:


//-------------------------------------------------------------------------------------------

let pathName = "my_json_data_file.json"


// Big loop to see how memory grows

for var n=0;n<20000;n++ {


// Autorelease pool to release objects a soon as possible and avoid confusing the memory leak with that

autoreleasepool {

do {

let jsonData = try NSData(contentsOfFile: pathName, options: NSDataReadingOptions())

let value: AnyObject = try NSJSONSerialization.JSONObjectWithData(jsonData, options: .AllowFragments)

// This is the problematic code

switch value {

case _ as [String : AnyObject]:

// Something to do

break

default:

// Somethig...else

break

}


} catch {

print("error!")

}

}

}


But if I change the "switch" part with any of the followings the memory doesn't grow.


if value is [String:AnyObject] {

// Do something

} else {

// ... else

}

if let _ = value as? [String:AnyObject] {

// Do something

} else {

// ... else

}



Is it a bug? Am I doing something wrong?



Thanks

A memory leak without any remaining references is definitely a bug. Please file a bug report with a self-contained reproducer!

I'm sorry, but I didn't report a bug before and it seems a little bit complex.

If you're able to attach your entire project, we can attempt to find the issue on our side. (If the code in your post is really all it takes to reproduce, that's great, but we still want to make sure we're matching your build settings and version of Xcode.)

Swift 2 and Xcode 7 have a memory leak in "switch" statement
 
 
Q