QList 清除函数调用是否清除存储在 QList 中的动态分配对象的内存?
Does QList clear function call clears the memories of dynamically allocated objects that are stored in the QList?
所以我有 class 称为 AppointmentSchedule
,它属于以下类型:
namespace Ui {
class AppointmentSchedule;
}
class AppointmentSchedule : public QWidget
{
Q_OBJECT
public:
explicit AppointmentSchedule(QWidget *parent = 0);
~AppointmentSchedule();
Ui::AppointmentSchedule *ui;
};
这个class的ui
由两个QLabel
类型的对象和两个QDateTimeEdit
类型的对象组成。在外部 class 中,我需要 QList
类型 AppointmentSchedule
的动态分配对象。我将在 QList
中附加动态分配的对象,类似于以下方式。
QList<AppointmentSchedule*> scheduleList;
foreach (QDate date, dates)
{
AppointmentSchedule * newSchedule = new AppointmentSchedule(this);//Allocation of memory
QDateTime sDateTime(date, QTime(8, 0, 0));
newSchedule->ui->appointmentStartDateTimeEdit->setDateTime(sDateTime);
QDateTime eDateTime(date, QTime(8, 15, 0));
newSchedule->ui->appointmentEndDateTimeEdit->setDateTime(eDateTime);
scheduleList.append(newSchedule);
}
如果我这样调用 QList
的 clear()
函数:
scheduleList.clear();
它会释放我分配的所有内存吗?
谢谢。
Will it deallocate all the memory I have allocated?
没有。您声明了 指针 到 AppointmentSchedule
对象的列表。当你调用 clear
时,这些指针将被删除,但它们指向的内存将保持不变,因此你会发生内存泄漏。
要自行释放对象,请使用智能指针(如 std::unique_ptr
和 std::shared_ptr
)或在调用 clear()
之前在任何类型的 foreach
循环中手动删除它们.
编辑:
在这种情况下,不会有内存泄漏,因为 Qt 对象层次结构会负责适当的对象销毁。请参阅@Kirill 的回答。
Will it deallocate all the memory I have allocated?
否,clear()
仅从列表中删除所有项目。在您的情况下,这些项目是 AppointmentSchedule
对象上的指针。
当您为 AppointmentSchedule
个对象设置父对象时,该对象将在其父对象被销毁时被删除。这就是为什么你的情况没有内存泄漏。
因为你使用指针:
为避免内存泄漏,您需要删除每个指针,例如:
foreach(auto element, scheduleList)
{
delete element;
}
此外,如果您在 AppointmentSchedule 元素中使用父级 属性,它们会在父级删除时自动删除。
关于列表本身:
如果您想在列表中存储大对象(不是指针),调用 .clear()
只会删除可访问的项目,但不一定会调整容器的大小。如果你每次调用它都这样做,那将是低效的。
但是您可以调用 .squeeze()
来释放未使用的内存。
它仍然只删除为指针分配的 space,而不是您使用 new
运算符分配的堆上的对象。
所以我有 class 称为 AppointmentSchedule
,它属于以下类型:
namespace Ui {
class AppointmentSchedule;
}
class AppointmentSchedule : public QWidget
{
Q_OBJECT
public:
explicit AppointmentSchedule(QWidget *parent = 0);
~AppointmentSchedule();
Ui::AppointmentSchedule *ui;
};
这个class的ui
由两个QLabel
类型的对象和两个QDateTimeEdit
类型的对象组成。在外部 class 中,我需要 QList
类型 AppointmentSchedule
的动态分配对象。我将在 QList
中附加动态分配的对象,类似于以下方式。
QList<AppointmentSchedule*> scheduleList;
foreach (QDate date, dates)
{
AppointmentSchedule * newSchedule = new AppointmentSchedule(this);//Allocation of memory
QDateTime sDateTime(date, QTime(8, 0, 0));
newSchedule->ui->appointmentStartDateTimeEdit->setDateTime(sDateTime);
QDateTime eDateTime(date, QTime(8, 15, 0));
newSchedule->ui->appointmentEndDateTimeEdit->setDateTime(eDateTime);
scheduleList.append(newSchedule);
}
如果我这样调用 QList
的 clear()
函数:
scheduleList.clear();
它会释放我分配的所有内存吗?
谢谢。
Will it deallocate all the memory I have allocated?
没有。您声明了 指针 到 AppointmentSchedule
对象的列表。当你调用 clear
时,这些指针将被删除,但它们指向的内存将保持不变,因此你会发生内存泄漏。
要自行释放对象,请使用智能指针(如 std::unique_ptr
和 std::shared_ptr
)或在调用 clear()
之前在任何类型的 foreach
循环中手动删除它们.
编辑:
在这种情况下,不会有内存泄漏,因为 Qt 对象层次结构会负责适当的对象销毁。请参阅@Kirill 的回答。
Will it deallocate all the memory I have allocated?
否,clear()
仅从列表中删除所有项目。在您的情况下,这些项目是 AppointmentSchedule
对象上的指针。
当您为 AppointmentSchedule
个对象设置父对象时,该对象将在其父对象被销毁时被删除。这就是为什么你的情况没有内存泄漏。
因为你使用指针: 为避免内存泄漏,您需要删除每个指针,例如:
foreach(auto element, scheduleList)
{
delete element;
}
此外,如果您在 AppointmentSchedule 元素中使用父级 属性,它们会在父级删除时自动删除。
关于列表本身:
如果您想在列表中存储大对象(不是指针),调用 .clear()
只会删除可访问的项目,但不一定会调整容器的大小。如果你每次调用它都这样做,那将是低效的。
但是您可以调用 .squeeze()
来释放未使用的内存。
它仍然只删除为指针分配的 space,而不是您使用 new
运算符分配的堆上的对象。