Results 1 to 11 of 11

Thread: QHash::unite() question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default QHash::unite() question

    Hi,
    I have the following code
    Qt Code:
    1. QHash<Blub, Blab>* firstHash = new QHash..;
    2. QHash<Blub, Blab>* secondHash = new QHash..;
    3. ..
    4. firstHash->unite(*secondHash);
    5. // delete secondHash;
    To copy to clipboard, switch view to plain text mode 

    When I uncomment the delete, than my application crashes. I assume it's because all elements in the secondHash are deleted and not more available in the firstHash.

    Is it save to leave the second Hash undeletet? Wouldn't that be a memory leak?

  2. #2
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    76
    Thanked 37 Times in 32 Posts

    Default Re: QHash::unite() question

    I don't get any error in the following Code
    Qt Code:
    1. p, li { white-space: pre-wrap; } #include <iostream>
    2. #include <QHash>
    3.  
    4.  
    5.  
    6. int main(){
    7.  
    8. QHash<int, int > *firstHash = new QHash<int, int >;
    9. (*firstHash)[0] =0;
    10. (*firstHash)[1] =1;
    11. (*firstHash)[2] =1;
    12. (*firstHash)[3] =2;
    13. (*firstHash)[4] =3;
    14.  
    15.  
    16.  
    17. QHash<int, int > *secondHash = new QHash<int, int>;
    18. (*secondHash)[0] =0;
    19. (*secondHash)[2] =1;
    20. (*secondHash)[4] =2;
    21. (*secondHash)[6] =3;
    22. (*secondHash)[8] =4;
    23.  
    24. firstHash->unite( *secondHash);
    25.  
    26.  
    27. delete secondHash;
    28. std::cout << " Second Deleted " << std::endl;
    29. std::cout << " First :" << (*firstHash)[2] << std::endl;
    30. std::cout << " First :" << (*firstHash)[6] << std::endl;
    31. delete firstHash;
    32. std::cout << " First Deleted " << std::endl;
    33. return 0;
    34. }
    To copy to clipboard, switch view to plain text mode 

    Now is your Blah a Blah or a Blah*
    Or is the Blah class having a dataMember as pointer. if so have you made the copy construction and assisgnment operator.
    We can't solve problems by using the same kind of thinking we used when we created them

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: QHash::unite() question

    What kind of type is "Blub"? Qt's containers contain copies of values. So both containers have their own instances of Blubs. So destroying one hash does basically not affect another hash anyhow. Normally you don't need to allocate containers on the heap so they get automatically destroyed after going out of the scope..
    J-P Nurmi

  4. #4
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default Re: QHash::unite() question

    Thanks for the help but I think the problem is a bit more complex.

    I use the QHash for a TableModel. The key is a struct which describes the position of the item in the table and the value is a QVariant. I then search the QHash in the TableModel::data() method for the value and return it.
    So far, so good.
    When I want to insert some columns or rows, I have to iterate over the Hash and add the count of the new columns or rows to the position. I'll attach the code for the insert method. Hopefully it's not to much..
    Qt Code:
    1. struct ItemPosition {
    2. ItemPosition(int p_column, int p_row) : column(p_column), row(p_row) {}
    3. int column;
    4. int row;
    5. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. m_tableItems = new QHash<TabM::ItemPosition, QVariant>();
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. bool TableModel::insertColumns(int p_column, int p_count, const QModelIndex & p_parent) {
    2. if (p_column > columnCount())
    3. p_column = columnCount();
    4. beginInsertColumns(p_parent, p_column, p_column+p_count-1);
    5.  
    6. // moving elements
    7. QHash<TabM::ItemPosition, QVariant>* l_tempTableItems = new QHash<TabM::ItemPosition, QVariant>();
    8. QMutableHashIterator<TabM::ItemPosition, QVariant> l_iterator(*m_tableItems);
    9. while(l_iterator.hasNext()) {
    10. l_iterator.next();
    11. QVariant l_tmpVal = l_iterator.value();
    12. TabM::ItemPosition l_tmpPos= l_iterator.key();
    13. if(l_tmpPos.column >= p_column) {
    14. l_tempTableItems->insert(TabM::ItemPosition(l_tmpPos.column+p_count, l_tmpPos.row), l_tmpVal);
    15. l_iterator.remove();
    16. }
    17. }
    18.  
    19. // joining hashes
    20. if (l_tempTableItems->size() < m_tableItems->size()) {
    21. // works..
    22. m_tableItems->unite(*l_tempTableItems);
    23. delete l_tempTableItems;
    24. }
    25. else {
    26. // causes a crash if the delete is uncommented
    27. l_tempTableItems->unite(*m_tableItems);
    28. // delete m_tableItems;
    29. m_tableItems = l_tempTableItems;
    30. }
    31.  
    32. m_columnCount+=p_count;
    33. endInsertColumns();
    34.  
    35. //debug output
    36. cout << "i3" << endl;
    37. QHashIterator<TabM::ItemPosition, QVariant> l_iterator2(*m_tableItems);
    38. while(l_iterator2.hasNext()) {
    39. l_iterator2.next();
    40. QVariant l_tmpVal = l_iterator2.value();
    41. TabM::ItemPosition l_tmpPos= l_iterator2.key();
    42. cout << "position, row: " << l_tmpPos.row << ", column: " << l_tmpPos.column << " value: ";
    43. if(l_tmpVal.isValid()) cout << l_tmpVal.toString().toStdString() << endl;
    44. else cout << "no val" << endl;
    45. }
    46. cout << "i4" << endl;
    47. return true;
    48. }
    To copy to clipboard, switch view to plain text mode 

    The program is crashing after the insert function, the "i4" is emited..

    @jpn
    The hash is on the heap, because of the joining and because i don't want to have the includes in the header files (faster compiling).
    Last edited by aMan; 2nd November 2006 at 18:14.

  5. #5
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default Re: QHash::unite() question

    Does really nobody can help me?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QHash::unite() question

    Can we see the backtrace?

  7. #7
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default Re: QHash::unite() question

    I have put the code into a function insertFields(..) that is called from insertRows(..) and insertColumns(). Here is the Backtrace..

    Qt Code:
    1. #0 0x0804f0bb in My::TableModel::insertFields ()
    2. #1 0x0804f4fe in My::TableModel::insertColumns ()
    3. #2 0x0804bc57 in T2A::MainWindow::addColumn ()
    4. #3 0x0804bf4e in T2A::MainWindow::MainWindow ()
    5. #4 0x0804bb8d in main ()
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QHash::unite() question

    Quote Originally Posted by aMan View Post
    #0 0x0804f0bb in My::TableModel::insertFields ()
    #1 0x0804f4fe in My::TableModel::insertColumns ()
    #2 0x0804bc57 in T2A::MainWindow::addColumn ()
    #3 0x0804bf4e in T2A::MainWindow::MainWindow ()
    #4 0x0804bb8d in main ()
    1. compile your application in debug mode, so that we can see parameter values,
    2. make it crash.

  9. #9
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default Re: QHash::unite() question

    It had crashed, because it crashes right after the start and there was a seg fault (I hadn't copied the whole text, maybe a mistake..).

    Here is the full (well, i cut the gdb infos) gdb output.
    (gdb) run
    Starting program: /home/damdam/Documents/projekte/cpp/qt/Table2ASCII/build/table2ascii
    [Thread debugging using libthread_db enabled]
    [New Thread -1221245264 (LWP 13707)]
    Qt: gdb: -nograb added to command-line options.
    Use the -dograb option to enforce grabbing.
    i3
    i4

    Program received signal SIGSEGV, Segmentation fault.
    [Switching to Thread -1221245264 (LWP 13707)]
    0x0804fbbb in QHash<My::TabM::ItemPosition, QVariant>::setSharable (this=0x80a9aa8, sharable=true) at /usr/include/QtCore/qhash.h:229
    229 inline void setSharable(bool sharable) { if (!sharable) detach(); d->sharable = sharable; }
    (gdb) bt
    #0 0x0804fbbb in QHash<My::TabM::ItemPosition, QVariant>::setSharable (this=0x80a9aa8, sharable=true) at /usr/include/QtCore/qhash.h:229
    #1 0x0804fbe7 in ~QMutableHashIterator (this=0xbfd3609c) at /usr/include/QtCore/qhash.h:802
    #2 0x0804ee0f in My::TableModel::insertFields (this=0x809ae08, p_field=0, p_count=1, p_orientation=Qt::Horizontal, p_parent=@0xbfd36154) at my_tablemodel.cpp:137
    #3 0x0804eeb6 in My::TableModel::insertColumns (this=0x809ae08, p_column=0, p_count=1, p_parent=@0xbfd36154) at my_tablemodel.cpp:63
    #4 0x0804bd61 in T2A::MainWindow::addColumn (this=0xbfd361e8) at MainWindow.cpp:47
    #5 0x0804c822 in MainWindow (this=0xbfd361e8) at MainWindow.cpp:27
    #6 0x0804bc87 in main (argc=Cannot access memory at address 0x0) at main.cpp:9
    This is the first time i use gdb from the command line, so please apologise if I've forgotten something..

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: QHash::unite() question

    Quote Originally Posted by aMan View Post
    #0 0x0804fbbb in QHash<My::TabM::ItemPosition, QVariant>::setSharable (this=0x80a9aa8, sharable=true) at /usr/include/QtCore/qhash.h:229
    #1 0x0804fbe7 in ~QMutableHashIterator (this=0xbfd3609c) at /usr/include/QtCore/qhash.h:802
    It looks like you delete m_tableItems while QMutableHashIterator still uses it. Add curly braces around lines 8--17 to force the destruction of l_iterator.

  11. The following user says thank you to jacek for this useful post:

    aMan (5th November 2006)

  12. #11
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default Re: QHash::unite() question

    Cool, thanks..
    It works now..

Similar Threads

  1. QTextEdit simple question
    By Marcopolo in forum Qt Tools
    Replies: 4
    Last Post: 11th October 2007, 00:01
  2. QThread exit()/quit() question
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2006, 14:38
  3. simple thread layout question
    By mhoover in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2006, 11:02
  4. simple question on Class-Members
    By mickey in forum General Programming
    Replies: 7
    Last Post: 4th February 2006, 22:37
  5. xml with binary question
    By TheKedge in forum Qt Programming
    Replies: 7
    Last Post: 12th January 2006, 23:21

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
  •  
Qt is a trademark of The Qt Company.