Results 1 to 5 of 5

Thread: Implicit sharing and pointers

  1. #1
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Implicit sharing and pointers

    Hi! I have a doubt on implicit sharing. If I have a pointer to a QImage named pImage, and I create a QImage by creating a shallow copy this way:

    Qt Code:
    1. QImage image(*pImage);
    To copy to clipboard, switch view to plain text mode 

    when image will be destroyed, because the ref counter has reached zero, will the space allocated for pImage be deallocated? Or will I have that data pImage refers to (so an entire QImage) will remain? Sorry for the stupid question but I really can't sort myself out.
    Thanks!

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Implicit sharing and pointers

    The first "clue" is that QImage doesn't have a constructor that takes a QImage* as a parameter.

    And second thing that a want to add is: don't do that, if you need to copy, than copy the object not just the pointer.

    The other way might look "ok" if you perform just some "small tests", because if you delete the first pointer and access the memory from the second (copy) pointer, the program might work "ok" a couple of times, because the memory isn't zero-ed out, it's just marked as un-allocated and the object remains until some other parts of your program write that memory and this is the "easy" way for you to get some hard to debug "Segmentation Fault" errors at run-time.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Implicit sharing and pointers

    Quote Originally Posted by Luc4 View Post
    will the space allocated for pImage be deallocated?
    One is contradictory with the other. Refcount will not go down to zero until you explicitely delete pImage because pImage will be referencing the data.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Implicit sharing and pointers

    And second thing that a want to add is: don't do that, if you need to copy, than copy the object not just the pointer.
    Copy? I suppose this is not a good solution both in terms of memory and of computational performance (I'm working on a slow device).

    One is contradictory with the other. Refcount will not go down to zero until you explicitely delete pImage because pImage will be referencing the data.
    mmh... and what about I create the new QImage and then I immediately delete pImage? The memory should not be deallocated as creating the new QImage has incremented the ref counter. Is this correct?

    Thanks!

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Implicit sharing and pointers

    Quote Originally Posted by Luc4 View Post
    mmh... and what about I create the new QImage and then I immediately delete pImage? The memory should not be deallocated as creating the new QImage has incremented the ref counter. Is this correct?
    Deleting pImage will decrease the reference count. Look, it doesn't matter whereas you create the object on heap (using "new") or on the stack. The object looks exactly the same in both cases and when you copy it, delete it or do all other possible operations with it, it still behaves the same.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Implicit sharing vs. c++ refereces
    By IrYoKu in forum Qt Programming
    Replies: 12
    Last Post: 9th November 2011, 23:20
  2. Replies: 4
    Last Post: 17th October 2009, 22:31
  3. QVector::data() and implicit sharing
    By abernat in forum Qt Programming
    Replies: 2
    Last Post: 7th July 2009, 18:34
  4. Question about implicit sharing
    By Cruz in forum Qt Programming
    Replies: 4
    Last Post: 17th February 2009, 17:03
  5. QSharedData - implicit sharing
    By gyre in forum Newbie
    Replies: 4
    Last Post: 28th October 2007, 19:09

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.