zlib 内存不足问题
我想使用 zlib 压缩字符串。如果我在大约一个小时后将此函数放入循环中,“压缩”将返回 -4,这意味着 Z_MEM_ERROR。有人知道问题出在哪里吗?
std::string compressData(std::string const& line)
{
char *src=(char*)line.c_str();
int srcLen=strlen(src);
int destLen=compressBound(srcLen);
char *dest=new char[destLen];
int result=compress((unsigned char *)dest ,(uLongf*)&destLen ,(const unsigned char *)src ,srcLen );
QByteArray sd = QByteArray::fromRawData(dest, destLen);
QString hexZipData (sd.toHex());
std::string hexZipDataStr = hexZipData.toStdString();
if( result != Z_OK)
{
hexZipDataStr = "";
std::cout << "error !";
}
delete []dest;
dest = NULL;
return hexZipDataStr;
}
I want to compress a string using zlib. If I put this function in a loop after about an hour "compress" returns -4 which means Z_MEM_ERROR. Anybody knows where is the problem?
std::string compressData(std::string const& line)
{
char *src=(char*)line.c_str();
int srcLen=strlen(src);
int destLen=compressBound(srcLen);
char *dest=new char[destLen];
int result=compress((unsigned char *)dest ,(uLongf*)&destLen ,(const unsigned char *)src ,srcLen );
QByteArray sd = QByteArray::fromRawData(dest, destLen);
QString hexZipData (sd.toHex());
std::string hexZipDataStr = hexZipData.toStdString();
if( result != Z_OK)
{
hexZipDataStr = "";
std::cout << "error !";
}
delete []dest;
dest = NULL;
return hexZipDataStr;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看到的唯一可疑的地方是您提供
int destLen
作为uLongf
类型的输出参数。如果uLongf
大于int
,这可能会导致堆栈崩溃,并且“long”部分表明在 64 位平台上可能会出现这种情况。我建议您立即声明
uLongf
类型的destLen
并避免强制转换。除此之外,我看不出您的代码有任何问题。
The only suspicious place I can see is that you supply your
int destLen
as an output parameter of typeuLongf
. This may blow up your stack ifuLongf
is larger thanint
, and the "long" part suggests it could be so on 64-bit platforms.I would recommend you to declare
destLen
of typeuLongf
right away and avoid the cast.Other than that I cannot se any problems with your code.