Storing ObjC NSDictionary as Swift [NSObject:AnyObject] leaks memory

Say you have something like the following (or just grab the attached project: https://dl.dropboxusercontent.com/u/364098/DictionaryLeak.zip\)

//SwiftModel.swift
class SwiftModel : NSObject {
  let prop: [NSObject:AnyObject]
  init(dict: [NSObject:AnyObject]){
    prop = dict
  }
}

//MyViewController.m
- (void)viewDidLoad {
  [super viewDidLoad];

  NSDictionary *dict = @{@"Thing" : @"Stuff"}; //Leaks!
  SwiftModel *model = [[SwiftModel alloc] initWithDict:dict];
  [self setSwiftModel: model];
}

Running this in Instruments (Xcode 7.2 7C68) reports a leak:

Is this actually leaking? Or is this just Instruments loosing track of what's actually holding on to the NSDictionary once it's bridged to swift-world?

Or am I completely overlooking some important concepts somewhere along the line? :)

Cheers,
-jemmons

Hi, jemmons. I managed to reproduce this in pure Swift code, and then changed it so that the Swift dictionary got released. In the latter version, no leaks were reported, so I think it's just a problem with our tools. (This isn't too surprising; the internals of a Swift Dictionary use some of the known-zero bits in the storage pointer to store flags, like "this dictionary came from Cocoa".)

I'ne filed a bug report with the Instruments team. Thanks for bringing this up!

Jordan

···

On Jan 21, 2016, at 13:36 , Joshua Scott Emmons via swift-users <swift-users@swift.org> wrote:

Say you have something like the following (or just grab the attached project: https://dl.dropboxusercontent.com/u/364098/DictionaryLeak.zip\)

//SwiftModel.swift
class SwiftModel : NSObject {
  let prop: [NSObject:AnyObject]
  init(dict: [NSObject:AnyObject]){
    prop = dict
  }
}

//MyViewController.m
- (void)viewDidLoad {
  [super viewDidLoad];

  NSDictionary *dict = @{@"Thing" : @"Stuff"}; //Leaks!
  SwiftModel *model = [[SwiftModel alloc] initWithDict:dict];
  [self setSwiftModel: model];
}

Running this in Instruments (Xcode 7.2 7C68) reports a leak:
<Screen Shot 2016-01-21 at 3.27.47 PM.png>
Is this actually leaking? Or is this just Instruments loosing track of what's actually holding on to the NSDictionary once it's bridged to swift-world?

Or am I completely overlooking some important concepts somewhere along the line? :)

Cheers,
-jemmons
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users