在做项目的时候,用QImage来将原始rgb值(uchar*buf)保存为图片,显示到widget上,然后delete buf,程序发生内存泄露。经过排查发现产生上述原因为:
qimage构造函数只是创建一个指向buf的指针,因此在使用的时候必须保持buf为有效值。qt官方给出的解释为:
uses an existing memory buffer.
the buffer must remain valid throughout the life of the qimage and all copies that have not been modifies or otherwise detached from the original buffer.
QImg 导致内存异常
最新推荐文章于 2024-09-09 11:19:17 发布