Is it possible to use ARC on Mac in a Qt application?
-
I know in the past Qt was incompatible with ARC, is it still the case with Qt 6? Is there a way to enable ARC for my application without breaking Qt?
-
Hi,
Can you remind me what ARC is ? The name rings a really old distant bell.
-
Automatic reference counting, a feature of Objective-C that is usually enabled and expected in macOS applications, but Qt is incompatible with it, or at least used to be.
-
Automatic reference counting, a feature of Objective-C that is usually enabled and expected in macOS applications, but Qt is incompatible with it, or at least used to be.
@Violet-Giraffe said in Is it possible to use ARC on Mac in a Qt application?:
Automatic reference counting
I don't know what this is and never used Objective-C, but what about smart pointers, either the STL types in C++, like
std::unique_ptr
,std::shared_ptr
andstd::weak_ptr
or the matching Qt counterparts (QPointer
,QSharedPointer
,QScopedPointer
,QWeakPointer
, ... )? -
@Violet-Giraffe said in Is it possible to use ARC on Mac in a Qt application?:
Automatic reference counting
I don't know what this is and never used Objective-C, but what about smart pointers, either the STL types in C++, like
std::unique_ptr
,std::shared_ptr
andstd::weak_ptr
or the matching Qt counterparts (QPointer
,QSharedPointer
,QScopedPointer
,QWeakPointer
, ... )?@Pl45m4, that is unrelated. Of course I know how to use smart pointers. Our Qt application has started having inexplicable issues calling macOS frameworks (system functions), which happens in a Qt application but not in a clean native Mac application. ARC is one notable way in which the Qt application differs from the native application. And in general, not being able to use ARC has been a major pain for the Mac version of our application, so I would love to finally enable it if possible.
-
@Pl45m4, that is unrelated. Of course I know how to use smart pointers. Our Qt application has started having inexplicable issues calling macOS frameworks (system functions), which happens in a Qt application but not in a clean native Mac application. ARC is one notable way in which the Qt application differs from the native application. And in general, not being able to use ARC has been a major pain for the Mac version of our application, so I would love to finally enable it if possible.
Because Qt's platform independency, I doubt that there is something that specific (for adapted from one language and for mac only) in C++ or Qt.
After reading this thread on StackOverflow, I now know what it does/is, but this reply there explains why it wasn't introduced to C++ or Qt yet.
I'm not an expert when it comes to macOS, but if your C++ Qt app on Mac really needs this feature, it's probably a design issue with your app and there is also probably a better alternative/workaround for this (for memory management).
Edit:
I guess nothing has changed since then:
(if it would have been added already, it would be documented somewhere, for sure)Maybe someone who knows more than me or the Qt maintainers will comment on this :)
-
You're still missing the point, it's a trait of the application, ARC or no ARC. I don't need to use it, but I may need it to be enabled for other macOS stuff to function properly in this application. Yes, I've seen that article, but it's old.
-
ARC isn't actually a property of the application, or an all-or-nothing thing. It can be enabled or not for each compilation unit separately - just add
-fobjc-arc
to the compilation options of the relevant Objective-C++.mm
files. AFAIK Clang's Objective-C compiler's implementation of ARC doesn't do full-project analysis, but just relies on the selector naming conventions to deduce type of ownership. So whether Qt's Cocoa platform integration and style use ARC or MRC shouldn't matter.That said, the problem with enabling ARC on Qt applications seems to be primarily that ARC forbids casting between pointers to Objective-C objects and C/C++ types. So something pretty common to do like casting the result of
QWidget::winId
to aNSView*
is a compilation error under ARC:error: cast of 'WId' (aka 'unsigned long long') to 'NSView *' is disallowed with ARC NSView* view = reinterpret_cast<NSView*>(parent->winId());