Results 1 to 7 of 7

Thread: Using implicit sharing to return an image as a method parameter

  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 Using implicit sharing to return an image as a method parameter

    Hi! I've been stuck with this for hours, I don't know why I'm doing this wrong, can't understand.
    What I want to do is to create a method that returns a QImage, and pass this QImage not a the return type, but as a parameter of the method (I know I can do it other ways, but I really would like to learn why this is wrong and how it should be done).
    This is more or less what I did:

    Qt Code:
    1. QImage myImage;
    2. myMethod(myImage);
    To copy to clipboard, switch view to plain text mode 

    where myMethod is defined as:

    Qt Code:
    1. bool MyClass::myMethod(QImage& myImage)
    2. {
    3. ...
    4. myImage = QImage::fromData(...);
    5. ...
    6. return true;
    7. }
    To copy to clipboard, switch view to plain text mode 

    I thought that implicit sharing used this way should have assigned the new image to my myImage which, being passed by reference, is my original image. Unfortunately this is wrong as I can see. Why?

    Thanks!

  2. #2
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Using implicit sharing to return an image as a method parameter

    What results do you get with the above?

  3. #3
    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: Using implicit sharing to return an image as a method parameter

    That code should modify the original image the implicit sharing and copy on write are not used there (since you pass it by reference, not by value - so you don't have many objects that share data).

    You can read more about implicit sharing here and as DanH said, describe the problem you have with the code, or else we can't help you very much.

  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: Using implicit sharing to return an image as a method parameter

    I'm very sorry, it seems I was wrong. Maybe when I tried that way that I described, I also inadvertently introduced a second mistake. It seems that I get a segfault inside my function when I do this:

    Qt Code:
    1. const char* _cover = QString::fromWCharArray(cover.c_str()).toStdString().c_str();
    2. image = QImage::fromData(QByteArray::fromBase64(QByteArray(_cover)));
    To copy to clipboard, switch view to plain text mode 

    instead of:

    Qt Code:
    1. image = QImage::fromData(QByteArray::fromBase64(QByteArray(QString::fromWCharArray(cover.c_str()).toStdString().c_str())));
    To copy to clipboard, switch view to plain text mode 

    I excluded this as a possible mistake and confused it with a problem related to the way I was passing the parameter.
    Any idea why those are different?
    Thanks!

    EDIT: I'm maybe misunderstanding the exact meaning of implicit sharing, or misusing it. I was referring to the fact that I'm using the operator= to copy references to image data, without copying just pointers. But maybe I'm wrong in this case, and implicit sharing is not involved.
    Last edited by Luc4; 22nd May 2011 at 01:03.

  5. #5
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Using implicit sharing to return an image as a method parameter

    Implict sharing is where two distinct objects (with different addresses if you were to check) share the same internal buffer (until one of them is modified). This happens for strings, images, and many other Qt types.

    The two code sequences you quote are (or at least appear to be, on casual examination) equivalent. Not clear offhand why the first might fail, other than the possibility that it causes a leak and you run out of heap. But it is needlessly complex. You can go from the QString to QByteArray with toAscii().

  6. The following user says thank you to DanH for this useful post:

    Luc4 (22nd May 2011)

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Using implicit sharing to return an image as a method parameter

    Quote Originally Posted by Luc4 View Post
    I'm very sorry, it seems I was wrong. Maybe when I tried that way that I described, I also inadvertently introduced a second mistake. It seems that I get a segfault inside my function when I do this:

    Qt Code:
    1. const char* _cover = QString::fromWCharArray(cover.c_str()).toStdString().c_str();
    2. image = QImage::fromData(QByteArray::fromBase64(QByteArray(_cover)));
    To copy to clipboard, switch view to plain text mode 
    The c_str() method returns a pointer to the data buffer of the std::string. The std::string is a temporary object created in the execution of the first statement: its scope ends at the completion of the enclosing statement. After that time the temporary object is destroyed and the pointer you have stored is invalid. Often you will seem to get away with this because the memory, although deallocated/destructed, still contains something that looks OK. Eventually though, you will use the invalid pointer after something else has made use of the memory it points at and BOOM!

  8. The following user says thank you to ChrisW67 for this useful post:

    Luc4 (22nd May 2011)

  9. #7
    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: Using implicit sharing to return an image as a method parameter

    Oh, I see. This is what I came up with then, far simpler:

    Qt Code:
    1. QString _cover = QString::fromWCharArray(cover.c_str());
    2. image = QImage::fromData(QByteArray::fromBase64(_cover.toAscii()));
    To copy to clipboard, switch view to plain text mode 

    seems to work now.
    Thanks for your help!

Similar Threads

  1. Implicit sharing vs. c++ refereces
    By IrYoKu in forum Qt Programming
    Replies: 12
    Last Post: 9th November 2011, 23:20
  2. Classes with Implicit Sharing
    By weaver4 in forum Newbie
    Replies: 5
    Last Post: 15th January 2011, 18:57
  3. Threads and Implicit Sharing
    By stillwaiting in forum Newbie
    Replies: 5
    Last Post: 30th November 2010, 16:46
  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.