2012-05-22 67 views
3

我最近在以下問題在那裏我有問題與線程被殺害之前沒有被清除在線程對象:移動到線程引起問題

Thread count increases a lot, even when deleting the threads

我決定到移動對象的主線程刪除它之前像這樣:

void WorkspaceBase::deleteFinishedUploader() { 
    FileUploader *fileUploader = qobject_cast<FileUploader*>(sender()); 

    if(fileUploader != NULL) { 
     fileUploader->moveToThread(this->thread()); 
     fileUploader->deleteLater(); 
    } 
} 

在Windows中,我從來沒有得到任何警告,它似乎工作正常。現在,在OSX,我得到以下警告eveytime做到這一點:

On Mac OS X, you might be loading two sets of Qt binaries into the same process. Check that all plugins are compiled against the right Qt binaries. Export DYLD_PRINT_LIBRARIES=1 and check that only one set of binaries are being loaded. 
QObject::moveToThread: Current thread (0x102a07180) is not the object's thread (0x116428090). 
Cannot move to target thread (0x102a07180) 

我也不太清楚是什麼問題,但會盡力來形容我的項目,以及我可以。

首先,我有一個共享的dylib,被我的其他dylib和app使用。另一個dylib使用Shared.dylib,並在向網絡(QNetworkAccessManager等)上傳東西時包含所有的功能。然後我有我的應用程序,其中包括這個dylib,以及shared.dylib(其中包含一些輔助方法和解析器)。這些庫以及來自boost和qjson.framework的date_time.dylib被添加到.app-package。

一切都建立並運行完美,只有當我試圖將上傳對象移動到發生此問題的主線程。上傳器對象是應用程序可執行文件的一部分,不在.dylib中(但它使用庫進行上載)。

這裏有什麼問題?不確定我之前是否遇到過這個問題,而且我也不太確定我做了什麼。如果我提供的信息不夠,我很樂意添加更多信息。當我不知道問題是什麼時,很難知道去哪裏看。

謝謝!

編輯:

這些都是在運行時我正在裝載庫:

