NSAnimation delegate-ownership is ignored
| Bugzilla Link | 2542 |
| Resolution | FIXED |
| Resolved on | Dec 23, 2018 16:12 |
| Version | unspecified |
| OS | All |
| Attachments | A clang test case which describes my assumptions about NSAnimation. Test case fails in clang rev 53413 |
| Reporter | LLVM Bugzilla Contributor |
| CC | @tkremenek |
Extended Description
NSAnimation object retains itself and is then autoreleased at the end of the animation or when it receives stopAnimation. Currently self-ownership is ignored and memory leak false positive is reported. See attached test case.
*** Bug llvm/llvm-project#2898 has been marked as a duplicate of this bug. ***
While the analyzer still doesn't do interprocedural analysis to handle this correctly, it will now suppress reference-count related warnings when a tracked object is sent a message containing the keyword 'delegate:' in the selector. This heuristic assumes that the analyzer just doesn't have the reasoning power to handle delegates well at all, and thus shouldn't emit a warning in such cases.
http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20090420/016210.html
This will go into checker-0.189. Eventually we'll have a real fix for this by doing interprocedural analysis (Nikita's example is now in the test suite).
Same issue as rdar://problem/6793409.
Delegates are an idiom that the analyzer eventually needs to handle to cut down on an important source of false positives (and also potentially find other bugs). Some of this will require interprocedural analysis (IPA), which I have been gradually laying the groundwork for in the analyzer core. While it will be a while before doing IPA across source files, we can first start by doing IPA within a single file. Since @implementations don't span multiple files, I think even this limited IPA will get some traction in the short-term.
The question is what to do when we cannot analyze the methods used for the delegate idiom. Even with full-blown, cross-file IPA, if the source of a method or class is not available we will have to make assumptions. I think for methods involving delegates that the checker should opt for false negatives instead of false positives. We can tailor the checker to take this strategy for well-known delegate APIs, which hopefully will handle 90% of the common cases where the analyzer get's it wrong. Once more IPA features come into play, we can improve the precision and do more checking.
Hi,
I think this was actually my own misunderstanding of the documentation of NSAnimation (and incorrect interpretation of a memory leak report as false positive). The documentation if startAnimation says:
The receiver retains itself and is then autoreleased at the end of the animation or when it receives stopAnimation.
That means, of course, that the creator of NSAnimation must release (or autorelease) the object normally, and the memory leak report was, in fact, correct.
Whether it's safe to send messages to a NSAnimation object after it has been autoreleased really depends on two things: when the message is sent and the blocking mode of NSAnimation object. First, NSAnimation can be run either in blocking mode (blocking the current thread), non-blocking mode (running in the runloop of the current thread) or threaded mode (running in the runloop of another thread). Blocking modes can also be changed by the user (blocking mode of the same NSAnimation object can be changed several times). I've included some examples at the end of this message.
The f2() example does still produce false memory leak report. This example demonstrates a case where ownership is moved from the creator of an object to a delegate object (we talked about it on mailing list in June). However, it may be too complicated case for clang to resolve, since it must inspect the contents of callback method, which may be in another class.
I've updated test cases to use the correct intrepretation of NSAnimation documentation, and to demonstrate the effect of delegate-ownership. I'll attach the updated test case file to this report.
Examples:
Safe calls, no leaks:
-
(void)f1 { NSAnimation *animation = [[NSAnimation alloc] init...]; // NSAnimationBlocking is the default blocking mode. rc = 1
[animation setDelegate:self]; [animation startAnimation]; // retains itself, blocks this thread until animation is complete and releases itself after that. rc = 1 [animation release]; // rc = 0. }
// Called by NSAnimation object when its progress is 1.0
- (void)animationDidEnd:(NSAnimation *)animation { if ([animation currentProgress] == 1.0) // safe, because the object has not yet been deallocated. This is called from inside startAnimation runloop. rc = 2. NSLog(@"We're really done!"); }
-
(void)f2 { NSAnimation *animation = [[NSAnimation alloc] init...]; // NSAnimationBlocking is the default blocking mode. rc = 1
[animation setDelegate:self]; [animation startAnimation]; // retains itself, blocks this thread until animation is complete and releases itself after that. rc = 0. }
// Called by NSAnimation object when its progress is 1.0
- (void)animationDidEnd:(NSAnimation *)animation { [animation release]; // This is called from inside startAnimation runloop. rc = 1. }
-
(void)f3 { NSAnimation *animation = [[NSAnimation alloc] init...]; // NSAnimationBlocking is the default blocking mode. rc = 1
[animation setAnimationBlockingMode:NSAnimationNonblocking]; [animation startAnimation]; // retains itself, adds itself to the runloop of the current thread. rc = 2 [animation release]; // rc = 1 [animation stopAnimation]; // safe, because the object has not yet been deallocated (it will be, when the current event has been processed) }
Nonsafe call:
-
(void)f4 { NSAnimation *animation = [[NSAnimation alloc] init...]; // NSAnimationBlocking is the default blocking mode. rc = 1
[animation startAnimation]; // retains itself, blocks this thread until animation is complete and releases itself after that. rc = 1 [animation release]; // rc = 0. [animation stopAnimation]; // not safe, because the object has been already deallocated }
Thanks Nikita! These are fantastic test cases.
From the NSAnimation documentation, it appears that the automatic retain occurs when the object receives -startAnimation:
http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSAnimation_Class/Reference/Reference.html#//apple_ref/occ/instm/NSAnimation/startAnimation
My interpretation of this means that an NSAnimation objected that is allocated and initialized doesn't immediately own itself.
Having never used NSAnimation, this documentation is a little confusing to me. When is the object "autoreleased"? Is it legal to still send messages to an NSAnimation object after the object receives -startAnimation?
Fixed the 'Hardware' value.
assigned to @tkremenek