PDA

View Full Version : QSharedPointer - how to prevent delete?



Piskvorkar
30th March 2010, 21:02
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:


class X
{
protected:
~X() {};

class Deleter;
friend class Deleter;

class Deleter
{
void operator()(X* p) { delete p; }
};

public:
QSharedPointer<X> create()
{
QSharedPointer<X> px = QSharedPointer<X>(new X(), X::Deleter());
return px;
}
};


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.

wysota
30th March 2010, 22:01
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?

Piskvorkar
30th March 2010, 22:19
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:


MyObject *obj = sharedPointerToObject.data();
delete obj;

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.

wysota
30th March 2010, 23:58
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:


MyObject *obj = sharedPointerToObject.data();
delete obj;

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.

Piskvorkar
31st March 2010, 09:33
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).

faldzip
31st March 2010, 11:31
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:


MyClass myObj;
delete &myObj;

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).

Piskvorkar
31st March 2010, 11:44
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.

wysota
31st March 2010, 15:46
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 :)