Nyaruko
Nyaruko

Reputation: 4479

qt, is this the safe way to use deleteLater?

Qt said the deleteLater is called after the event loop returns. So, if I have the following code:

 Waypoint* wp = new Waypoint();
 WaypointWidget* wp_widget = new WaypointWidget(wp);
 ...
 delete wp;
 wp_widget->deleteLater();
 ...
 Waypoint* wp2 = new Waypoint();
 WaypointWidget* wp_widget2 = new WaypointWidget(wp2);

the constructor of WaypointWidget is:

WaypointWidget(Waypoint* wp){
  _wp = wp;//_wp is declared as private variable inside WaypointWidget.h
}

My concern is that, for some situation, the wp2 will take the same address as the wp, and when the deleteLater() cause the wp_widget is finally deleted.The corresponding _wp will be deleted. And because it has the same address as wp2. The wp2 will be deleted/affected.

Upvotes: 0

Views: 165

Answers (1)

Dmitry Sazonov
Dmitry Sazonov

Reputation: 8994

You call deleteLater for wp_widget, not for wp, so same address will not be set. Possible error will be if you will try to access _wp in WaypointWidget destructor.

I recommend you to read about RAII.

Upvotes: 2

Related Questions