Summary:public An observer object that registered to a notification center needs to be unregistered before it is deallocated. If not, the notification center may send a notification to a gost object. This diff introduce a checker for this problem. Reviewed By: dulmarod Differential Revision: D2949692 fb-gh-sync-id: 1653cec shipit-source-id: 1653cecmaster
parent
021cf213a6
commit
c868f51b2d
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (c) 2016 - present Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
#import <Cocoa/Cocoa.h>
|
||||
@interface ViewController : NSViewController
|
||||
@end
|
||||
|
||||
@implementation ViewController
|
||||
|
||||
- (instancetype)init {
|
||||
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(fired)
|
||||
name:@"some_notification"
|
||||
object:nil];
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)fired {
|
||||
NSLog(@"This codepath fired");
|
||||
}
|
||||
|
||||
- (void)invalidate1 {
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||
}
|
||||
|
||||
- (void)invalidate2 {
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self
|
||||
name:@"some_notification"
|
||||
object:nil];
|
||||
}
|
||||
@end
|
||||
|
||||
int fooError() {
|
||||
ViewController* vc = [[ViewController alloc] init];
|
||||
ViewController* vc2;
|
||||
vc2 = vc;
|
||||
[vc fired];
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fooOK1() {
|
||||
ViewController* vc = [[ViewController alloc] init];
|
||||
ViewController* vc2;
|
||||
vc2 = vc;
|
||||
[vc fired];
|
||||
[vc invalidate1];
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fooOK2() {
|
||||
ViewController* vc = [[ViewController alloc] init];
|
||||
ViewController* vc2;
|
||||
vc2 = vc;
|
||||
[vc fired];
|
||||
[vc invalidate2];
|
||||
return 0;
|
||||
}
|
||||
|
||||
int barError() {
|
||||
ViewController* vc = [[ViewController alloc] init];
|
||||
[vc fired];
|
||||
|
||||
vc = [[ViewController alloc] init];
|
||||
return 0;
|
||||
}
|
||||
|
||||
int barOK1() {
|
||||
ViewController* vc = [[ViewController alloc] init];
|
||||
[vc invalidate1];
|
||||
vc = [[ViewController alloc] init];
|
||||
[vc invalidate1];
|
||||
return 0;
|
||||
}
|
||||
|
||||
int barOK2() {
|
||||
ViewController* vc = [[ViewController alloc] init];
|
||||
[vc invalidate2];
|
||||
vc = [[ViewController alloc] init];
|
||||
[vc invalidate1];
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (c) 2015 - present Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package endtoend.objc;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static utils.matchers.ResultContainsExactly.containsExactly;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import utils.DebuggableTemporaryFolder;
|
||||
import utils.InferException;
|
||||
import utils.InferResults;
|
||||
import utils.InferRunner;
|
||||
|
||||
public class RegisteredObserver {
|
||||
|
||||
public static final String VCFile =
|
||||
"infer/tests/codetoanalyze/objc/errors/registered_observer/ViewController.m";
|
||||
|
||||
private static ImmutableList<String> inferCmd;
|
||||
|
||||
public static final String REGISTERED_OBSERVER = "REGISTERED_OBSERVER_BEING_DEALLOCATED";
|
||||
|
||||
@ClassRule
|
||||
public static DebuggableTemporaryFolder folder = new DebuggableTemporaryFolder();
|
||||
|
||||
@BeforeClass
|
||||
public static void runInfer() throws InterruptedException, IOException {
|
||||
inferCmd = InferRunner.createObjCInferCommandSimple(
|
||||
folder,
|
||||
VCFile,
|
||||
"cf");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void RegisteredObserverShouldBeFound()
|
||||
throws InterruptedException, IOException, InferException {
|
||||
InferResults inferResults = InferRunner.runInferObjC(inferCmd);
|
||||
String[] methods = {
|
||||
"fooError", "barError"
|
||||
};
|
||||
assertThat(
|
||||
"Results should contain " + REGISTERED_OBSERVER,
|
||||
inferResults,
|
||||
containsExactly(
|
||||
REGISTERED_OBSERVER,
|
||||
VCFile,
|
||||
methods
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue