Results 1 to 5 of 5

Thread: Why does this crash (compilable code included)?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2009
    Posts
    26
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows

    Default Why does this crash (compilable code included)?

    Hello All,

    probably this is a simple question, however (maybe due to being tired) I cannot see any solution. The code below is extracted and simplified part of my application (these are some factory methods, basing on xml input). However now it magically(?) crashes on something else, than my original application. But still, maybe the reason is the same.

    I put both parts in main.cpp file. Hovewer originally have separated declarations and definitions.

    Two classes used are as follows:
    Qt Code:
    1. #include <QBitArray>
    2. #include <QDebug>
    3.  
    4. //! Base class, with one pure virtual method doActionThatCrashes()
    5. class PropertyConverter {
    6. public:
    7. virtual ~PropertyConverter(){}
    8. virtual void doActionThatCrashes() = 0;
    9. };
    10.  
    11. //! Inherits PropertyConverter and implements funciton that really crashes.
    12. class PropertyBitmaskConverter:
    13. public PropertyConverter
    14. {
    15. public:
    16. PropertyBitmaskConverter(QBitArray &mask):
    17. _bitmaskFromInternal(mask) {}
    18.  
    19. const QBitArray &mask(){return _bitmaskFromInternal;}
    20.  
    21. virtual void doActionThatCrashes();
    22.  
    23. private:
    24. const QBitArray &_bitmaskFromInternal;//!\warning Edit, thanks to d_stranz: That was an error in this code, since reference to stack variable was being copied. Remvoe ampresand.
    25. };
    26.  
    27. void PropertyBitmaskConverter::doActionThatCrashes() {
    28. QBitArray array = this->mask();
    29. int size = array.size();
    30. qDebug()<<"The number of bits is"<<size;
    31. }
    To copy to clipboard, switch view to plain text mode 
    And the code itself is:
    Qt Code:
    1. PropertyConverter *createPropertyConverter();
    2. QBitArray bitArrayFromString(QString &array, bool *ok);
    3. QBitArray bitArrayFromChar(const char *data, int size, bool *ok);
    4.  
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. PropertyConverter *bitmaskConverter = createPropertyConverter();
    9. if (0 != bitmaskConverter) {
    10. //! \note Here it DOES crash
    11. bitmaskConverter->doActionThatCrashes();
    12. }
    13.  
    14. return 0;
    15. }
    16.  
    17. PropertyConverter *createPropertyConverter()
    18. {
    19. bool ok;
    20. QString mask = "b'11101'";
    21. QBitArray bitMask;
    22. if (!mask.isEmpty())
    23. bitMask = bitArrayFromString(mask, &ok);
    24. if (!ok || bitMask.isNull())
    25. return 0;
    26.  
    27. PropertyConverter *bitmaskConverter = new PropertyBitmaskConverter(bitMask);
    28.  
    29. //! \note Here this doesn't crash
    30. bitmaskConverter->doActionThatCrashes();
    31.  
    32. return bitmaskConverter;
    33. }
    34.  
    35. QDebug operator<<(QDebug dbg, const QBitArray& z)
    36. {
    37. QString text;
    38. for (int i = 0; i < z.size(); ++i)
    39. text.prepend(z.testBit(i) ? "1": "0");
    40. dbg << text;
    41. return dbg;
    42. }
    43.  
    44. QBitArray bitArrayFromString(QString &array, bool *ok)
    45. {
    46. return bitArrayFromChar(array.toLatin1().data(), array.size(), ok);
    47. }
    48.  
    49. QBitArray bitArrayFromChar(const char *data, int size, bool *ok)
    50. {
    51. if (0 != ok)
    52. *ok = true;
    53.  
    54. if ( ((strncmp(data, "B'", 2) == 0) || (strncmp(data, "b'", 2) == 0)) &&
    55. data[size - 1] == '\'') {
    56. const int significantDataSize = size - 3;
    57. QBitArray result(significantDataSize, false);
    58. const char *bitPtr = &data[2];
    59. for (int i = significantDataSize - 1; i >= 0; --i) {
    60. if (*bitPtr == '1')
    61. result.setBit(i, true);
    62. ++bitPtr;
    63. }
    64. return result;
    65. } else
    66. if (0 != ok) *ok = false;
    67.  
    68. return QBitArray();
    69. }
    To copy to clipboard, switch view to plain text mode 

    In the code attached the crash occurs on the streaming by QDebug, in the doActionThatCrashes(). When doActionThatCrashes() is called inside createPropertyConverter it works fine. When it's called one stack call higher (in the main() function), on the same object (same virtual call) it does nasty things.

    In original application, the crash occurs on the array.size() call in PropertyConverter::doActionThatCrashes(). Please, if anyone could help me with this problem, I would be very glad. It stops me from further development and my fine grade

    Best regards,
    Krzysztof

    Edit: Oh, I almost forgot. This could be important. I am using Qt 4.7.4 (64bit) on Linux (Ubuntu).
    Last edited by Krzysztow; 6th December 2011 at 20:37.

Similar Threads

  1. Replies: 0
    Last Post: 2nd February 2010, 04:38
  2. Release ok, debug crashes [compilable code]
    By Raccoon29 in forum Qt Programming
    Replies: 8
    Last Post: 16th December 2009, 15:48
  3. QTest dir not included by qmake
    By allstar in forum Qt Programming
    Replies: 1
    Last Post: 20th June 2007, 11:10
  4. header files not getting included
    By nimmyj in forum Installation and Deployment
    Replies: 1
    Last Post: 19th December 2006, 06:18
  5. Displaying picture included with html code
    By Djony in forum Qt Tools
    Replies: 1
    Last Post: 27th November 2006, 16:15

Tags for this Thread

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.