Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Creating a pointer and using a reference, what about deletion?
Forum Updated to NodeBB v4.3 + New Features

Creating a pointer and using a reference, what about deletion?

Scheduled Pinned Locked Moved C++ Gurus
6 Posts 3 Posters 3.7k Views 1 Watching
  • 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.
  • F Offline
    F Offline
    fluca1978
    wrote on last edited by
    #1

    Hi,
    I've got still a bit of confusion between the usage of pointers and references. In a widget I've got I'd like to use a list as a reference:

    @class MyWidget : public QWidget{
    ...
    QList<TableViewHandler*> handlers;
    };@

    The widget above is constructed in a method connected as a slot, so I allocate the list on the heap to avoid it being destructed due to the scope of the method call:

    @
    QList<TableViewHandler*>* handlers = new QLits<TableViewHandler*>();
    // initialize the list
    ...
    QList<TableViewHandler>& ref = handlers;
    MyWidget
    w = new MyWidget( ref );
    @

    Now the doubt I've got is about the deletion: since the list has been constructed on the heap, and I've using a reference, is it sure to delete in the constructor using &handlers as address?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      Hi,

      you misunderstood refernces. in your example, in löine 4 you create a reference of the heap object from line 1, but the call on line 5 will copy it, as the member of the widget is a value and not a reference.

      If you use a reference inside the widget, it would be:

      @
      class MyWidget : public QWidget{
      MyWidget(QList<TableViewHandler*>& _handlers, QWidget* parent);
      ...
      QList<TableViewHandler*>& handlers;
      };

      MyWidget::MyWidget(QList<TableViewHandler*>& _handlers, QWidget* parent):
      QWidget(parent),
      handlers(_handlers) // here, a reference is given to the object, as the member is a reference
      {
      }

      //+++++++++++++++++++++++++++++++++++++++++++++++++++

      foo()
      {
      QList<TableViewHandler*>* handlers = new QLits<TableViewHandler*>();
      // initialize the list
      ...
      // QList<TableViewHandler>& ref = handlers; this is not needed
      MyWidget
      w = new MyWidget( *handlers ); // here, a reference is given to the constructor, as the parameter is a reference
      ...
      }
      @

      If you go this way, you MUST ensure that the heap object will not be deleted, before the widget!

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fluca1978
        wrote on last edited by
        #3

        But in this case the pointer to heap data will be lost when the method goes out of scope, so the only way to get the address of the data to be deleted is thru the &reference, right?

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          You mix two things.

          In calling list to methods you may use references (&) and pointers (*). They are basically similar from the overhead point of view. You do not copy the data.
          Within the method the references are typically easier to use. You do not need the *. So far nothing with memory allocation.

          If you use in a method a pointer and you allocate memory, you have to make sure that the memory is deleted prior to exit of the method. Otherwise you get a memory leak.

          You may hand allocated memory to a method through a pointer. You may delete/release this memory in the method, that is possible. However, you have to make sure that you are not using the pointer again somewhere else.

          Edit: If you are not sure about the reference and pointer concept, it is better to keep it simple. Release the meory where you have allocated it.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fluca1978
            wrote on last edited by
            #5

            Thanks, it is clearer now.
            My problem is that I need to initialize a widget with some custom (i.e., user defined) data, so I build the data, pass it to the widget and then deallocate it when the widget is destructed. This does not follow your principle of deallocating where the allocation occurs, but I don't know how to do otherwise. A solution could be to copy all the built data within the widget, not storing so the pointers.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              This can be done in an easy way:

              @
              class MyWidget : public QWidget{
              MyWidget(QList<TableViewHandler*>& _handlers, QWidget* parent);
              ...
              QList<TableViewHandler*> handlers;
              };

              MyWidget::MyWidget(QList<TableViewHandler*>& _handlers, QWidget* parent):
              QWidget(parent),
              handlers(_handlers) // <-- here, the data is copied
              {
              }

              //+++++++++++++++++++++++++++++++++++++++++++++++++++

              foo()
              {
              QList<TableViewHandler*> handlers;
              // initialize the list
              ...
              MyWidget* w = new MyWidget( handlers ); // here, a reference is given to the constructor, as the parameter
              // is a reference, but it will be copied behind the scenes, so the
              // automatic deletion on method end is ok.
              ...
              }
              @

              As QList is an implicitly shared class, the copy will be cheap as it only copies a simple pointer to the contained data and the deletion of the stack object will not really delete the data, only decrease the ref count and everything is safe. Thanks to the implicitly shared classes, it's cheap and easy...

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0

              • Login

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