Saturday, June 11, 2016

How has OCMock broken shared_ptrs in partial mocks?

1 comment

I'd like to understand the change that lead to this bug: Shared pointers are deallocated in 3.1 and 3.2 when you partial mock, causing EXC_BAD_ACCESS.

In particular, how do C++ members of Objective-C classes interact with the runtime?

Some excerpts from the gist:

#include "someCplusplusfile.hpp" std::string someCplusplusfile::getId() {    return "someString"; } 
#import <Foundation/Foundation.h> #ifdef __cplusplus #include "someCplusplusfile.hpp" #endif @interface AnMMFile : NSObject @property (readonly) NSString *name; -(id) init; #ifdef __cplusplus @property (readonly) std::shared_ptr<someCplusplusfile> someCplusplusfile; #endif -(NSString*) getInternalString; @end 
#import "AnMMFile.h" @implementation AnMMFile {     std::shared_ptr<someCplusplusfile> _someCplusplusfile;     NSString *_name; } - (id)init {     if ((self = [super init])) {         _someCplusplusfile = std::make_shared<someCplusplusfile>();         _name = @"someName";     }     return self; } -(NSString*) getInternalString {     return [NSString stringWithCString:_someCplusplusfile->getId().c_str() encoding:[NSString defaultCStringEncoding]]; } - (std::shared_ptr<someCplusplusfile>)someCplusplusfile {     return _someCplusplusfile; } @end 
#import <XCTest/XCTest.h> #import <OCMock/OCMock.h> #import "AnMMFile.h" #import "someCplusplusfile.hpp"  @interface SharedPointerIssueTests : XCTestCase @end  @implementation SharedPointerIssueTests  - (void)testMock {     AnMMFile *anmmFile = [[AnMMFile alloc] init];     id partialMock = [OCMockObject partialMockForObject:anmmFile];     // next line fails with a EXC_BAD_ACCESS for OCMock 3, is fine on OCMock 2     std::shared_ptr<someCplusplusfile> aNativeObject2 = anmmFile.someCplusplusfile; 

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

1 comment:

  1. Are you looking to earn money from your visitors via popunder ads?
    If so, did you take a look at Propeller Ads?

    ReplyDelete