Results 1 to 3 of 3

Thread: object allocation question

  1. #1
    Join Date
    Apr 2014
    Posts
    53
    Thanks
    9

    Default object allocation question

    Hi,
    in many QT methods, for example the data() method of QAbstractTableModel (and many others) I am creating objects on the stack and returning them, like this:

    Qt Code:
    1. QVariant ActionListModel::data(const QModelIndex &index, int role) const {
    2. Action *a;
    3. int row_num,col_num;
    4. col_num=index.column();
    5. row_num=index.row();
    6. if (row_num>=actions->count()) return (QVariant());
    7. a=actions->value(row_num);
    8. if (a==0) {
    9. return(QVariant());
    10. }
    11. if (role==Qt::DecorationRole) {
    12. if (col_num==0) {
    13. cat_action_id_t act_code;
    14. act_code=a->get_action_code();
    15. switch(act_code) {
    16. case CAT_ACTION_UNDEFINED: {
    17. break;
    18. }
    19. case CAT_ACTION_COMMENT : {
    20. QPixmap icon("/home/action_type_icons/comment.png");
    21. return(icon);
    22. break;
    23. }
    24. case CAT_ACTION_NEW_DEPENDENCY: {
    25. QPixmap icon("/home/action_type_icons/new_dependency.png");
    26. return(icon);
    27. break;
    28. }
    To copy to clipboard, switch view to plain text mode 

    The "icon" variable is created on the stack and is destroyed after the method execution ends, but I am not getting segmentation faults in my app. The question is, how does it work ? Is QT copying all objects I am returning in the QT methods that I am overriding?

    TIA.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: object allocation question

    The method is returning an instance of QVariant.
    The type QPixmap, is registered with the Qt variant type system.
    For that it needs to be copyable.
    So the QVariant instance contains a copy of the pixmap.

    Since QPixmap is an implicitly shared class, copying it into the variant increments the pixmap's reference counter.
    Destroying the local object decreased the reference counter.
    The pixmap in the variant is then the only reference of the pixmap data.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    nuliknol (8th November 2015)

  4. #3
    Join Date
    Apr 2014
    Posts
    53
    Thanks
    9

    Default Re: object allocation question

    wow! amazing. thanks a lot for the clarification.

Similar Threads

  1. Question about read-only object.
    By robgeek in forum Qt Programming
    Replies: 3
    Last Post: 8th April 2015, 14:23
  2. Object Member Question
    By Atomic_Sheep in forum General Programming
    Replies: 1
    Last Post: 15th September 2013, 10:54
  3. Question about initializing an object without assigning...
    By Zingam in forum General Programming
    Replies: 4
    Last Post: 21st August 2013, 21:14
  4. Basic question about allocation
    By feraudyh in forum Newbie
    Replies: 5
    Last Post: 24th June 2010, 12:42
  5. Amateur Qt question about movement of an object
    By gtbgmaniak in forum Newbie
    Replies: 1
    Last Post: 19th April 2009, 10:12

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.