首页 > 解决方案 > 在创建 pdf 文件期间处理 HPDF_FAILD_TO_ALLOC_MEM

问题描述

我正在使用 libharu 库来创建 pdf 并进入系统

hPDF error error_no=4117, detail_no=0 which HPDF_FAILD_TO_ALLOC_MEM 

我的查询可能知道此错误背后的原因。并且应用程序崩溃了。有没有办法处理这个错误,因为我无法生成 pdf

**Error: hPDF error error_no=4117, detail_no=0
***Error: hPDF error error_no=4117, detail_no=0
***Error: hPDF error error_no=4117, detail_no=0
***Error: hPDF error error_no=4117, detail_no=0
***Error: hPDF error error_no=4117, detail_no=0
***Error: hPDF error error_no=4117, detail_no=0
***Error: hPDF error error_no=4177, detail_no=0
***Error: hPDF error error_no=4177, detail_no=0
***Error: hPDF error error_no=4177, detail_no=0
***Error: hPDF error error_no=4177, detail_no=0
***Error: hPDF error error_no=4177, detail_no=0
***Error: hPDF error error_no=4177, detail_no=0
***Error: hPDF error error_no=4177, detail_no=0
***Error: hPDF error error_no=4133, detail_no=0
***Error: (Report::NextPage) current page is empty
***Fatal: *************************** FATAL ERROR ***************************


***Fatal: /usr/lib64/libc.so.6(+0x35270) [0x7ffff6801270]
***Fatal: HPDF_Page_SetWidth+0x49 [0x596159]

我曾尝试编写小程序并尝试重现此问题,但没有遇到任何错误。

#include<iostream>
#include "hpdf.h"
using namespace std;

class Report {
    public:
    HPDF_Doc document;
    HPDF_Page currentpage;
    void static error_handler (HPDF_STATUS,HPDF_STATUS,void *);
    void NextPage();
    Report(string, string, int);
    ~Report();
    string m_ReportFilePath;
};

void Report::NextPage()
{
    if (document == NULL)
    cerr << "(Report::NextPage) document is empty" << endl;

    currentpage = HPDF_AddPage(document);
    if (currentpage == NULL)
    cerr << "(Report::NextPage) current page is empty" << endl;
    cout<<currentpage<<"  "<<std::endl;
    HPDF_Page_SetWidth(currentpage, 595.3);
}

Report::Report(string fileName, string booktitle, int marg) {
    m_ReportFilePath = fileName;
    document = HPDF_New(Report::error_handler, &document);
    NextPage();         // Create new page
}

Report::~Report()
{
}

void Report::error_handler(HPDF_STATUS error_no,
               HPDF_STATUS detail_no, void *user_data)
{
    cout << "hPDF error error_no=" << (HPDF_UINT) error_no <<
    ", detail_no=" << (HPDF_UINT) detail_no << endl;
}

int main() {

    Report r("test.pdf", "test", 50);
    return 0;
}

编译命令

/depot/gcc-4.8.2/bin/g++ -rdynamic -Wall -I../include -std=c++11 -c lib_haru.cpp -o lib_haru -L../libs -lhpdf /depot/gcc-4.8.2/bin/g++ -o libh lib_haru.o -rdynamic -L../libs -lm -ltcl8.4 -lxml2 -lz -lpng -lhpdf -lncurses -lreadline -Wl,-rpath,/depot/gcc-4.8.2/lib64

OBJECTS=$(addsuffix .o,$(basename $(SRC)))
VERSION=$(USER)
CXXFLAGS=-g -D Version=$(VERSION) -rdynamic  -Wall -I../include
LDFLAGS=-rdynamic -L../libs -lm -ltcl8.4 -lxml2 -lz -lpng -lhpdf -lncurses -lreadline -Wl,-rpath,/depot/gcc-4.8.2/lib64 
#CXX= /usr/bin/g++
CXX= /depot/gcc-4.8.2/bin/g++

我在这台机器上编译时的问题

Linux Machine 3.10.0-957.5.1.el7.x86_64 #1 SMP Fri Feb 1 14:54:57 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

GDB 输出

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff6852c31 in __strlen_sse2 () from /usr/lib64/libc.so.6

可能的问题

currentpage = HPDF_AddPage(document);
if (currentpage == NULL)
logerror << "(Report::NextPage) current page is empty" << endl;
#value of currentpage is 0

标签: c++libharu

解决方案


我唯一一次看到此错误是当 PDF 变得如此之大以致耗尽所有可用内存时。就我而言,我使用 PDF 基元(如填充矩形)创建图像。

您确实没有提供足够的信息来帮助诊断问题。libHaru 的版本,32 位或 64 位,通常您尝试做的事情会有所帮助。


推荐阅读