- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 QIcon(带有 MSVC 2008 的 Qt 4.8.1)时遇到了一些内存泄漏。
这种情况发生在简单的 QAction(如菜单条目,甚至是由 Qt Designer 自动生成的条目)或容器项(如 QTreeWidgetItem)中。
例如:
QTreeWidgetItem *newItem = new QTreeWidgetItem();
newItem->setText(0, "Item");
// This causes a memory leak!
newItem->setIcon(0, QIcon("D:\\Dnl\\QtSandBoxApp\\Resources\\dataset2.png"));
treeWidget->addTopLevelItem(newItem);
经过大量调试后,我发现 QIcon 内部使用 QImage,它似乎已正确销毁(引用计数器降至零)。
我可以生成的最简单的示例如下:
#include "stdafx.h"
#include <QtGui/QApplication>
#include <crtdbg.h>
int main(int argc, char *argv[])
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
QApplication a(argc, argv);
//QPixmapCache::setCacheLimit(0);
// MEMORY LEAK!
// Internally uses QImageReader
QImage image("D:\\Dnl\\QtSandBoxApp\\Resources\\dataset2.png");
// NO MEMORY LEAK!
//QImage image(QSize(16, 16), QImage::Format_ARGB32);
return 0;
}
我错过了什么吗?
这是一个非常烦人的问题,因为它阻止了正确的内存泄漏跟踪(即由应用程序引起,而不是 Qt 引起的)。
编辑:这是内存泄漏报告
Detected memory leaks!
Dumping objects ->
{5637} normal block at 0x00A46020, 76 bytes long.
Data: <d ,g ^ L Bg> 64 D2 2C 67 E8 5E A4 00 00 00 00 00 4C B9 42 67
{5636} normal block at 0x00A45EE8, 12 bytes long.
Data: < z ` z > 14 7A D3 00 20 60 A4 00 00 7A D3 00
{5634} normal block at 0x00A46148, 128 bytes long.
Data: < 6 6 Za > 01 00 00 00 36 00 00 00 36 00 00 00 5A 61 A4 00
{5614} normal block at 0x00A45DC8, 76 bytes long.
Data: <d ,g x L Bg> 64 D2 2C 67 E8 78 A4 00 00 00 00 00 4C B9 42 67
{5613} normal block at 0x00A478E8, 12 bytes long.
Data: < ] > A4 96 CA 00 C8 5D A4 00 90 96 CA 00
{5611} normal block at 0x00A45C48, 128 bytes long.
Data: < 6 5 Z\ > 01 00 00 00 36 00 00 00 35 00 00 00 5A 5C A4 00
{5591} normal block at 0x00A477C8, 76 bytes long.
Data: <d ,g u L Bg> 64 D2 2C 67 10 75 A4 00 00 00 00 00 4C B9 42 67
{5590} normal block at 0x00A47510, 12 bytes long.
Data: < w > B8 96 C6 00 C8 77 A4 00 A4 96 C6 00
{5588} normal block at 0x00A45B88, 128 bytes long.
Data: < 6 5 [ > 01 00 00 00 36 00 00 00 35 00 00 00 9A 5B A4 00
{5566} normal block at 0x00A473F0, 76 bytes long.
Data: <d ,g s L Bg> 64 D2 2C 67 A8 73 A4 00 00 00 00 00 4C B9 42 67
{5565} normal block at 0x00A473A8, 12 bytes long.
Data: < s > E0 D7 C3 00 F0 73 A4 00 CC D7 C3 00
{5563} normal block at 0x00A471E0, 128 bytes long.
Data: < 6 5 q > 01 00 00 00 36 00 00 00 35 00 00 00 F2 71 A4 00
{5543} normal block at 0x00A47008, 76 bytes long.
Data: <d ,g o L Bg> 64 D2 2C 67 C0 6F A4 00 00 00 00 00 4C B9 42 67
{5542} normal block at 0x00A46FC0, 12 bytes long.
Data: <La p 8a > 4C 61 BB 00 08 70 A4 00 38 61 BB 00
{5540} normal block at 0x00A46DF8, 128 bytes long.
Data: < 6 6 n > 01 00 00 00 36 00 00 00 36 00 00 00 0A 6E A4 00
{5520} normal block at 0x003ED9A8, 76 bytes long.
Data: <d ,g` > L Bg> 64 D2 2C 67 60 D9 3E 00 00 00 00 00 4C B9 42 67
{5519} normal block at 0x003ED960, 12 bytes long.
Data: < > > A4 C6 B4 00 A8 D9 3E 00 90 C6 B4 00
{5517} normal block at 0x00A46D38, 128 bytes long.
Data: < 6 5 Jm > 01 00 00 00 36 00 00 00 35 00 00 00 4A 6D A4 00
{5496} normal block at 0x003ED8D8, 76 bytes long.
Data: <d ,g > L Bg> 64 D2 2C 67 80 D7 3E 00 00 00 00 00 4C B9 42 67
{5495} normal block at 0x003ED780, 12 bytes long.
Data: << > ( > 3C CA 00 10 D8 D8 3E 00 28 CA 00 10
{5491} normal block at 0x00A412A0, 128 bytes long.
Data: < 6 5 > 01 00 00 00 36 00 00 00 35 00 00 00 B2 12 A4 00
{5260} normal block at 0x00A48448, 64 bytes long.
Data: < Z > 01 00 00 00 16 00 00 00 13 00 00 00 5A 84 A4 00
{5233} normal block at 0x00A48380, 56 bytes long.
Data: < Ha Bg> 00 00 CD 00 C0 82 A4 00 48 61 A4 00 04 BA 42 67
{5231} normal block at 0x00A482C0, 128 bytes long.
Data: < 6 6 > 01 00 00 00 36 00 00 00 36 00 00 00 D2 82 A4 00
{4681} normal block at 0x00A48150, 64 bytes long.
Data: < b > 01 00 00 00 16 00 00 00 13 00 00 00 62 81 A4 00
{4655} normal block at 0x00A47FB8, 56 bytes long.
Data: < ~ H\ Bg> 00 00 CA 00 F8 7E A4 00 48 5C A4 00 04 BA 42 67
{4653} normal block at 0x00A47EF8, 126 bytes long.
Data: < 5 5 > 01 00 00 00 35 00 00 00 35 00 00 00 0A 7F A4 00
{4102} normal block at 0x00A47DB8, 64 bytes long.
Data: < } > 01 00 00 00 16 00 00 00 13 00 00 00 CA 7D A4 00
{4075} normal block at 0x00A47D40, 56 bytes long.
Data: < { [ Bg> 00 00 C6 00 D8 7B A4 00 88 5B A4 00 04 BA 42 67
{4073} normal block at 0x00A47BD8, 126 bytes long.
Data: < 5 5 { > 01 00 00 00 35 00 00 00 35 00 00 00 EA 7B A4 00
{3522} normal block at 0x00A479F8, 64 bytes long.
Data: < z > 01 00 00 00 16 00 00 00 13 00 00 00 0A 7A A4 00
{3496} normal block at 0x00A47AA0, 56 bytes long.
Data: < 8y q Bg> 00 00 BD 00 38 79 A4 00 E0 71 A4 00 04 BA 42 67
{3494} normal block at 0x00A47938, 126 bytes long.
Data: < 5 5 Jy > 01 00 00 00 35 00 00 00 35 00 00 00 4A 79 A4 00
{2943} normal block at 0x00A41220, 64 bytes long.
Data: < 2 > 01 00 00 00 16 00 00 00 13 00 00 00 32 12 A4 00
{2917} normal block at 0x00A40418, 56 bytes long.
Data: < X m Bg> 00 00 B6 00 58 03 A4 00 F8 6D A4 00 04 BA 42 67
{2915} normal block at 0x00A40358, 128 bytes long.
Data: < 6 6 j > 01 00 00 00 36 00 00 00 36 00 00 00 6A 03 A4 00
{2367} normal block at 0x00A40760, 64 bytes long.
Data: < r > 01 00 00 00 16 00 00 00 13 00 00 00 72 07 A4 00
{2341} normal block at 0x00A401D0, 56 bytes long.
Data: < h 8m Bg> 00 00 B4 00 68 00 A4 00 38 6D A4 00 04 BA 42 67
{2339} normal block at 0x00A40068, 126 bytes long.
Data: < 5 5 z > 01 00 00 00 35 00 00 00 35 00 00 00 7A 00 A4 00
{1788} normal block at 0x00A40820, 64 bytes long.
Data: < 2 > 01 00 00 00 16 00 00 00 13 00 00 00 32 08 A4 00
{1762} normal block at 0x00A417B0, 56 bytes long.
Data: < Bg> 00 00 00 10 B0 18 A4 00 A0 12 A4 00 04 BA 42 67
{1760} normal block at 0x00A418B0, 126 bytes long.
Data: < 5 5 > 01 00 00 00 35 00 00 00 35 00 00 00 C2 18 A4 00
{252} normal block at 0x003E8A08, 40 bytes long.
Data: < @B > 00 00 00 00 CC CD CD CD 40 42 0F 00 00 00 00 00
{251} normal block at 0x003E8950, 120 bytes long.
Data: <Lc-g > L Bg> 4C 63 2D 67 08 89 3E 00 00 00 00 00 4C B9 42 67
{250} normal block at 0x003E8908, 8 bytes long.
Data: < c-gP > > 0C 63 2D 67 50 89 3E 00
{249} normal block at 0x003E88A0, 40 bytes long.
Data: < @B > 00 00 00 00 CC CD CD CD 40 42 0F 00 00 00 00 00
{248} normal block at 0x003E8828, 60 bytes long.
Data: < > > 07 00 00 00 08 89 3E 00 80 06 00 00 00 CD CD CD
Object dump complete.
The program '[2808] QtSandBoxApp.exe: Native' has exited with code 0 (0x0).
最佳答案
我解决了这个问题。
对于 Qt 来说,使用 CrtDbg 库 API 似乎不是一个好主意。
Qt allocates memory in it’s DLLs and this memory is released when the DLLs are unloaded. Unfortunately this is after Visual Studio reports the supposed leaks.
情况确实如此,事实上,当我从磁盘请求 QImage 时,会在运行时加载以下模块:
'QtSandBoxApp.exe': Loaded 'C:\Libraries\Qt\4.8.1\plugins\imageformats\qgifd4.dll'
'QtSandBoxApp.exe': Loaded 'C:\Libraries\Qt\4.8.1\plugins\imageformats\qicod4.dll'
'QtSandBoxApp.exe': Loaded 'C:\Libraries\Qt\4.8.1\plugins\imageformats\qjpegd4.dll'
'QtSandBoxApp.exe': Loaded 'C:\Libraries\Qt\4.8.1\plugins\imageformats\qmngd4.dll'
'QtSandBoxApp.exe': Loaded 'C:\Libraries\Qt\4.8.1\plugins\imageformats\qsvgd4.dll'
'QtSandBoxApp.exe': Loaded 'C:\Libraries\Qt\4.8.1\bin\QtSvgd4.dll', Symbols loaded.
'QtSandBoxApp.exe': Loaded 'C:\Libraries\Qt\4.8.1\bin\QtXmld4.dll', Symbols loaded.
'QtSandBoxApp.exe': Loaded 'C:\Libraries\Qt\4.8.1\plugins\imageformats\qtgad4.dll'
'QtSandBoxApp.exe': Loaded 'C:\Libraries\Qt\4.8.1\plugins\imageformats\qtiffd4.dll'
Visual Leak Detector正确报告没有内存泄漏。
附:This QtCentre 的 wiki 条目具有误导性:P
关于qt - QIcon/QImage 内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10970316/
我有这个代码; void FileBrowser::setItemIcon(QStandardItem *item) { // check whether its a file or not
我想在我的 QComboBox 中的条目上包含一个“删除”图标,但我无法捕获鼠标按下事件。我试图在组合框中捕获它,并且尝试重新实现 QIcon 类以在那里捕获鼠标按下。没有骰子。有人知道怎么做吗? -
我有一个自定义文件格式,用于存储一些图像。我在程序运行期间将这些图像加载到内存中。图像以二进制格式加载(即它们在磁盘上的显示方式相同)。我知道图像的格式(几乎都是 jpeg)。问题是我不知道宽度/高度
我尝试了多种方法在 QTableWidget 单元格中显示 QIcon,但我不确定为什么它不起作用。我有一个按钮,按下时会在表格中添加一行。这是代码... void MainWindow::press
我在使用 QIcon(带有 MSVC 2008 的 Qt 4.8.1)时遇到了一些内存泄漏。 这种情况发生在简单的 QAction(如菜单条目,甚至是由 Qt Designer 自动生成的条目)或容器
我有一个带有自定义主题的应用程序,并且禁用的图标呈亮灰色。我想更改禁用的图标色调颜色。 现在我知道有这样的可能性: QTableWidgetItem *name = new QTableWidgetI
我正在尝试扩大 QIcon,但它不起作用。 class Example(QMainWindow): def __init__(self): super().__init__()
目标 我希望用户能够单击 QIcon 来执行某些操作。也就是说,我想将 QIcon 内的鼠标单击事件连接到插槽。 我尝试过的 在我的示例(如下)中,我使用 TreeView ,当用户单击其中一个图标时
我正在开发一个需要 的应用程序显示与不同文件类型关联的图标 . 例如对于 .doc 扩展,我需要它才能显示 Microsoft Word 图标 . 问题: 如何使用 QT sdk 从系统中获取 QIc
openFile = QtGui.QAction(QtGui.QIcon('Black.png'), 'Open', self) 添加 Action 后运行模块时,单击文件时,打开的 Action 没
正如 docs 中所写我想从当前代码编辑 setThemeSearchPaths() : if __name__ == "__main__": app = QtGui.QApplication
我尝试使用派生自 QSettings 的类来存储 QIcon,如下所示: setValue("fancy_icon", QIcon::fromTheme("document-new")); 但是我有一
我有一个额外的窗口,里面有一个文本编辑 + qicon(我称之为 float 编辑)。 我有 2 个 qicons - 用于通常的 DPI(16 x 16 图标),它是 2x 版本(32 x 32)。
我有一个 play button 的 png 格式图像我在其中存储为 embedded resource在我的申请中。 play.png 我已经为 Normal 和 Activ
我正在学习如何使用 Qt 进行 C++ 编程。当我单击一个按钮并且与该按钮相关的矩阵上的位置等于-1时,我想显示这张图像,我还想清除同一个按钮上的文本,我的代码到目前为止部分是: if(Tabulei
我已经为 GUI 菜单创建 QT 自定义设计器小部件工作了一段时间。这个想法是您只需将它拖到设计器中,选择您想要的帧数、每帧按钮的数量等,它就会为您生成一切并调整其大小。 小部件的结构方式有一些属性可
我正在使用 QTableView 和 QSqlTableModel 来显示来自 sqlite DB 的数据。在数据库(和模型)中,有一列包含包含路径的字段,即:“/home/foo/bar.txt”或
我想要一个文本左对齐、图标右对齐的表格单元格。 但是现在,我让图标和文本都左对齐,这是我尝试过的 QtTableWidgetItem * item = new QtTableWidgetItem("p
假设我做了这样的事情: QIcon myIcon(":/resources/icon.ico"); 我以后如何确定该图标的路径,例如 QString path = myIcon.getPath();
我想设计一个新的 QIcon 并希望它看起来像一个固定的文本,周围有一个圆角矩形 .-----. | Phy | `-----´ 当在最终应用了缩放变换的 QPainter 上绘制时,图标应该在没有“
我是一名优秀的程序员,十分优秀!