Oliver Weichhold
Oliver Weichhold

Reputation: 10306

MonoTouch native resource management

Example:

var image = UIImage.FromFile("/path/to/image.png");
var imageView = new UIImageView();
imageView.Image = image;
image.Dispose();

Will the native reference from the UIImageView keep the underlying native image alive or will this crash sooner or later because the native image is actually dead?

Update:

void Foo()
{
  var image = UIImage.FromFile("/path/to/image.png");
  this.imageView = new UIImageView();
  this.imageView.Image = image;
}

Will this leak the underlying native image?

Upvotes: 2

Views: 128

Answers (1)

Rolf Bjarne Kvinge
Rolf Bjarne Kvinge

Reputation: 19345

  1. The native UIImageView will keep a reference to the image, so it won't crash.
  2. No, it will not leak the underlying native image. But it might end up freed a lot later than you'd expect. We recommend you call Dispose once you're done with images, since they tend to use a lot of memory which the garbage collector doesn't know about.

Upvotes: 3

Related Questions