-2

I am writing a simple Obj-c app. So far I've never had to alloc anything and I gave for granted that ARC would take care of memory management.

But now I have to call:

NSImage *myImage = [[NSImage alloc] initByReferencingFile: pathToMyImg];

After I'm done with myImage should I manually dealloc it?

Maurizio
  • 547
  • 5
  • 13
  • 1
    If you're using ARC then it will automatically be deallocated when you aren't using it anymore – Alex Nov 17 '16 at 17:04

2 Answers2

1

If you’re using Objective-C ARC (which is the default, and you said you were anyway), there is no difference in required memory management between factory method-style instantiation and [[NSImage alloc] init]-style instantiation. Everything you create will be automatically released when the last strong reference goes out of scope (or the object holding the reference’s last strong reference goes out of scope).

If you weren't using ARC, you’d have to manually do [myImage release] when you were done with it, but there’s no reason not to be unless you’re writing code for an older project.

ThatsJustCheesy
  • 1,370
  • 14
  • 24
0

After I'm done with myImage should I manually dealloc it?

The question makes no sense. You are using ARC. Suppose the answer were Yes. How would you do it? You cannot. ARC will not let you give any NSObject memory management commands.

Therefore the answer must be No.

matt
  • 515,959
  • 87
  • 875
  • 1,141