将 QSharedPointer 与 new[] 一起使用会产生“不匹配的 free()/delete/delete[]”在瓦尔格林德
我有以下代码:
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);
}
在某些时候,此函数返回的 QSharedPointer 将超出范围,并且构造函数中设置的指针将被释放。使用 valgrind 3.6.1,我收到“不匹配的 free()/delete/delete[]”错误。我使用 QSharedPointer 是否有什么问题,还是我必须忍受这个 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 out of scope and the pointer set in the constructor will be free'd. Using valgrind 3.6.1, I get a "Mismatched free() / delete / delete[]" error. Is there anything wrong with my use of QSharedPointer
or do I just have to live with this valgrind warning?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决这个问题的一种方法是编写一个自定义删除器并将其传递给 QSharedPointer 的构造函数,如下所示:
然后
我不确定是否有更优雅的解决方案(这会很好)
One way to fix this is to write a custom deleter and pass that to the constructor of
QSharedPointer
like so:And then
I am not sure whether there is a more elegant solution (which would be nice)