SlideServe
Menu
Préférences
© SlideServe · v2026

Effective Memory Management in Qt Mobile Programming

marlo avatar
12 diapositives · 216 Views · Jul 13, 2014

Learn about memory management, object ownership, and construction principles in Qt with clear examples and best practices. Training kit by Agus Kurniawan.

Effective Memory Management in Qt Mobile Programming - slide 1
01 / 12Effective Memory Management in Qt Mobile Programming
1 / 17

Télécharger la présentation

Effective Memory Management in Qt Mobile Programming
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Qt Mobile ProgrammingTraining Kit Agus Kurniawan agusk@cs.ui.ac.id http://blog.aguskurniawan.net

  2. Memory Management Agus Kurniawan agusk@cs.ui.ac.id http://blog.aguskurniawan.net

  3. Memory Management parent deletes child1 and child2 child1 deletes child1_1 and child1_2 • QObject can have parent and children • When a parent object is deleted, it deletes its children QObject *parent = new QObject(); QObject *child1 = new QObject(parent); QObject *child2 = new QObject(parent); QObject *child1_1 = new QObject(child1); QObject *child1_2 = new QObject(child1); delete parent; parent child1 child2 child1_1 child1_2

  4. Memory Management parent deletes box and button box deletes option1 and option2 • This is used when implementing visual hierarchies. QDialog *parent = new QDialog(); QGroupBox *box = new QGroupBox(parent); QPushButton *button = new QPushButton(parent); QRadioButton *option1 = new QRadioButton(box); QRadioButton *option2 = new QRadioButton(box); delete parent;

  5. Usage Patterns dialog is deleted when the scope ends • Use the this-pointer as top level parent • Allocate parent on the stack Dialog::Dialog(QWidget *parent) : QDialog(parent) { QGroupBox *box = QGroupBox(this); QPushButton *button = QPushButton(this); QRadioButton *option1 = QRadioButton(box); QRadioButton *option2 = QRadioButton(box); ... void Widget::showDialog() { Dialog dialog; if (dialog.exec() == QDialog::Accepted) { ... } }

  6. Heap • When using new and delete, memory is allocated on the heap. • Heap memory must be explicitly freed using delete to avoid memory leaks. • Objects allocated on the heap can live for as long as they are needed. new Construction Destruction delete

  7. Stack • Local variables are allocated on the stack. • Stack variables are automatically destructed when they go out of scope. • Objects allocated on the stack are always destructed when they go out of scope. int a Construction Destruction }

  8. Stack and Heap • To get automatic memory management, only the parent needs to be allocated on the stack. MyMainWindow QApplication MyMainWindow::MyMainWindow(... { new QLabel(this); new ... } int main(int argc, char **argv) { QApplication a(argc, argv); MyMainWindow w; w.show(); return a.exec(); }

  9. Changing Ownership List items are not children per se, but owned. The example demonstrates the nomenclature. • QObjects can be moved between parents • The parents know when children are deleted • Methods that return pointers and “take” releases data from its owner and leaves it in the takers care obj->setParent(newParent); delete listWidget->item(0); // Removes the first item (unsafe) QLayoutItem *QLayout::takeAt(int); QListWidgetItem *QListWidget::takeItem(int); // Safe alternative QListWidgetItem *item = listWidget->takeItem(0); if (item) { delete item; }

  10. Constructor Etiquette • Almost all QObjects take a parent object with a default value of 0 (null) • The parent of QWidgets are other QWidgets • Classes have a tendency to provide many constructors for convenience (including one taking only parent) • The parent is usually the first argument with a default value QObject(QObject *parent=0); QPushButton(QWidget *parent=0); QPushButton(const QString &text, QWidget *parent=0); QPushButton(const QIcon &icon, const QString &text, QWidget *parent=0); QLabel(const QString &text, QWidget *parent=0, Qt::WindowFlags f=0);

  11. Constructor Etiquette • When creating your own QObjects, consider • Always allowing parent be 0 (null) • Having one constructor only accepting parent • parent is the first argument with a default value • Provide several constructors to avoid having to pass 0 (null) and invalid (e.g. QString()) values as arguments

  12. Thank you Agus Kurniawan agusk@cs.ui.ac.id http://blog.aguskurniawan.net