Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I do not know the APIs you are using, so I'm not 100% sure of what's going on. I googled and they seem to be part of OCMock. I downloaded it and (without installing it as I'm not interested) I rapidly browsed the source.</p> <p>I see something very fishy in that code. Here's how they implement the first method you call:</p> <pre><code>@implementation OCMArg .... + (id *)setTo:(id)value { return (id *)[[[OCMPassByRefSetter alloc] initWithValue:value] autorelease]; } </code></pre> <p>So they are returning an <code>id*</code> which is really just an <code>id</code>.</p> <p>To me that's either a nonsense/error or an attempt to manipulate ObjC internals (even if undocumented, the first thing an ObjC object stores is in fact a pointer to the object class and is therefore of type <code>Class</code> which is compatible with <code>id</code>, therefore it somehow is valid to cast a pointer to an object or an <code>id</code> that refers to an object, to <code>Class*</code> or <code>id*</code>). I have no time or interest in going and studying the whole API to figure out why they do that. They may actually have a good reason (for example if you only pass that result to another API that knows what it's supposed to be, but you are doing more than that here). Instead of studying OCMock I'll try to explain you what is happening as far as I can say (ObjC and ARC).</p> <pre><code>id __autoreleasing *arg = [OCMArg setTo:mockData]; </code></pre> <p>ARC will do absolutely nothing in this line of code. </p> <p>What that method does you can see above. Class <code>OCMPassByRefSetter</code> is a simple class that just stores the argument after retaining it, so <code>mockData</code> is retained. The <code>OCMPassByRefSetter</code> is autoreleased and will disappear at the next drain (releasing the <code>mockData</code> <strong>and</strong> making <code>*arg</code> reference to released memory).</p> <p>Note that <code>arg</code> in fact points to the <code>isa</code> of the <code>OCMPassByRefSetter</code> (the <code>isa</code> is the "first" ivar of any object, it's of type <code>Class</code> and points to the class of the object. But this is undocumented and may change at any time). </p> <pre><code>CFTypeRef expectedResult = (__bridge CFTypeRef) *arg; </code></pre> <p><code>*arg</code> is of type <code>id</code> which is compatible with <code>CFTypeRef</code>, so the cast is valid. You use <code>__bridge</code> so ARC does absolutely nothing.</p> <p>If <code>arg</code> pointed to a "toll free bridged" CF/Cocoa class this would be perfectly valid code but you'd have to be careful that <code>expectedResult</code> would become invalid at the next drain (it's not <code>retained</code>, but it's live as an autoreleased instance).</p> <pre><code>[[[self.mockSecItemService expect] andReturnValue:OCMOCK_VALUE(mockCopyStatus)] copyItemMatching:queryCheck result:&amp;expectedResult]; </code></pre> <p>No idea what this line does. Given the prototype you posted in the comment above, ARC does nothing on the part <code>result:&amp;expectedResult</code>.</p> <p>You say it's a wrapper around <code>SecItemCopyMatching</code>, but as I understand it it's more than that. If it was just immediately calling <code>SecItemCopyMatching</code> passing it the <code>result:</code> argument, you'd likely be messing things up. But the name <code>expectedResult</code> and the fact that this is OCMock makes me think this is a little more complex than that.</p> <p>You'll have to investigate it yourself a bit. But remember:</p> <ul> <li>as soon as the current function exits, the argument you passed (<code>&amp;expectedResult</code>) will become invalid as it's a local variable.</li> <li>as soon as there is a drain, the <em>value</em> of <code>expectedResult</code> will become invalid, as that address points to memory that is going to be deallocated by the drain.</li> <li>doing <em>anything</em> with the value of <code>expectedResult</code> is likely do be going very wrong as I do not think that a <code>Class</code> qualifies as "toll free bridged".</li> </ul> <p>I suspect, but I may be very wrong, that you are not using the OCMock apis the way they are intended to be used. But on this front I cannot help you, and maybe you are actually doing it right.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload