ocmock: Crash on Xcode 9 when mocking a NSManagedObject

I made a sample project to reproduce the crash: https://github.com/koke/ocmock-xcode9

What I know so far from debugging:

  • setupForwarderForClassMethodSelector is called successfully for a few selectors
  • setupForwarderForClassMethodSelector is called successfully for resolveClassMethod:
  • setupForwarderForClassMethodSelector is called for retain, then it enters infinite recursion and eventually crashes

This is the recurring part of the backtrace:

    frame #92313: 0x0000000109a784af libobjc.A.dylib`lookUpImpOrForward + 451
    frame #92314: 0x0000000109a782c1 libobjc.A.dylib`lookUpImpOrNil + 20
    frame #92315: 0x0000000109a6efa3 libobjc.A.dylib`class_respondsToSelector + 37
    frame #92316: 0x000000010a5b94ac CoreFoundation`___forwarding___ + 492
    frame #92317: 0x000000010a5b9238 CoreFoundation`__forwarding_prep_0___ + 120
    frame #92318: 0x0000000109a6ec61 libobjc.A.dylib`_class_resolveMethod + 124
    frame #92319: 0x0000000109a784af libobjc.A.dylib`lookUpImpOrForward + 451
    frame #92320: 0x0000000109a782c1 libobjc.A.dylib`lookUpImpOrNil + 20
    frame #92321: 0x0000000109a6efa3 libobjc.A.dylib`class_respondsToSelector + 37
    frame #92322: 0x000000010a5b94ac CoreFoundation`___forwarding___ + 492
    frame #92323: 0x000000010a5b9238 CoreFoundation`__forwarding_prep_0___ + 120
    frame #92324: 0x0000000109a6ec61 libobjc.A.dylib`_class_resolveMethod + 124
    frame #92325: 0x0000000109a784af libobjc.A.dylib`lookUpImpOrForward + 451
    frame #92326: 0x0000000109a78251 libobjc.A.dylib`class_getInstanceMethod + 53
  * frame #92327: 0x00000001211fd2f9 ocmock-xcode9-test`-[OCClassMockObject setupForwarderForClassMethodSelector:](self=0x0000618001060140, _cmd="setupForwarderForClassMethodSelector:", selector="retain") at OCClassMockObject.m:149

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 9
  • Comments: 38 (17 by maintainers)

Commits related to this issue

Most upvoted comments

Thank you. I am aware of the problem. Haven’t had a chance to look into it in detail. Please do not add more “me too” comments.

Don’t mock NSManagedObject. It will never work.

Thanks for confirming. I’ll get a release out when I have a moment, hopefully on Monday.

Mocking managed objects is actually a feature since OCMock v3.4. It seems fine with Xcode 9 when a iOS 10 or any macOS SDK is selected, it only breaks on iOS 11. I ran the OCMockTests on an iOS 11 simulator and they crash as well (-[OCMockObjectPartialMocksTests testMockingManagedObject]) with the same stack trace.

Any ideas on this? We use mogenerator and have custom logic in our NSManagedObject subclasses so there aren’t any protocols that we can mock instead of the managed objects.

Hi, i had the same problem with a crash for partialMockForObject with OCMock 3.4.1.

Adding the changes from https://github.com/erikdoe/ocmock/pull/354/commits/9d2a803dedfe625983684fa7b2b78a2d241f62b8 fixed the crash and the (old) test seem also to work.

Please add this to a new Release

As I wrote in my comment on 11 Nov, I don’t understand what the proposed change does. The fix I added should cover subclasses. As I wrote back then: Do you have a test case that fails without your change but passes with it? Did I miss a comment somewhere?