zlib内存不足的问题
Out of memory problem with zlib
我想使用zlib压缩字符串。如果我在大约一个小时后将这个函数放入循环中,"compress"返回-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;
}
我能看到的唯一可疑的地方是您将int destLen
作为uLongf
类型的输出参数提供。如果uLongf
大于int
,这可能会破坏您的堆栈,并且"长"部分表明在64位平台上可能会出现这种情况。
我建议您立即将destLen
声明为uLongf
类型,并避免强制转换。
除此之外,我看不出你的代码有任何问题。
相关文章:
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 在内存不足之前,我可以声明多少个 const 变量?
- 如何避免由于内存不足而导致 gcc 崩溃
- 检测到由于操作系统内存不足而导致子进程终止
- 导出 vulkan 内存分配句柄会导致设备内存不足
- 如何消除ROS打开CV中的内存不足错误?
- 32 位应用程序内存不足
- 在本机 Android 应用中处理内存不足
- 在 Linux 上C++的无限循环中捕获内存不足的错误 bad_alloc()
- JNA 参数问题:内存访问无效
- 处理静态数组内存C++的内存不足情况
- 访问违规可以是一个伪装的内存不足错误吗
- g++ 内存不足为 std::bitset 分配
- C++ 分配器在内存不足时使应用程序崩溃
- 使用队列的非递归回溯:内存不足
- C++ : 内存不足时应返回什么?
- 使用 sqlite3 时内存不足
- std::int 和 struct 内存不足的映射 (std::Bad_alloc) c++
- 如何解决似乎不太可能是内存不足问题的bad_alloc
- zlib内存不足的问题