title: How to safely store an id object in a C++ void* member under ARC when no other references hold on to the object?
body: <p>I'm working with Box2D (C++) and I create an Objective-C object and assign it to a Box2D body's <code>userData</code> property, which is of type <code>void*</code>.</p>
  
  <p>Now in some cases the <code>void* userData</code> may be the only active reference to that ObjC object. Therefore, because I used <code>(__bridge void*)</code> in the assignment, ARC is letting it go. That's something I need to fix.</p>
  
  <p>I've been pondering the options to prevent this from happening? I read <a href="http://clang.llvm.org/docs/AutomaticReferenceCounting.html#arc-objects-restrictions-conversion">Clang's ARC documentation</a>, specifically the parts about bridge casting (as well as Q&amp;A on SO) as well as nodding to the various bridge casting constructs they consider to be "ill-formed".</p>
  
  <p>Still, my first thought was to use <code>(__bridge_retained void*)</code> in the initial assignment to userData. But that made me wonder how to balance that retain? I obviously can't send release to the object.</p>
  
  <p>So would I have to <code>CFRelease()</code> the object? Or would it need to be <code>CFBridgingRelease()</code>? Or are both illegal here?</p>
  
  <p>Is a <code>(__bridge_transfer void*)</code> cast from <code>userData</code> to a temporary id type enough, perhaps while setting userData to NULL afterwards? Is that even a good idea?</p>
  
  <p>I know the alternative would be to keep a separate <code>NSArray</code>/<code>NSDictionary</code> for the <code>userData</code> objects and keep them in synch with the lifetime of the Box2D body, adding and removing them in synch with their Box2D bodies. </p>
  
  <p>But this feels like overkill because here I know what I'm doing, I know I need to <code>+1</code> the object for as long as the Box2D body is active, and <code>-1</code> the object when the Box2D body is removed. Plus I know that there are only two methods where the Box2D bodies are being added and removed, and direct access to <code>userData</code> is not even possible in my framework because all Box2D objects are hidden behind Objective-C interfaces/wrappers. </p>
  
  <p>Setting possibly "ill-formed" aside for a moment, what would you recommend I should do in this situation?</p>