From 26697704dc987e426e5d184cec8d46f59b60808a Mon Sep 17 00:00:00 2001 From: Dulma Churchill Date: Fri, 9 Mar 2018 09:13:47 -0800 Subject: [PATCH] [retain cycles] Example of a cycle that includes a C++ class Reviewed By: mbouaziz Differential Revision: D7213885 fbshipit-source-id: e460f6a --- .../codetoanalyze/objcpp/errors/Makefile | 18 +++++ .../codetoanalyze/objcpp/errors/issues.exp | 1 + .../retain_cycles/RetainCycleWithStruct.mm | 69 +++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 infer/tests/codetoanalyze/objcpp/errors/Makefile create mode 100644 infer/tests/codetoanalyze/objcpp/errors/issues.exp create mode 100644 infer/tests/codetoanalyze/objcpp/errors/retain_cycles/RetainCycleWithStruct.mm diff --git a/infer/tests/codetoanalyze/objcpp/errors/Makefile b/infer/tests/codetoanalyze/objcpp/errors/Makefile new file mode 100644 index 000000000..170784f47 --- /dev/null +++ b/infer/tests/codetoanalyze/objcpp/errors/Makefile @@ -0,0 +1,18 @@ +# 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. + +TESTS_DIR = ../../.. + +ANALYZER = checkers +CLANG_OPTIONS = -x objective-c++ -std=c++11 -fobjc-arc -c +INFER_OPTIONS = --biabduction-only --no-filtering --debug-exceptions --project-root $(TESTS_DIR) +INFERPRINT_OPTIONS = --issues-tests + +SOURCES = \ + $(wildcard */*.mm) \ + +include $(TESTS_DIR)/clang.make diff --git a/infer/tests/codetoanalyze/objcpp/errors/issues.exp b/infer/tests/codetoanalyze/objcpp/errors/issues.exp new file mode 100644 index 000000000..888a96021 --- /dev/null +++ b/infer/tests/codetoanalyze/objcpp/errors/issues.exp @@ -0,0 +1 @@ +codetoanalyze/objcpp/errors/retain_cycles/RetainCycleWithStruct.mm, main_bad, 2, RETAIN_CYCLE, [start of procedure main_bad(),start of procedure tracer,start of procedure _State,return from a call to _State__State,start of procedure initWithAnimation:,Condition is true,return from a call to Tracer_initWithAnimation:,return from a call to Animation_tracer] diff --git a/infer/tests/codetoanalyze/objcpp/errors/retain_cycles/RetainCycleWithStruct.mm b/infer/tests/codetoanalyze/objcpp/errors/retain_cycles/RetainCycleWithStruct.mm new file mode 100644 index 000000000..b8e5cf1db --- /dev/null +++ b/infer/tests/codetoanalyze/objcpp/errors/retain_cycles/RetainCycleWithStruct.mm @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2018 - 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 + +@class Tracer; + +struct _State { + Tracer* tracer; + + virtual ~_State() { tracer = nil; } +}; + +@interface Animation : NSObject +- (Tracer*)tracer; +@end + +@interface Animation () { + @public + struct _State* _state; +} + +@end + +@interface Tracer : NSObject +- (instancetype)initWithAnimation:(Animation*)a; +@end + +@implementation Animation + +- (Tracer*)tracer { + _state = new _State(); + _state->tracer = [[Tracer alloc] initWithAnimation:self]; + return _state->tracer; +} + +- (void)dealloc { + NSLog(@"dealloc Animation"); +} + +@end + +@implementation Tracer { + _State* _state; +} + +- (id)initWithAnimation:(Animation*)a { + self = [super init]; + if (nil != self) { + _state = a->_state; + } + return self; +} + +- (void)dealloc { + NSLog(@"dealloc Tracer"); +} +@end + +int main_bad() { + Animation* a = [Animation new]; + Tracer* tracer = [a tracer]; + return 0; +}