Results 1 to 2 of 2

Thread: Problem with std::string's

  1. #1
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Problem with std::string's

    I am writing some data loader libraries and I am sticking to standard libraries to make them as portable as possible, just in-case we want to utilize the loaders outside of a Qt environment. I am building a std::string with the file header information in a function called string DataType::getHeaderString( ).

    Qt Code:
    1. string DataType::getHeaderString( )
    2. {
    3. ostringstream collection,
    4. segment,
    5. altitude,
    6. longitude,
    7. latitude;
    8.  
    9. string hdr;
    10.  
    11. hdr += string("Timestamp: ") + mTimestamp + string("\n");
    12.  
    13. hdr += string("Description: ") + mDesc + string("\n");
    14.  
    15. collection << mCollectionNumber;
    16. hdr += string("Collection Number: ") + collection.str( ) + string("\n");
    17.  
    18. segment << mSegmentNumber;
    19. hdr += string("Segment Number: ") + segment.str( ) + string("\n");
    20.  
    21. altitude << mLocation.altitude;
    22. hdr += string("Location:\n Altitude: ") + altitude.str( ) + string("\n");
    23.  
    24. latitude << mLocation.latitude;
    25. hdr += string(" Latitude: ") + latitude.str( ) + string("\n");
    26.  
    27. longitude << mLocation.longitude;
    28. hdr += string(" Longitude: ") + longitude.str( ) + string("\n");
    29.  
    30. hdr += string("Data Type: ") + mDataType + string("\n");
    31.  
    32. return hdr;
    33. }
    To copy to clipboard, switch view to plain text mode 

    Immediatly after calling this function
    Qt Code:
    1. string hdrStr( data.getHeaderString( ) );
    To copy to clipboard, switch view to plain text mode 
    , Visual Studios 2003 returns a "Invalid Heap Pointer" exception and aborts. I was reading in forums to pass the string as a reference argument:
    Qt Code:
    1. void DataType::getHeaderString( string &hdr )
    2. {
    3. ostringstream collection,
    4. segment,
    5. altitude,
    6. longitude,
    7. latitude;
    8.  
    9. hdr += string("Timestamp: ") + mTimestamp + string("\n");
    10.  
    11. hdr += string("Description: ") + mDesc + string("\n");
    12.  
    13. collection << mCollectionNumber;
    14. hdr += string("Collection Number: ") + collection.str( ) + string("\n");
    15.  
    16. segment << mSegmentNumber;
    17. hdr += string("Segment Number: ") + segment.str( ) + string("\n");
    18.  
    19. altitude << mLocation.altitude;
    20. hdr += string("Location:\n Altitude: ") + altitude.str( ) + string("\n");
    21.  
    22. latitude << mLocation.latitude;
    23. hdr += string(" Latitude: ") + latitude.str( ) + string("\n");
    24.  
    25. longitude << mLocation.longitude;
    26. hdr += string(" Longitude: ") + longitude.str( ) + string("\n");
    27.  
    28. hdr += string("Data Type: ") + mDataType + string("\n");
    29.  
    30. return hdr;
    31. }
    To copy to clipboard, switch view to plain text mode 
    but I receive the same error when I reach the end of the scope of the string.

    Qt Code:
    1. {
    2. string hdrStr;
    3.  
    4. data.getHeaderString( hdrString );
    5. } // Error occurs after brace
    To copy to clipboard, switch view to plain text mode 

    I am really at a loss as to why this is occuring. I have used strings before in this fashion on Linux and VS 6.0 without any problems. Any suggestions?

    Thanks!

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with std::string's

    This seems like a buffer overrun. Take closer look and see if you put in the string variable some character arrays(char*) which are not NULL terminated.
    This could be a cause.
    Perhaps those class members? What type are they?

    From what you say, the error appears only at deallocation time, so the string tries to delete some memory which it does not own.

    Regards
    Last edited by marcel; 17th July 2007 at 21:59.

Similar Threads

  1. [QMYSQL] connection problem
    By chaos_theory in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd July 2007, 09:52
  2. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  3. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  4. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.