I
I
Iwan2015-06-19 04:28:38
Cocoa
Iwan, 2015-06-19 04:28:38

NSImage.initWithContentsOfFile how to deallocate memory correctly in Objective-C with ARC?

I have a simple OS X application that needs to frequently (very, very often) take a picture from a file on disk and change its dock icon to it.
Here's how I do it now.
Initialization

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    self.iconX =  [[NSImage alloc] initWithContentsOfFile: @"/Users/test.png"];
}

At the time of frequent change
[self.iconX initWithContentsOfFile: @"/Users/test.png"];
[NSApp setApplicationIconImage:self.iconX];

But since changes happen often, and the size of the picture can be large, the application simply eats up memory and hangs. I don’t understand how and what should be released in this case, provided that ARC is used by default?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
S
s0L, 2015-06-19
@s0L

[self.iconX initWithContentsOfFile: @"/Users/test.png"];

very bad to do this, use the same line as when starting

Similar questions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question