Important: Please read the Qt Code of Conduct -

Doubt related to Implicit Sharing

  • Greetings.

    I'm working on a project using Qt and OpenCV.
    I'm using QStack in one of my classes and I read in the documentation about 'Implicit Sharing'.

    Well, although I think I understand almost everything, however there is an issue that I'm not entirely sure, and since I have been having some problems with the amount of memory available to an application or process, thought I'd clarify my doubt.

    Suppose you have a stack QStack<T> where 'T' is a type not belonging to Qt (specifically it is an image cv::Mat... although I think it is irrelevant to my problem).

    Assume also that the stack several images have been added, what happens if you run a statement like this:

    imgsStack = QStack cv::Mat ();

    What happens with the memory space occupied by the old stack of images? ... Is it free?

    I understand by 'Implicit Sharing', the space must be released, although I'm not entirely sure.

    Is the space occupied by each image cv::Mat is also released automatically or, on the contrary, I release myself manually?

    Thanks in advance for any responses and/or comments.

  • If the memory of imgsStack is not referenced by ("shared with") another QStackcv::Mat variable, then yes, the cv::Mat is freed, assuming cv::Mat frees its memory properly in the destructor. And of course assuming, you're not using a QStackcv::Mat*. In that case, the memory of the pointer addresses (n*4 bytes) is freed, but not the memory the pointers point to (the actual cv::Mat data).

    With implicit sharing you don't have to take special care not to use more memory than necessary. It actually helps you to use less memory.

Log in to reply