Results 1 to 2 of 2

Thread: Allocation of object within a function with pointer local on stack

  1. #1
    Join Date
    Oct 2015
    Posts
    1
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Allocation of object within a function with pointer local on stack

    I'm still an amateur at Qt and C++ so I'm sure I'm missing something obvious here.
    I am allocating an object, call it A, on the heap within a function (or method) using a locally-defined pointer. My function passes in A's parent, PA so that A can be deleted automatically when PA is deleted as shown in the example below:

    void f(PA,datatoplot)
    {
    AType *A = new AType(PA,datatoplot); // note that PA is the parent of A, passed to A's constructor and PA is a pointer
    .....
    ..... // more code, but A is not explicitly deleted or deallocated

    A->show() // say A is some kind of plot or other widget and we're displaying it here. Repeated calls to f show different plots
    }

    My question: Will this lead to memory leaks and/or dangling pointers? The pointer A gets deleted from the local stack of f() each time we exit f() but a copy of A is created in RAM every time f() is called. I'm presuming that it doesn't matter if the local pointer to A is lost due to the disposal of the stack of f() when exiting f() because PA retains a copy of each pointer of each copy of A created (one for each call to f()) and when PA is deleted, all the copies of A will also be deleted? If only PA needs to handle the memory management of all the copies of A, does the above lead to problems?
    Thanks so much,
    Phil

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    456
    Thanks
    10
    Thanked 61 Times in 59 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Allocation of object within a function with pointer local on stack

    Hi, the parent widget will delete its children, so PA will delete all the new A objects, but only when PA itself gets deleted. So while PA exists you will accumulate AType objects which you cannot access because you did not store a pointer (you could get a pointer by using methods like QObject::findChild, but I try to avoid that if I can).

    It would be nicer if you would clean unused AType objects, or (probably better because it avoids the new allocations) you can create it once on the first call, then re-use the AType object from the first call and only use show() and hide().

    Ginsengelf

  3. The following user says thank you to Ginsengelf for this useful post:

    montanaviking (3rd November 2020)

Similar Threads

  1. Correctly destruction of child objects inside stack object
    By papercut87 in forum Qt Programming
    Replies: 4
    Last Post: 12th August 2016, 18:36
  2. object allocation question
    By nuliknol in forum Qt Programming
    Replies: 2
    Last Post: 6th November 2015, 18:15
  3. How to call a function in the mother object from a child object?
    By Momergil in forum General Programming
    Replies: 4
    Last Post: 18th December 2011, 16:49
  4. Stack object on top of one other
    By Markus_AC in forum Qt Programming
    Replies: 3
    Last Post: 23rd September 2011, 12:05
  5. Replies: 4
    Last Post: 10th May 2006, 23:37

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.