Results 1 to 8 of 8

Thread: QSharedPointer - how to prevent delete?

  1. #1
    Join Date
    Sep 2009
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSharedPointer - how to prevent delete?

    Hi, I've one problem. I want to disallow to call destructor on object which I want always have in QSharedPointer. I was trying something as this:
    Qt Code:
    1. class X
    2. {
    3. protected:
    4. ~X() {};
    5.  
    6. class Deleter;
    7. friend class Deleter;
    8.  
    9. class Deleter
    10. {
    11. void operator()(X* p) { delete p; }
    12. };
    13.  
    14. public:
    15. QSharedPointer<X> create()
    16. {
    17. QSharedPointer<X> px = QSharedPointer<X>(new X(), X::Deleter());
    18. return px;
    19. }
    20. };
    To copy to clipboard, switch view to plain text mode 

    But this code is not compilable:
    Error 1 error C2248: 'X::~X' : cannot access protected member declared in class 'X' c:\qt\4.6.2\src\corelib\tools\qsharedpointer_impl. h 391

    It's probably because what QSharedPointer is implemented. But is there any way how to disallow to anybody to destrouy object using standard c++ pointer? I thing it will be much more secure and clearer.

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSharedPointer - how to prevent delete?

    Make the pointer const, then it can't be deleted because the "delete" operator is mutable. I mean the regular pointer, I have no idea if this would work for QSharedPointer (it should but that's just a guess) but then I don't know why you want to use QSharedPointer if you don't want the object to be deleted... That's exactly what QSharedPointer is for, isn't it?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2009
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSharedPointer - how to prevent delete?

    Ooh .. Maybe I have bad expressed. Of course, I want object to be deleted, But I want to assure, that only QShraredPointer can do it. I want to prevent something as this:
    Qt Code:
    1. MyObject *obj = sharedPointerToObject.data();
    2. delete obj;
    To copy to clipboard, switch view to plain text mode 
    My first example above is the way how boost shared pointers do what I want. If I provide my object to some module, I want to disallow module to delete object using raw pointer to provided interface. But againts boost shared pointers, this way is not compilable with Qt's shared pointer. So I'm finding another way how assure it. Unfortunatelly, protected destructor is not right way with QSharedPointer.

    Of course, it's not functional problem, I would like only to make my code safer. Tries to delete object with provided interface could be than detected in compile time.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSharedPointer - how to prevent delete?

    Quote Originally Posted by Piskvorkar View Post
    Ooh .. Maybe I have bad expressed. Of course, I want object to be deleted, But I want to assure, that only QShraredPointer can do it. I want to prevent something as this:
    Qt Code:
    1. MyObject *obj = sharedPointerToObject.data();
    2. delete obj;
    To copy to clipboard, switch view to plain text mode 
    You can't. You can try making the delete operator protected and make QSharedPointer a friend of your class but I doubt this will work. Oh.. I see you already tried something similar (not the same though)...

    My first example above is the way how boost shared pointers do what I want. If I provide my object to some module, I want to disallow module to delete object using raw pointer to provided interface. But againts boost shared pointers, this way is not compilable with Qt's shared pointer. So I'm finding another way how assure it. Unfortunatelly, protected destructor is not right way with QSharedPointer.
    You can always use the Boost pointer.

    Of course, it's not functional problem, I would like only to make my code safer. Tries to delete object with provided interface could be than detected in compile time.
    Honestly it won't make your code safer. You can very easily hack around such protection (resistance is futile). If you want to make sure nothing deletes the pointer then don't expose it at all.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2009
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSharedPointer - how to prevent delete?

    Ok, it's nothing important. Thanks for reply.

    Anyway, I didn't want to protect every way how somebody could delete object using raw pointer. I only want to protect delting by mistake since I provide some objects through interface. I realy have seen such cases in real, that somebody deleted raw pointer because of innocence and than protected constructor is competent. I've even tried make QSharedPointer friend, but code still wasn't compilable whith same message. Maybe I did it wrong and maybe it doesn't work because of something else (QSharedPointer inherits from some other classes in QtSharedPointer namespace).

  6. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSharedPointer - how to prevent delete?

    Quote Originally Posted by Piskvorkar View Post
    Ok, it's nothing important. Thanks for reply.

    Anyway, I didn't want to protect every way how somebody could delete object using raw pointer. I only want to protect delting by mistake since I provide some objects through interface. I realy have seen such cases in real, that somebody deleted raw pointer because of innocence and than protected constructor is competent. I've even tried make QSharedPointer friend, but code still wasn't compilable whith same message. Maybe I did it wrong and maybe it doesn't work because of something else (QSharedPointer inherits from some other classes in QtSharedPointer namespace).
    So maybe consider making your class explicitly shared and expose it as and object or reference instead of pointer. Of course you can still do:
    Qt Code:
    1. MyClass myObj;
    2. delete &myObj;
    To copy to clipboard, switch view to plain text mode 
    but it is rather less possible that some would like to delete an object or reference then a pointer (and by the way this code will crash on delete because of invalid pointer).
    Last edited by faldzip; 31st March 2010 at 11:44.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  7. #7
    Join Date
    Sep 2009
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSharedPointer - how to prevent delete?

    Yes, it should be possible, thansk for idea, but in my case it's not usable. It would be needlessly complicated to remake it now.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSharedPointer - how to prevent delete?

    QSharedPointer is like explicit sharing. The only difference is that with QSharedPointer you can access the raw pointer which you can't do for an explicitly shared class. One should be able to subclass QSharedPointer and hide appropriate methods if needed but of course there are still ways to break it
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QSharedPointer vs Boost::tr1::shared_ptr()
    By photo_tom in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2010, 16:48
  2. Replies: 3
    Last Post: 7th August 2009, 13:05
  3. QSpitter - prevent resizing
    By jonks in forum Newbie
    Replies: 3
    Last Post: 28th July 2009, 19:36
  4. Replies: 1
    Last Post: 5th March 2009, 05:52
  5. Replies: 4
    Last Post: 19th February 2009, 11:10

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.