dyld: loaded: /Users/me/Documents/MyApp/MyApp.app/Contents/MacOS/./MyApp 
dyld: loaded: /Users/me/Documents/MyApp/MyApp.app/Contents/MacOS/qjson.framework/Versions/0/qjson 
dyld: loaded: /Users/me/Documents/MyApp/MyApp.app/Contents/MacOS/libboost_date_time.dylib 
dyld: loaded: /Users/me/Documents/MyApp/MyApp.app/Contents/MacOS/libQt.Shared.1.dylib 
dyld: loaded: /Users/me/Documents/MyApp/MyApp.app/Contents/MacOS/libQt.Framework.Network.1.dylib 
dyld: loaded: /Users/me/QtSDK1.2.1/Desktop/Qt/4.8.1/gcc/lib/QtGui.framework/Versions/4/QtGui 
dyld: loaded: /Users/me/QtSDK1.2.1/Desktop/Qt/4.8.1/gcc/lib/QtCore.framework/Versions/4/QtCore 
dyld: loaded: /Users/me/QtSDK1.2.1/Desktop/Qt/4.8.1/gcc/lib/QtNetwork.framework/Versions/4/QtNetwork 
dyld: loaded: /usr/lib/libstdc++.6.dylib 
dyld: loaded: /usr/lib/libSystem.B.dylib 
dyld: loaded: /usr/lib/libz.1.dylib 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices 
dyld: loaded: /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation 
dyld: loaded: /System/Library/Frameworks/Security.framework/Versions/A/Security 
dyld: loaded: /System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices 
dyld: loaded: /usr/lib/system/libmathCommon.A.dylib 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/ImageIO 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/CoreText.framework/Versions/A/CoreText 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ATS.framework/Versions/A/ATS 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ColorSync.framework/Versions/A/ColorSync 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/HIServices.framework/Versions/A/HIServices 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/LangAnalysis.framework/Versions/A/LangAnalysis 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/PrintCore.framework/Versions/A/PrintCore 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/QD.framework/Versions/A/QD 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/SpeechSynthesis.framework/Versions/A/SpeechSynthesis 
dyld: loaded: /usr/lib/libbsm.0.dylib 
dyld: loaded: /usr/lib/libicucore.A.dylib 
dyld: loaded: /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit 
dyld: loaded: /usr/lib/system/libkxld.dylib 
dyld: loaded: /usr/lib/libauto.dylib 
dyld: loaded: /usr/lib/libobjc.A.dylib 
dyld: loaded: /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CarbonCore.framework/Versions/A/CarbonCore 
dyld: loaded: /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CFNetwork.framework/Versions/A/CFNetwork 
dyld: loaded: /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/Metadata.framework/Versions/A/Metadata 
dyld: loaded: /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/OSServices.framework/Versions/A/OSServices 
dyld: loaded: /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/SearchKit.framework/Versions/A/SearchKit 
dyld: loaded: /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/AE.framework/Versions/A/AE 
dyld: loaded: /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/LaunchServices 
dyld: loaded: /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/DictionaryServices.framework/Versions/A/DictionaryServices 
dyld: loaded: /System/Library/Frameworks/DiskArbitration.framework/Versions/A/DiskArbitration 
dyld: loaded: /System/Library/Frameworks/NetFS.framework/Versions/A/NetFS 
dyld: loaded: /usr/lib/libsqlite3.dylib 
dyld: loaded: /System/Library/Frameworks/SystemConfiguration.framework/Versions/A/SystemConfiguration 
dyld: loaded: /usr/lib/libxslt.1.dylib 
dyld: loaded: /usr/lib/libxml2.2.dylib 
dyld: loaded: /System/Library/Frameworks/Accelerate.framework/Versions/A/Accelerate 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/Resources/libTIFF.dylib 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/Resources/libGIF.dylib 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/Resources/libPng.dylib 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/Resources/libRadiance.dylib 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/Resources/libJPEG.dylib 
dyld: loaded: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vImage.framework/Versions/A/vImage 
dyld: loaded: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/vecLib 
dyld: loaded: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libvDSP.dylib 
dyld: loaded: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libvMisc.dylib 
dyld: loaded: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
dyld: loaded: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib 
dyld: loaded: /usr/lib/libcrypto.0.9.8.dylib 
dyld: loaded: /System/Library/PrivateFrameworks/TrustEvaluationAgent.framework/Versions/A/TrustEvaluationAgent 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ATS.framework/Versions/A/Resources/libFontParser.dylib 
dyld: loaded: /usr/lib/libcups.2.dylib 
dyld: loaded: /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation 
dyld: loaded: /System/Library/Frameworks/Kerberos.framework/Versions/A/Kerberos 
dyld: loaded: /usr/lib/libresolv.9.dylib 
dyld: loaded: /Users/annatisch/QtSDK1.2.1/Desktop/Qt/4.8.1/gcc/lib/QtXml.framework/Versions/4/QtXml 
dyld: loaded: /System/Library/Frameworks/Carbon.framework/Versions/A/Carbon 
dyld: loaded: /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit 
dyld: loaded: /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/CommonPanels.framework/Versions/A/CommonPanels 
dyld: loaded: /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/Help.framework/Versions/A/Help 
dyld: loaded: /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/HIToolbox 
dyld: loaded: /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/ImageCapture.framework/Versions/A/ImageCapture 
dyld: loaded: /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/Ink.framework/Versions/A/Ink 
dyld: loaded: /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/OpenScripting.framework/Versions/A/OpenScripting 
dyld: loaded: /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/Print.framework/Versions/A/Print 
dyld: loaded: /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/SecurityHI.framework/Versions/A/SecurityHI 
dyld: loaded: /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/SpeechRecognition.framework/Versions/A/SpeechRecognition 
dyld: loaded: /System/Library/PrivateFrameworks/CoreUI.framework/Versions/A/CoreUI 
dyld: loaded: /System/Library/Frameworks/QuartzCore.framework/Versions/A/QuartzCore 
dyld: loaded: /System/Library/PrivateFrameworks/DesktopServicesPriv.framework/Versions/A/DesktopServicesPriv 
dyld: loaded: /System/Library/Frameworks/AudioToolbox.framework/Versions/A/AudioToolbox 
dyld: loaded: /System/Library/PrivateFrameworks/FamilyControls.framework/Versions/A/FamilyControls 
dyld: loaded: /System/Library/PrivateFrameworks/MultitouchSupport.framework/Versions/A/MultitouchSupport 
dyld: loaded: /System/Library/Frameworks/CoreVideo.framework/Versions/A/CoreVideo 
dyld: loaded: /System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL 
dyld: loaded: /System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGLImage.dylib 
dyld: loaded: /System/Library/Frameworks/OpenCL.framework/Versions/A/OpenCL 
dyld: loaded: /System/Library/Frameworks/IOSurface.framework/Versions/A/IOSurface 
dyld: loaded: /System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGLU.dylib 
dyld: loaded: /System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib 
dyld: loaded: /System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGLProgrammability.dylib 
dyld: loaded: /System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libCoreVMClient.dylib 
dyld: loaded: /System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGFXShared.dylib 
dyld: loaded: /System/Library/Frameworks/CoreAudio.framework/Versions/A/CoreAudio 
dyld: loaded: /System/Library/Frameworks/AudioUnit.framework/Versions/A/AudioUnit 
dyld: loaded: /System/Library/Frameworks/vecLib.framework/Versions/A/vecLib 
dyld: loaded: /System/Library/Frameworks/SecurityFoundation.framework/Versions/A/SecurityFoundation 
dyld: loaded: /System/Library/Frameworks/OpenDirectory.framework/Versions/A/OpenDirectory 
dyld: loaded: /System/Library/PrivateFrameworks/CommerceKit.framework/Versions/A/Frameworks/CommerceCore.framework/Versions/A/CommerceCore 
dyld: loaded: /System/Library/Frameworks/OpenDirectory.framework/Versions/A/Frameworks/CFOpenDirectory.framework/Versions/A/CFOpenDirectory 
dyld: loaded: /System/Library/PrivateFrameworks/PasswordServer.framework/Versions/A/PasswordServer 
dyld: loaded: /System/Library/Frameworks/DirectoryService.framework/Versions/A/DirectoryService 
dyld: loaded: /usr/lib/libsasl2.2.dylib 
dyld: loaded: /System/Library/Frameworks/CoreData.framework/Versions/A/CoreData 
dyld: loaded: /usr/lib/liblangid.dylib 
dyld: loaded: /usr/lib/libiconv.2.dylib 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/CoreGraphics.framework/Versions/A/Resources/libCSync.A.dylib 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Frameworks/ColorSync.framework/Versions/A/Resources/ColorSyncDeprecated.dylib 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/CoreGraphics.framework/Versions/A/Resources/libRIP.A.dylib 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ATS.framework/Versions/A/Resources/libFontRegistry.dylib 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/CoreGraphics.framework/Versions/A/Resources/libCGXType.A.dylib 
dyld: loaded: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ATS.framework/Versions/A/Resources/libTrueTypeScaler.dylib 
QDir( 
dyld: loaded: /Users/me/QtSDK1.2.1/Desktop/Qt/4.8.1/gcc/plugins/bearer/libqcorewlanbearer.dylib 
dyld: loaded: /System/Library/Frameworks/CoreWLAN.framework/Versions/A/CoreWLAN 
dyld: loaded: /Users/me/QtSDK1.2.1/Desktop/Qt/4.8.1/gcc/plugins/bearer/libqgenericbearer.dylib 
dyld: loaded: /usr/lib/libssl.0.9.8.dylib 

我看不到任何重複因此它必須有與物體的移動辦。我怎麼能夠將對象移動到主線程的主線程。我曾經在某處讀過,這是出於安全考慮而無法實現的,但我真的需要將它移動到可以完全刪除的位置(這意味着它需要一個事件循環,而不是其當前的QThread,它將被終止)

回答

6

QObject文檔:

警告:這個函數是不是線程安全的;當前線程必須是與當前線程關聯

所有者必須調用moveToThread放棄對象的所有權,而不是另一個線程聲稱對象的所有權。也就是說,您必須從擁有對象(默認創建的對象)中調用moveToThread。另請注意,您無法移動擁有父母的對象。

+0

我創建上傳,並將其移動到線程在同一類(主線程),這是我嘗試將其移回。上傳者沒有任何父母,但是上傳者具有將上傳者作爲父母的對象。 – chikuba

+0

「同班同學」不是你需要做的。如果threadA創建了上傳器,threadA是唯一可以將它移動到其他地方的@chikuba。 – Mat

+0

是的,treadA是試圖將其移回自身以便上傳者可以正確刪除的人 – chikuba

0

您也可以通過從對象的所有者線程中將其移動到您的線程。

#include <thread> 
#include <memory> 
#include <condition_variable> 
#include <QTimer> 
#include <QThread> 
#include <QApplication> 


template <typename Func> 
inline void runOnThread(QThread *qThread, Func &&func) 
{ 
    QTimer *t = new QTimer(); 
    t->moveToThread(qThread); 
    t->setSingleShot(true); 
    QObject::connect(t, &QTimer::timeout, [=]() 
    { 
     func(); 
     t->deleteLater(); 
    }); 
    QMetaObject::invokeMethod(t, "start", Qt::QueuedConnection, Q_ARG(int, 0)); 
} 



void moveToThread(QObject *ptr, QThread *targetThrd=QThread::currentThread()) 
{ 
    std::mutex mt; 
    std::condition_variable_any cv; 
    runOnThread(ptr->thread(),[&] 
    { 
     ptr->setParent(NULL); 
     ptr->moveToThread(targetThrd); 
     cv.notify_one(); 
    }); 
    cv.wait(mt); 
} 

你只需要調用

moveToThread(fileUploader, this->thread());