Originally Posted by
walito
I was testing and I think the problem is the memory, because the readline() method don't free him buffer and the program crash (memcpy)
Have you any evidence for this assertion?
How can I free the memory?
Allow the QTextStream and QString to go out of scope.
I'm doing "watch -n1 free -m" and the memory start to down until the program crash.
Something in your program, that is not in the code we have been shown is allocating memory and not freeing it: perhaps storing data about each line. Eventually, a memory allocation fails and the attempt to copy something into it crashes. That this occurs inside Qt code does not make it a Qt problem.
If you want to test for actual memory leaks, which are exceptionally unlikely in the relevant Qt code, then use valgrind or something similar. System level tools like free are not useful in finding memory leaks. See this thread for more:
http://www.qtcentre.org/threads/3742...ee+memory+leak
I thought the readline() free the memory
QTextStream and QString both manage their memory.
#include <QtCore>
#include <QDebug>
int main(int argc, char *argv[])
{
// 3 million lines of 100 characters into temp file
QFile f
("/tmp/test.txt");
data.append('\n');
qDebug() << "Writing";
for (int line = 0; line < 3000000; ++line) {
f.write(data);
}
f.close();
}
// Read it line at a time
qDebug() << "Reading...";
while (!f.atEnd())
line = s.readLine();
f.close();
}
}
#include <QtCore>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
// 3 million lines of 100 characters into temp file
QFile f("/tmp/test.txt");
QByteArray data(99, 'A');
data.append('\n');
qDebug() << "Writing";
if (f.open(QIODevice::WriteOnly)) {
for (int line = 0; line < 3000000; ++line) {
f.write(data);
}
f.close();
}
// Read it line at a time
qDebug() << "Reading...";
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream s(&f);
QString line;
while (!f.atEnd())
line = s.readLine();
f.close();
}
}
To copy to clipboard, switch view to plain text mode
$ valgrind --leak-ckeck=yes ./test
...
Writing
Reading...
==6051==
==6051== HEAP SUMMARY:
==6051== in use at exit: 10,966 bytes in 144 blocks
==6051== total heap usage: 3,055,495 allocs, 3,055,351 frees, 3,058,183,080 bytes allocated
==6051==
...
==6051== LEAK SUMMARY:
==6051== definitely lost: 0 bytes in 0 blocks // NO LEAKS
==6051== indirectly lost: 0 bytes in 0 blocks //
==6051== possibly lost: 3,136 bytes in 29 blocks
==6051== still reachable: 7,830 bytes in 115 blocks
==6051== suppressed: 0 bytes in 0 blocks
==6051== Reachable blocks (those to which a pointer was found) are not shown.
==6051== To see them, rerun with: --leak-check=full --show-reachable=yes
==6051==
==6051== For counts of detected and suppressed errors, rerun with: -v
==6051== Use --track-origins=yes to see where uninitialised values come from
==6051== ERROR SUMMARY: 19 errors from 18 contexts (suppressed: 0 from 0)
$ valgrind --leak-ckeck=yes ./test
...
Writing
Reading...
==6051==
==6051== HEAP SUMMARY:
==6051== in use at exit: 10,966 bytes in 144 blocks
==6051== total heap usage: 3,055,495 allocs, 3,055,351 frees, 3,058,183,080 bytes allocated
==6051==
...
==6051== LEAK SUMMARY:
==6051== definitely lost: 0 bytes in 0 blocks // NO LEAKS
==6051== indirectly lost: 0 bytes in 0 blocks //
==6051== possibly lost: 3,136 bytes in 29 blocks
==6051== still reachable: 7,830 bytes in 115 blocks
==6051== suppressed: 0 bytes in 0 blocks
==6051== Reachable blocks (those to which a pointer was found) are not shown.
==6051== To see them, rerun with: --leak-check=full --show-reachable=yes
==6051==
==6051== For counts of detected and suppressed errors, rerun with: -v
==6051== Use --track-origins=yes to see where uninitialised values come from
==6051== ERROR SUMMARY: 19 errors from 18 contexts (suppressed: 0 from 0)
To copy to clipboard, switch view to plain text mode
All of the small possible 'leaks' here are coming from libraries outside Qt (glib in my case).
Have you tried rebuilding the entire application yet?
Can you produce a small test program and set of data that breaks for you?
Bookmarks