Search code examples
calayernsviewmetal

Deleting CAMetalLayer for NSView


I have a custom NSView class which is layer backed. I make a CAMetalLayer for this NSView which is created in makeBackingLayer.

In makeBackingLayer I create a layer CAMetalLayer *backingLayer = [CAMetalLayer layer]; and set properties as required by me.

My question is, do I need to dealloc this layer explicitly while destructing NSView? I do not create this layer in an overridden function, is it my responsibility to delete this or the NSView will take care of it?

I do not see any documentation around this. Moreover, all the samples that I see do not mention about deleting layers anywhere.

Thanks


Solution

  • You likely do not need to manually free your layer. Assuming your program has ARC (Automatic Reference Counting) enabled, your NSView should automatically free whichever CALayer is set to its .layer property upon its destruction.

    If you're not sure if ARC is enabled, you can go to Build Settings in your Xcode project and search for Automatic Reference Counting. It's been on by default for new Xcode projects for several years now.

    Note: Your NSView will only be able to free your layer if it is the only object holding a reference to it. If other objects in your program are holding references to your CAMetalLayer, your layer would not be freed until they remove their references.