0

我发现使用 tesseract api 运行的非常简单的应用程序存在内存泄漏。我知道 tesseract api 说我必须从 GetUTF8Text() 中删除 [] char 数组,但它在我的情况下崩溃了。这是代码块:

int main(int argc, char** argv){
Mat plate_segments = imread(argv[1], 0);
tesseract::TessBaseAPI *myOCR = new tesseract::TessBaseAPI();
myOCR->SetPageSegMode(tesseract::PSM_AUTO);
if (myOCR->Init("./", "newnz+oldnz")) {

        std::cerr << "Could not initialize tesseract." << std::endl;
        exit(1);
    }
myOCR->SetVariable("tessedit_char_blacklist","abcdefghijklmnopqrstuvwxyz`~!@#$%^&*()_+-={}[]|\':;/?.<,>");
myOCR->SetVariable("tessedit_char_whitelist","ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
myOCR->SetImage((uchar*) plate_segments.data,plate_segments.size().width, plate_segments.size().height, plate_segments.channels(), plate_segments.step1());
myOCR->Recognize(0);
char* license_result = myOCR->GetUTF8Text();
printf("OCR output:\n%s", license_result );
    delete [] license_result; // CAUSE CRASH
myOCR->Clear();
myOCR->End();
return 0;}

我需要做“删除myOCR”吗?我确实尝试过,但它也崩溃了。

4

1 回答 1

0

尝试

delete myOCR;
myOCR=nullptr;

所以,你不需要打电话

myOCR->Clear();
myOCR->End();

因为删除是为你做的!(它叫 ~TessBaseAPI() )

于 2014-12-28T21:29:23.053 回答