Using QSharedPointer with the new [] gives "Inconsistency free () / delete / delete []" in valgrind

I have the following code:

QPair<QSharedPointer<unsigned int>, int> someclass::somefunction() { int siz = data_size(); QSharedPointer<unsigned int> buffer(new unsigned int[siz]); // Fill the buffer... return qMakePair(buffer, siz); } 

At some point, the QSharedPointer returned by this function will go beyond the scope and the pointer set in the constructor will be free. Using valgrind 3.6.1, I get the message "Mismatch error free () / delete / delete []". Is there something wrong with my use of QSharedPointer or do I just need to live with this valgrind warning?

+4
source share
1 answer

One way to fix this is to write a custom deaerator and pass it to the QSharedPointer constructor as follows:

 template <typename T_> void do_delete(T_ buf[]) { delete[] buf; } 

And then

 QSharedPointer<unsigned int> buffer(new unsigned int[siz], do_delete<unsigned int>); 

I'm not sure if there is a more elegant solution (which would be nice)

+11
source

All Articles