我试图“专门化” QQmlComponent 的 std::default_delete 但没有成功,我的实现从未被调用,只调用 ::std 的默认删除器。因此,出于调试目的,我为 QObject 创建了另一个专业化,这个专业化称为:
在我的 util.hi 中有:
namespace std {
// QQmlComponent often theses objects
// are owned by another thread despite their QQmlEngine::CppOwnership, using delete operator
// directly generate crashes, specify a default_delete specifically for QQmlComponent to
// use a deleteLater() call over a raw delete
template<>
struct default_delete<QQmlComponent> {
void operator()(QQmlComponent* ptr) {
if ( ptr &&
QQmlEngine::objectOwnership(ptr) == QQmlEngine::CppOwnership )
ptr->deleteLater();
}
};
template<>
struct default_delete<QObject> {
void operator()(QObject* ptr) {
///...
};
}
在我的 utils.cpp 我有:
std::unique_ptr<QObject> testQObject= std::make_unique<QObject>(new QObject());
std::unique_ptr<QQmlComponent> connectorComponent = std::make_unique<QQmlComponent>(engine, QStringLiteral("qrc:/QuickQanava/VisualConnector.qml"));
在testQObject
专门default_delete
的被称为而不是在connectorComponent
我不明白我做错了什么。作为旁注,此代码在共享库中实现