[英]Qt : How to debug a bad alloc exception in QByteArray?
I have a QNetworkReply
and I want to store the downloaded bytes in a QByteArray
. 我有一个
QNetworkReply
,我想将下载的字节存储在QByteArray
。
connect(_replyRef, SIGNAL(readyRead()), this, SLOT(PushDownloadedBytesToFile()));
void PushDownloadedBytesToFile()
{
_internalBufferBytes.append(_replyRef->readAll());
}
The problem is that I get a bad alloc
when the size is approximately 33552950. 问题是,当大小约为33552950时,我得到了一个
bad alloc
分配。
I do not understand what is the problem or how can I debug this. 我不明白这是什么问题,或者如何调试。
From you comment, regarding maintaining 40 MB in buffer, I'd suggest to reserve this much memory (more would be better) in advance using QByteArray::reserve()
API. 根据您的评论,关于在缓冲区中保留40 MB,我建议使用
QByteArray::reserve()
API提前预留这么多的内存(更好的方法是QByteArray::reserve()
。 But, with QByteArray
, you have to maintain the total size, consumed size, current index, etc. 但是,使用
QByteArray
,您必须维护总大小,消耗的大小,当前索引等。
Another solution could be to use QString
with reserve() . 另一种解决方案是将
QString
与reserve()结合使用 。 You wouldn't have to maintain the details. 您无需维护细节。 You can simply use
QString::append()
and then convert it to QByteArray
using QString::toUtf8()
whenever you need it. 您可以简单地使用
QString::append()
,然后在需要时使用QString::toUtf8()
将其转换为QByteArray
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.