Re: Cocoa-dev Digest, Vol 16, Issue 72
Re: Cocoa-dev Digest, Vol 16, Issue 72
- Subject: Re: Cocoa-dev Digest, Vol 16, Issue 72
- From: Uli Kusterer via Cocoa-dev <email@hidden>
- Date: Wed, 28 Aug 2019 01:38:58 +0200
On 8/26/2019 2:49 AM, Turtle Creek Software via Cocoa-dev wrote:
> In the hybrid C++/Obj-C++ files, we use __bridge on all the casts of void
> pointers to Cocoa object > No use of NSBridgingRetain or Release at
all. Is that necessary
under ARC?
NSBridgingRetain() etc. are basically wrappers around the requisite
bridge casts. And yes, in fact they are *only* necessary under ARC (in
MRC you just use retain/release).
When taking a pointer managed by ARC, you *have* to explicitly retain it
if you want to be able to safely pass it elsewhere beyond the lifetime
of whatever ObjC variable it is in. if you don't do that, ARC will think
the pointer is no longer used and will free it, leaving your C++ classes
with dangling pointers. I strongly recommend reading up on those, and in
particular when to use which of the several casts. In general, you can't
just use the Fix It's suggestion as it doesn't know whether an object
needs to be retained or not.
> The void pointers are mostly to text fields and controls, plus the app
> delegate
> and one view. None of those have had lifetime problems. We mostly just
> access them and have them do one thing.
>
> No void pointers to the window controller that is dying, nor its window.
By the way, I usually use forward declarations like
#if __OBJC__
@class NSButton;
#else
struct NSButton;
#endif
or the like. Structs name-mangle the same in C++ as the equivalent ObjC
classes, but are recognized by a C++ compiler. Then I create an ObjC++
class whose member variables use that ObjC type, and only have ObjC in
the implementation file.
Gives you the type-safety you're used to, and readable code, but
prevents ObjC++ from leaking into pure C++ code.
I've mostly been doing Swift the past year, but I vaguely remember that
ARC (but maybe only on iOS) does not retain certain objects that MRC
used to. In particular IBOutlets. So unless you declare them as strong,
they don't actually retain. Have you checked that you're not coding
against pre-ARC retain/release rules?
It could be that your object is not strongly referenced and therefore
immediately released.
Cheers,
-- Uli Kusterer
http://www.zathras.de
"The Witnesses of TeachText are everywhere..."
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden