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. QSharedPointer and Temporary Copies
Forum Updated to NodeBB v4.3 + New Features

QSharedPointer and Temporary Copies

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.2k 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.
  • A Offline
    A Offline
    ajaynatu
    wrote on last edited by
    #1

    Hello,
    I have something like below in my code @void Fun()
    {
    ClassA var = queue->dequeue();
    }@

    Now var is a qsharedpointer. From, what i could guess looking into the dequeue function of the QQueue, is that it would return a temporary copy of the var. So, it would increment by 1 the reference count for var and then decrement when it goes out of scope. Can this cause a problem ? Actually, it crashed while doing so. So, i fear, while returning temporary object, the reference counting is going wrong, the actual pointer is being deleted. It is not 100% reproducable
    The architecture is ARM XScale.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tucnak
      wrote on last edited by
      #2

      It's bad practice. I will do it in this way:
      @
      void Foo::bar() {
      MyClass * var = queque->dequeue():
      var->somefunction();
      }
      @

      It's recommended to contains pointers (instead of objects) in containers.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        ajaynatu
        wrote on last edited by
        #3

        You mean to say QSharedPointers cannot be used in containers?

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

          can you give a bit more code?
          How is the queue defined? Which types are stored? How are they added?
          This has a big influence on the error...

          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