Questions tagged [qsharedpointer]

QSharedPointer is a Qt class which holds a strong reference to a shared pointer.

Introduced in Qt 4.5 QSharedPointer class represents a strong reference to a shared pointer. It is similar to boost::shared_ptr or std::shared_ptr.

The QSharedPointer is an automatic, shared pointer in C++. It is thread-safe and operates atomically on the pointer value.

QSharedPointer holds a shared pointer by means of an external reference count.

Official Qt documentation can be found here for Qt 4.8 and here for Qt 5.

74 questions
38
votes
2 answers

What is the difference between QPointer, QSharedPointer and QWeakPointer classes in Qt?

I have read from the Qt documentations about QPointer, QSharedPointer and QWeakPointer classes. It says: QPointer is a template class that provides guarded pointers to Qt objects and behaves like a normal C++ pointer except that it is…
Nejat
  • 28,909
  • 10
  • 91
  • 119
14
votes
1 answer

Difference between QSharedPointer and QSharedDataPointer?

What is the difference between these two types of pointers? As far as I can read, QSharedPointer can handle situation well, so what is the need for QSharedDataPointer?
paul simmons
  • 5,258
  • 11
  • 49
  • 77
10
votes
2 answers

Attaching signals and slots to an object within a QSharedPointer

My application contained several functions like this: void SomeClass::set_data_provider(DataProvider *data_provider) { connect(data_provider, SIGNAL(data_available(int)), this, SLOT(data_available(int))); } To avoid passing raw pointers…
bdesham
  • 13,980
  • 10
  • 70
  • 116
9
votes
2 answers

QSharedPointer and QObject::deleteLater

I have a situation where a QSharedPointer managed object signalizes that it has finished it's purpose and is ready for deletion soon (after execution left the function emitting my readyForDeletion signal). When working with normal pointers, I'd just…
athre0z
  • 422
  • 4
  • 11
8
votes
2 answers

Why does QSharedPointer::create call destructor of incomplete object?

I have following code example: #include #include #include #include class A { public: A() { throw 1; } ~A() { qDebug() << "A destr"; } }; int main(int argc, char*…
Yrchgrchh
  • 169
  • 6
8
votes
1 answer

Where is Qt’s PointerToMemberFunction defined?

In this question I was able to adapt the QObject method QMetaObject::Connection QObject::connect(const QObject * sender, const char * signal, const QObject * receiver, const char * method, Qt::ConnectionType type =…
bdesham
  • 13,980
  • 10
  • 70
  • 116
8
votes
2 answers

Pimpl + QSharedPointer - Destructor = Disaster

Yesterday I ran into misery which took me 24 hours of frustration. The problem boiled down to unexpected crashes occurring on random basis. To complicate things, debugging reports had absolutely random pattern as well. To complicate it even more,…
Alexander Shukaev
  • 15,556
  • 8
  • 64
  • 81
6
votes
2 answers

Does Qt applications have automatic garbage collection?

I am researching this but I don't see a conclusive answer. Does a Qt widget application clean up the memory when it exits? Does it make any difference deriving from QObject? If there is garbage collection than why is there QSharedPointer class? I am…
zar
  • 9,241
  • 10
  • 74
  • 145
6
votes
2 answers

Qt equivalent to boost::ptr_vector?

I need a pointer container that takes ownership of the pointers - i.e. when an element is removed, or the container goes out of scope, it frees all its pointers, like in boost::ptr_vector. QList > doesn't work (compile…
sashoalm
  • 63,456
  • 96
  • 348
  • 677
6
votes
1 answer

Making a QSharedPointer

For historical reasons, I use QSharedPointer in my software. At some points, we want to store boost::shared_ptr that point to the same data, and which should keep alive the instances of the QSharedPointer. The common way to do this is to…
Johannes Schaub - litb
  • 466,055
  • 116
  • 851
  • 1,175
5
votes
1 answer

Good practice for QSharedPointer as method parameter or return value of a method?

Is there any good practice or regulation on how to use a QSharedPointer object as method parameter or return value of a method ? By value: LMNode::setParent(QSharedPointer parent) { this->parent = parent; } QSharedPointer
4
votes
1 answer

QSharedPointer, how to pass them around, and do I need them?

Been trying to understand shared pointer for a few days now and it feels like I cant seem to get it. Not sure if it's just to obvious or if it's too complicated. First of all, could anyone please give me an example where you would ACTUALLY use…
chikuba
  • 3,988
  • 6
  • 39
  • 74
4
votes
1 answer

Using QSharedPointer with new[] yields "Mismatched free() / delete / delete[]" in valgrind

I have the following code: QPair, int> someclass::somefunction() { int siz = data_size(); QSharedPointer buffer(new unsigned int[siz]); // Fill the buffer... return qMakePair(buffer,…
arne
  • 3,975
  • 24
  • 42
4
votes
3 answers

Qt smart pointer equivalent to Boost::shared_ptr ?

I have used Boost::shared_ptr in my previous projects and now I want to find a smart-pointer in Qt which does the same/similar thing. Since there are many smart-pointer classes in Qt I was wondering which one to use. Is it QSharedPointer?
Ashika Umanga Umagiliya
  • 7,998
  • 19
  • 86
  • 167
4
votes
1 answer

How to properly use destructors with QSharedPointer AFTER the exec loop is stopped?

Good Morning everyone, I am using QSharedPointer with my classes derived from QObject. Since they use the signal/slot mechanism, I must use the QObject::deleteLater() in order to properly destroy them, see for example: ~QObject() : "Deleting a…
n3mo
  • 551
  • 3
  • 18
1
2 3 4 5