Unsolved An error occurred: bad allocation
-
@SPlatten said in An error occurred: bad allocation:
mpTCPsocket->write(crbaData);
So at a guess wait till
QAbstractSocket::waitForBytesWritten()
orQIODevice::bytesWritten()
(see note especially if Windows) there to make sure the write is not complaining too much has not been written yet when next chunk is sent? You should debug to see whenAn error occurred: bad allocation
arrives. -
@JonB , thank you, will give it a go.
-
@SPlatten said in An error occurred: bad allocation:
I'm not sure what you mean
What I mean is that you are writing data to the socket faster than it can send it...
-
@jsulm , ok, thank you, I've added a call to waitForBytesWritten to the sendBinary function.
-
@SPlatten said in An error occurred: bad allocation:
I've added a call to waitForBytesWritten to the sendBinary function.
Are you on Linux or Windows?
-
@JonB This project is on Windows 10.
-
@SPlatten So I wrote:
or
QIODevice::bytesWritten()
(see note especially if Windows)There is a reason I wrote that for you, I thought you would look at it before implementing. Did you read QAbstractSocket::waitForBytesWritten
Note: This function may fail randomly on Windows. Consider using the event loop and the
bytesWritten()
signal if your software will run on Windows.? Up to you, just be aware.
-
@JonB , thank you, will implement bytesWritten too.
-
@SPlatten
:) Of course be aware you cannot have both in your running code, they will get in each other's way, one or the other! -
@JonB , looking at the documentation:
https://doc.qt.io/qt-5/qiodevice.html#write-2write returns the number of bytes written, how can this work when you call a function like waitForBytesWritten after calling write?
-
For buffered devices, this function [
waitForBytesWritten()
] waits until a payload of buffered written data has been written to the deviceI think there is some distinction between
write()
accepting the bytes initially into a buffer versus those buffered bytes being sent to the device. Easy enough to use the signal to check, or look at whatever the source code does forwaitForBytesWritten()
.In any case: if you quickly try
waitForBytesWritten()
(and it works under Windows, I don't know, else use the signal) and your error message goes away you know where you are. If it does not, you do not.