Talk:How to perform file IO using QDataStream in Qt
Contents |
Dor666 - Memory leak
Hi,
I've tried this code like this in boost::test:
BOOST_AUTO_TEST_CASE( QFileAndQDataStreamBasicUsage)
{
QFile file("c://test.txt");
file.open(QIODevice::WriteOnly);
QDataStream out(&file); // we will serialize the data into the file
out << "welcome to the Qt World"; // serialize a string
file.close();
QFile read("c://test.txt");
read.open(QIODevice::ReadOnly);
QDataStream in(&read); // read the data serialized from the file
QString str;
in >> str;
}
And it gives me memory leaks:
Detected memory leaks!
Dumping objects ->
{9762} normal block at 0x009A4108, 40 bytes long.
Data: < @B > 00 00 00 00 CC CD CD CD 40 42 0F 00 00 00 00 00
{9761} normal block at 0x009BC1B8, 120 bytes long.
Data: <|cO\ L d\> 7C 63 4F 5C B0 06 9A 00 00 00 00 00 4C B9 64 5C
{9760} normal block at 0x009A06B0, 8 bytes long.
Data: <<cO\ > 3C 63 4F 5C B8 C1 9B 00
{9759} normal block at 0x009A40B0, 40 bytes long.
Data: < @B > 00 00 00 00 CC CD CD CD 40 42 0F 00 00 00 00 00
{9758} normal block at 0x009AFE78, 60 bytes long.
Data: < > 01 00 00 00 B0 06 9A 00 C4 1E 00 00 00 CD CD CD
Object dump complete.
dor666 16:39, 20 November 2012 (EET)
Hamishwillee - Fixed up text
Hi Dor666
Firstly, thanks for posting this feedback. I've tidied your layout - you can use Help:Code Syntax Highlighting for marking up code -see Help:Formatting for other wiki markup. To edit the text look for "Admin" link next to the "Comment" title.
I suspect the problem is that QFile read is opened but never closed. Can you try replacing
in >> str;
with
in >> str; read.close()
Regards
Hamishhamishwillee 05:45, 21 November 2012 (EET)
Dor666 -
Hi Hammish. Thank you for quick reply and layout fix.
I think, that file is closed automatically in QFile destructor.
I've tried also this:
for (int i = 0; i < 100; ++i)
{
QString fileName = QString("test%1.txt").arg(i);
QFile file(fileName);
file.open(QIODevice::WriteOnly);
QDataStream out(&file); // we will serialize the data into the file
out << "welcome to the Qt World"; // serialize a string
file.close();
QFile read(fileName);
read.open(QIODevice::ReadOnly);
QDataStream in(&read); // read the data serialized from the file
QString str;
in >> str;
read.close();
}
dor666 09:12, 21 November 2012 (EET)
Hamishwillee - I think you are probably right!
I have heard similar things before, but I can't find a reference sorry.hamishwillee 07:40, 22 November 2012 (EET)

