打开不存在的打印机时遇到错误。当找到打印机时,我的应用程序工作正常。但是当找不到打印机时,我会遇到双重释放或损坏错误。下面是我的代码:
#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)错误:
if(ePrinterType == UNKNOWN)
{
cout << "printer not found" << endl;
throw Exception("Printer not found");
}我找不到错误实际发生的位置。请帮帮忙
发布于 2019-12-12 15:17:12
看看这几行--你真的想把它们都做完吗--解释你为什么要这么做……
pclose(pPortFile);
...
fclose(pPortFile);
...
pclose(pPortFile);https://stackoverflow.com/questions/59299202
复制相似问题