首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复双重释放或损坏错误

如何修复双重释放或损坏错误
EN

Stack Overflow用户
提问于 2019-12-12 14:54:21
回答 1查看 62关注 0票数 0

打开不存在的打印机时遇到错误。当找到打印机时,我的应用程序工作正常。但是当找不到打印机时,我会遇到双重释放或损坏错误。下面是我的代码:

代码语言:javascript
复制
#define BUFSIZE 200

FILE *pPortFile = NULL;

void Printer::closePrinter()
{
    if (pPortFile != NULL)
    {
        fclose(pPortFile);
        pPortFile = NULL;
    }
}

void Printer::openPrinter(string sPortName)
{
    struct stat tFilest;
    int iFileDescriptor = 0, i = 0;
    char aResponse[BUFSIZE] = {0}, aFName[BUFSIZE];

    ePrinterType_ = UNKNOWN;

    closePrinter();

    pPortFile = popen("find /sys/devices/platform -name lp0 -print", "r");

    if (pPortFile != NULL)
    {
        i = fread(aResponse, sizeof(char), BUFSIZE - 1, pPortFile);

        aResponse[i] = '\0';

        if(i != 0)
        {
            pclose(pPortFile);
            strcpy(aFName, dirname(aResponse));
            strcat(aFName, "/../../idProduct");
            pPortFile = fopen(aFName, "r");            

            if (i != 0 && pPortFile != NULL)
            {
                i = fread(aResponse, sizeof(char), BUFSIZE - 1, pPortFile);
                aResponse[i] = '\0';

                if (strstr(aResponse,"3538"))
                    ePrinterType_ = PRINTER_1;
                else if (strstr(aResponse,"2305"))
                    ePrinterType_ = PRINTER_2;
            }

            fclose(pPortFile);
        }

        pclose(pPortFile);

    }

    if(ePrinterType_ == UNKNOWN)
    {
        cout << "printer not found" << endl;
        throw Exception("Printer not found");
    }
    else
    {
        pPortFile = fopen(sPortName.c_str(), "r+");
        iFileDescriptor = fileno(pPortFile);
    }
}

我在以下行之后遇到双重释放或损坏(!prev)错误:

代码语言:javascript
复制
if(ePrinterType == UNKNOWN)
{
    cout << "printer not found" << endl;
    throw Exception("Printer not found");
}

我找不到错误实际发生的位置。请帮帮忙

EN

回答 1

Stack Overflow用户

发布于 2019-12-12 15:17:12

看看这几行--你真的想把它们都做完吗--解释你为什么要这么做……

代码语言:javascript
复制
        pclose(pPortFile);
        ...
        fclose(pPortFile);
    ...
    pclose(pPortFile);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59299202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档