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

Joshua Scott Emmons skia at skia.net
Thu Jan 21 15:36:06 CST 2016


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160121/cd0155b7/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screen Shot 2016-01-21 at 3.27.47 PM.png
Type: image/png
Size: 60665 bytes
Desc: not available
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160121/cd0155b7/attachment.png>


More information about the swift-users mailing list