Results 1 to 4 of 4

Thread: Custom Deleter for QSharedPointer

  1. #1
    Join Date
    May 2009
    Posts
    1

    Default Custom Deleter for QSharedPointer

    UPD: It all works as expected, sorry for my mistake. Please, delete or close this topic.

    Hi, all!

    How to specify custom Deleter for QSharedPointer correctly?

    Qt Code:
    1. class A
    2. {
    3. public:
    4. void f() {...}
    5. };
    6.  
    7. ...
    8. QSharedPointer<A> pA1(new A, ???);
    To copy to clipboard, switch view to plain text mode 


    What shall I write instead of ??? in a code above to make QSharedPointer call method f() of object that is about to be deleted? Or it is not possible at all?
    I.e. i want pA1 to call method f() of controlled instance of class A when pA1's reference counter drops to 0. &A::f doesn't seem to work since A::f() is not static and object must be specified.

    But why does the code below always work when A is subclass of QObject?

    Qt Code:
    1. QSharedPointer<A> pA2(new A, &QObject::deleteLater);
    To copy to clipboard, switch view to plain text mode 

    Non-static deleteLater() method of newly created class A instance is called as ref. counter of pA2 drops to 0.

    Thank you in advance && Sorry for my English

    P.S. I use GCC version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5), Qt 4.8.4
    Last edited by hoRUS; 8th May 2013 at 18:08. Reason: updated contents

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Custom Deleter for QSharedPointer

    Qt Code:
    1. QSharedPointer<A> pA1(new A, &A::f);
    To copy to clipboard, switch view to plain text mode 

    This is a working example showing it calls the method with the correct this pointer:
    Qt Code:
    1. #include <QtCore>
    2.  
    3. class Test
    4. {
    5. public:
    6. Test() { qDebug() << "Constructed" << this; }
    7. ~Test() { qDebug() << "Destroyed" << this; }
    8. void deleteThis() {
    9. qDebug() << "Do this" << this;
    10. // perform elaborate deletion ritual
    11. delete this;
    12. }
    13. };
    14.  
    15. int main(int argc, char **argv)
    16. {
    17. QCoreApplication app(argc, argv);
    18. QSharedPointer<Test> test1(new Test, &Test::deleteThis);
    19. QSharedPointer<Test> test2(new Test, &Test::deleteThis);
    20. QSharedPointer<Test> test3(test1);
    21. test1.clear(); // no deletion, kept alive by test3
    22. test2.clear();
    23. return 0;
    24. }
    To copy to clipboard, switch view to plain text mode 


    Edit: Hmm, how did I miss the update?

  3. #3
    Join Date
    Aug 2015
    Posts
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Custom Deleter for QSharedPointer

    Have a question though. is it true that, custom deleter is copied in QSharedPoointer operator =?

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Custom Deleter for QSharedPointer

    Have a question though. is it true that, custom deleter is copied in QSharedPoointer operator =?
    You could easily modify ChrisW67's code to add a line that assigns test2 = test1 for example and verify that it does.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 11
    Last Post: 20th November 2012, 23:18
  2. QSharedPointer / QWeakPointer in QML?
    By centipede in forum Qt Quick
    Replies: 1
    Last Post: 23rd May 2012, 10:25
  3. inserting to Qlist of QSharedPointer
    By jajdoo in forum Newbie
    Replies: 1
    Last Post: 11th August 2011, 01:20
  4. emitting QSharedPointer
    By babu198649 in forum Newbie
    Replies: 11
    Last Post: 26th July 2010, 09:51
  5. Problem with QSharedPointer
    By weaver4 in forum Newbie
    Replies: 2
    Last Post: 19th April 2010, 15:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.