{"action":"opened","issue":{"url":"https://api.github.com/repos/pivotal/cedar/issues/319","labels_url":"https://api.github.com/repos/pivotal/cedar/issues/319/labels{/name}","comments_url":"https://api.github.com/repos/pivotal/cedar/issues/319/comments","events_url":"https://api.github.com/repos/pivotal/cedar/issues/319/events","html_url":"https://github.com/pivotal/cedar/issues/319","id":63018348,"number":319,"title":"EXC_BAD_ACCESS Crash with stubs","user":{"login":"KineticCafeAdmin","id":11218090,"avatar_url":"https://avatars.githubusercontent.com/u/11218090?v=3","gravatar_id":"","url":"https://api.github.com/users/KineticCafeAdmin","html_url":"https://github.com/KineticCafeAdmin","followers_url":"https://api.github.com/users/KineticCafeAdmin/followers","following_url":"https://api.github.com/users/KineticCafeAdmin/following{/other_user}","gists_url":"https://api.github.com/users/KineticCafeAdmin/gists{/gist_id}","starred_url":"https://api.github.com/users/KineticCafeAdmin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KineticCafeAdmin/subscriptions","organizations_url":"https://api.github.com/users/KineticCafeAdmin/orgs","repos_url":"https://api.github.com/users/KineticCafeAdmin/repos","events_url":"https://api.github.com/users/KineticCafeAdmin/events{/privacy}","received_events_url":"https://api.github.com/users/KineticCafeAdmin/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-03-19T15:29:40Z","updated_at":"2015-03-19T15:29:40Z","closed_at":null,"body":"I'm getting EXC_BAD_ACCESS once every 5 or so times I run my tests with Cedar. The issue arises in an NSView's addSubview: method; the view isn't a mock, but a real NSView.\r\n\r\nHere is my test:\r\n\r\n```smalltalk\r\n#import <Cedar/Cedar.h>\r\n#import \"NSModalNavigationController.h\"\r\n#import \"NSView+Animation.h\"\r\n\r\nusing namespace Cedar::Matchers;\r\nusing namespace Cedar::Doubles;\r\n\r\nSPEC_BEGIN(NSModalNavigationControllerSpec)\r\n\r\ndescribe(@\"NSModalNavigationController:\", ^{\r\n    \r\n    __block NSModalNavigationController *navigationController = nil;\r\n    __block NSModalViewController *rootViewController = nil;\r\n    __block id <CedarDouble> rootViewControllerContentViewMock = nil;\r\n    \r\n    beforeEach(^{\r\n        \r\n        rootViewControllerContentViewMock = nice_fake_for([NSView class]);\r\n        \r\n        rootViewController = [NSModalViewController new];\r\n        \r\n        spy_on(rootViewController);\r\n        \r\n        rootViewController stub_method(@selector(contentView)).and_return(rootViewControllerContentViewMock);\r\n        \r\n        navigationController = [[NSModalNavigationController alloc] initWithRootViewController:rootViewController];\r\n    });\r\n    \r\n    afterEach(^{\r\n        \r\n        stop_spying_on(rootViewController);\r\n    });\r\n    \r\n    it(@\"should have a root view controller\", ^{\r\n        \r\n        navigationController.viewControllers.count should equal(1);\r\n        navigationController.topViewController should equal(rootViewController);\r\n    });\r\n    \r\n    describe(@\"when adding another view controller\", ^{\r\n        \r\n        __block NSModalViewController *newViewController = nil;\r\n        __block id <CedarDouble> newViewControllerContentViewMock = nil;\r\n        \r\n        beforeEach(^{\r\n            \r\n            newViewControllerContentViewMock = nice_fake_for([NSView class]);\r\n            \r\n            newViewController = [NSModalViewController new];\r\n            \r\n            spy_on(newViewController);\r\n            \r\n            newViewController stub_method(@selector(contentView)).and_return(newViewControllerContentViewMock);\r\n            \r\n            rootViewControllerContentViewMock stub_method(@selector(setHidden:animated:completionBlock:));\r\n            \r\n            spy_on(navigationController.contentView);\r\n            \r\n            navigationController.contentView stub_method(@selector(addSubview:));\r\n        });\r\n        \r\n        afterEach(^{\r\n            \r\n            stop_spying_on(newViewController);\r\n            stop_spying_on(navigationController.contentView);\r\n        });\r\n        \r\n        it(@\"should have added the new view and tried to hide the previous view\", ^{\r\n            \r\n            [navigationController presentViewController:newViewController];\r\n            \r\n            rootViewControllerContentViewMock should have_received(@selector(setHidden:animated:completionBlock:));\r\n            navigationController.contentView should have_received(@selector(addSubview:)).with(newViewControllerContentViewMock);\r\n        });\r\n        \r\n        describe(@\"and dismissing that modal view controller\", ^{\r\n            \r\n            beforeEach(^{\r\n                \r\n                spy_on(navigationController);\r\n                \r\n                navigationController stub_method(@selector(viewControllers)).and_return(@[rootViewController, newViewController]);\r\n            });\r\n            \r\n            afterEach(^{\r\n                \r\n                stop_spying_on(navigationController);\r\n            });\r\n            \r\n            it(@\"should have tried to hide current view and add preview view\", ^{\r\n                \r\n                [navigationController dismissLastViewController];\r\n                \r\n                newViewControllerContentViewMock should have_received(@selector(setHidden:animated:completionBlock:));\r\n                // TODO: Cannot alter view controllers mid method, so this test is technically incorrect, but can simulate correct behaviour\r\n                navigationController.contentView should have_received(@selector(addSubview:)).with(newViewControllerContentViewMock);\r\n            });\r\n        });\r\n    });\r\n});\r\n\r\nSPEC_END\r\n```\r\n\r\nHere is the code for NSModalNavigationController : NSModalViewController\r\n\r\n```smalltalk\r\n- (id)initWithRootViewController:(NSModalViewController *)rootViewController\r\n{\r\n    if(self = [super init])\r\n    {\r\n        [rootViewController setModalNavigationController:self];\r\n        \r\n        [self adjustControllerContentView:rootViewController];\r\n        [self.contentView addSubview:rootViewController.contentView];\r\n        \r\n        viewControllers = [NSMutableArray arrayWithObject:rootViewController];\r\n        \r\n        //Set up inactivity listening for hibernation state\r\n        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inactivityNotificationReceived:) name:kTouchManagerInactivityNotificationKey object:nil];\r\n    }\r\n    return self;\r\n}\r\n```\r\n\r\nAnd here is the code for NSModalViewController that is run during the initial setup of the tests\r\n\r\n```smalltalk\r\n- (void)viewDidLoad {\r\n    [super viewDidLoad];\r\n    \r\n    self.overlayView = [ApplicationStyle overlayViewWithFrame:self.view.bounds];\r\n    [self.view addSubview:self.overlayView];\r\n    \r\n    KTapGestureRecognizer *tapGestureRecognizer = [[KTapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissModalViewController)];\r\n    [self.overlayView addGestureRecognizer:tapGestureRecognizer];\r\n    \r\n    self.contentView = [[NSView alloc]initWithFrame:NSMakeRect(0.0f, 0.0f, DEFAULT_CONTENT_WIDTH, DEFAULT_CONTENT_HEIGHT)];\r\n    [self.contentView setWantsLayer:YES];\r\n    [self.contentView.layer setBackgroundColor:[NSColor whiteColor].CGColor];\r\n    [self.contentView centerInWidth:self.view.viewWidth];\r\n    [self.contentView centerInHeight:self.view.viewHeight];\r\n    [self.view addSubview:self.contentView];\r\n}\r\n\r\n-(void)loadView\r\n{\r\n    [self setView:[[NSView alloc] initWithFrame:[NSScreen mainScreen].frame]];\r\n}\r\n\r\n- (NSView *)contentView\r\n{\r\n    @synchronized(self)\r\n    {\r\n        if(!self.view)return nil;\r\n        return _contentView;\r\n    }\r\n}\r\n```\r\n\r\nI'm getting EXC_BAD_ACCESS on the line:\r\n\r\n        navigationController = [[NSModalNavigationController alloc] initWithRootViewController:rootViewController];\r\n\r\nin my tests and the bad access is on addSubview for the line:\r\n\r\n        [self.contentView addSubview:rootViewController.contentView];\r\n\r\nIn NSModalNavigationController.\r\n\r\nSince the bad access is in a private method (addSubview:), I cannot provide more insight into what is going on, but it seems to be a threading issue with Cedar. I've never experienced a crash with my code, but only when Cedar is running the tests - also, I'm not sure why addSubview: would ever throw bad access outside of tests."}}