Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Dynamic mem allocation in local scope okay?
Forum Updated to NodeBB v4.3 + New Features

Dynamic mem allocation in local scope okay?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 198 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • LoquatL Offline
    LoquatL Offline
    Loquat
    wrote on last edited by
    #1

    In the snippet below.. how come the dynamic allocation QState *state = new QState(parent); is okay inside the "for" loop? I would've thought that doing that was forbidden as the scope of that dynamically allocated variable ceases to exist after the "for" loop. This is taken from animation/appchooser/main.cpp example I found in QtCreator but I noticed that is the general practice of doing things in Qt apps. Please guide me as I've been experiencing some memory leakages and I wonder if this one could be the cause.

    void createStates(const QObjectList &objects,
    const QRect &selectedRect, QState *parent)
    {
    for (int i = 0; i < objects.size(); ++i) {
    QState *state = new QState(parent);
    state->assignProperty(objects.at(i), "geometry", selectedRect);
    parent->addTransition(objects.at(i), SIGNAL(clicked()), state);
    }
    }

    JonBJ 1 Reply Last reply
    0
    • LoquatL Loquat

      In the snippet below.. how come the dynamic allocation QState *state = new QState(parent); is okay inside the "for" loop? I would've thought that doing that was forbidden as the scope of that dynamically allocated variable ceases to exist after the "for" loop. This is taken from animation/appchooser/main.cpp example I found in QtCreator but I noticed that is the general practice of doing things in Qt apps. Please guide me as I've been experiencing some memory leakages and I wonder if this one could be the cause.

      void createStates(const QObjectList &objects,
      const QRect &selectedRect, QState *parent)
      {
      for (int i = 0; i < objects.size(); ++i) {
      QState *state = new QState(parent);
      state->assignProperty(objects.at(i), "geometry", selectedRect);
      parent->addTransition(objects.at(i), SIGNAL(clicked()), state);
      }
      }

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Loquat said in Dynamic mem allocation in local scope okay?:

      QState *state = new QState(parent);

      • The local variable state goes out of scope and is destroyed.
      • The new QState created object persists.
      • The "magic" of any class derived from QObject which is created via new QState(parent) is that it gets attached to its parent QObject and when that is destroyed its code destroys its children too. So you don't leak. Read Object Trees & Ownership.
      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved