Вопрос

I'm trying to transfer a large file in a non-blocking fashion, by connecting bytesWritten to my function sendNextBlock.

void AsynchronousRetrieveCommand::start()
{
    connect(socket(), SIGNAL(bytesWritten(qint64)), this, SLOT(sendNextBlock()));
    sendNextBlock();
}

void AsynchronousRetrieveCommand::sendNextBlock()
{
    socket->write(file->read(64*1024));
}

I'm running this code on a Symbian phone, and after 5-6 megabytes have been transferred I'm getting "Memory Full" message box in the phone, and this message in the debug output:

[Qt Message] CActiveScheduler::RunIfReady() returned error: -4

I assume this is some kind of a memory leak, but I can't see what's causing it in my code.

Это было полезно?

Решение

Ok, it turned out the socket's buffer was growing uncontrollably because data was being fed to it faster than it could be flushed.

I fixed the problem by checking the value given by bytesWritten, and writing only so much bytes (in effect, refilling the buffer back to 64k).

My fixed code now looks like this:

void AsynchronousRetrieveCommand::start()
{
    connect(socket(), SIGNAL(bytesWritten(qint64)), this, SLOT(sendNextBlock(qint64)));
    sendNextBlock(64*1024);
}

void AsynchronousRetrieveCommand::sendNextBlock(qint64 bytes)
{
    socket()->write(file->read(bytes));
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top