gpt4 book ai didi

c++ - 我是否负责在 QImage::bits() 函数提供的指针上调用 delete?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:37:40 25 4
gpt4 key购买 nike

Qt 类 QImage 有两个版本的 bits() 函数,返回指向底层图像数据的指针。一个是 const,另一个不是。这是 the documentation对于非常量版本:

Returns a pointer to the first pixel data. This is equivalent to scanLine(0).

Note that QImage uses implicit data sharing. This function performs a deep copy of the shared pixel data, thus ensuring that this QImage is the only one using the current return value.

返回类型是uchar*

这是否意味着我有责任在完成此指针后调用 delete 以避免内存泄漏?

最佳答案

不,这只是意味着非常量版本导致 QImage 与共享相同数据的任何其他实例分离,因为您可能要修改它。它仍然保持所有权。可以肯定的是,实现(来自 Qt 4.7.2):

uchar *QImage::bits()
{
if (!d)
return 0;
detach();
// In case detach ran out of memory...
if (!d)
return 0;
return d->data;
}

关于c++ - 我是否负责在 QImage::bits() 函数提供的指针上调用 delete?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14878817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com