2

Say for instance I have the prototype:

QList<Foo *> *methodBar(int someParam);

This method returns a QList pointer to the client code - how does the client code know if it needs to destroy the returned pointer?

Is there a convention that says if you are given a pointer it's your business to control it's memory? Or vice versa?

My thoughts to solve this are:

Option 1: Document it in the doc block that the client code has to get rid of the QList once it is done with it.

Option 2 Change the signature to something like:

void methodBar(int someParam, QList<Foo *> &listForOutput);

So that the client code creates the list and definitely knows that it should destroy it when it is finished.

Option 3 Use some kind of smart pointer, I'm not sure if this works but if I wrapped a QList* inside a QPointer and returned a copy of the QPointer I assume it would shallow copy the internal QList* and then when the QPointer went out of scope in the client code it would be destroyed along with the QList*.

So which of these options (or perhaps something else?) is the most common in the c++ world. If there is no standard way of doing this I'll accept an answer that it is up to personal preference.

7
  • Usually this is handled by a naming convention. For example cocoa (objective c) uses newXYZ, allocXYZ, copyXYZ and mutableCopyXYZ to mark it as the caller's responsibility.
    – Dave
    Jun 9, 2013 at 18:48
  • also anything which is returned as const is guaranteed to be the object's responsibility, since the caller isn't allowed to free it (without dangerous casting)
    – Dave
    Jun 9, 2013 at 18:50
  • To be honest this has little to do with Qt, I would consider removing the Qt part and making it a more general C++11 question (make the QList a std::vector or something like that). For more references about smart pointers usage patterns, cf. herbsutter.com/2013/05/29/gotw-89-solution-smart-pointers and herbsutter.com/2013/06/05/… .
    – peppe
    Jun 9, 2013 at 19:13
  • @peppe: Thank you for those links Peppe. I agree that this is a general c++ question and perhaps the Qt elements and tags will create noise. I'm not sure I want to edit it though because the accepted answer has Qt elements also which may disjoint things somewhat. If a moderator thinks it best to change both then I have no problem with that.
    – Kasheen
    Jun 9, 2013 at 19:31
  • This is exactly the reason to avoid the use of raw pointers and use smart_pointers instead. Poorly defined interfaces.
    – Manu343726
    Jun 9, 2013 at 22:25

1 Answer 1

4

If you want the caller to know they take responsibility for the pointer, then use this:

std::unique_ptr<QList<Foo *>> methodBar(int someParam);

Another advantage is that the caller would have to go out of their way to make the memory not be automatically freed.

A std::unique_ptr cannot be copied, only moved, so passing a std::unique_ptr fairly explicitly causes the responsibility of freeing the memory to be moved from the callee to to the caller.

Note that this would also imply that the objects that are pointed to inside the list are not the responsibility of the caller. If you wanted those objects also to be the responsibility of the caller, you would use:

std::unique_ptr<QList<std::unique_ptr<Foo>>> methodBar(int someParam);
2
  • Great, so this is kind of like Option 3 but using the std smart pointers instead of the Qt ones. I can now see how the semantics of "unique_ptr" makes it clear the client code is being given the only copy so they should take care of it. Your bit about the Foo objects in the collection was also useful, thanks.
    – Kasheen
    Jun 9, 2013 at 19:09
  • Just to note for storing unique_ptr inside a QList I think this isn't actually possible since QList doesn't support std::move semantics. It operates as expected with std collections such as std::deque though: std::deque<std::unique_ptr<Foo>>
    – Kasheen
    Jun 11, 2013 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.