我搜索了关于这个问题的其他问题,并发现了它们的负荷,但我觉得它们不适合我的特殊情况。 我需要使用g ++(Debian 6.3.0-18)6.3.0从Debian 9 GNU / Linux上的PCSClite中检索智能卡读卡器列表。 此类列表采用PCSC API方法返回的 char * 数组的形式。 每个读者名称通过 \ 000 空字符与相邻名称分隔。一旦char数组正确形成(我用gdb检查),我使用以下代码片段来构建名称列表:
LONG iterationTotal = 0; // total number of processed characters
std::list<char*> ReaderList; // the final list of names
while (iterationTotal < dwReaders) { // dwReaders is returned by PCSC
// it is the total length of the
// char* array
LONG i = 0;
while (mszReaders[i] != NULL) { //mszReaders is the char* array
i++;
iterationTotal++;
}
iterationTotal++; // counts the NULL characters that separe names
char * tempReader = new char(i + 1); // allocate just enough
//memory for the current name
strcpy(tempReader, mszReaders); // copy name contents
std::cout << tempReader << std::endl; // this prints CORRECTLY the
// names of the readers
mszReaders += i; // "erases" the previous name
mszReaders += 1; // takes into account the NULL char
ReaderList.push_back(tempReader); // add element to list
//DELETE!
//delete[] tempReader; // I cannot uncomment this! Don't know why
i = 0;
}
for (std::list<char*>::iterator it = ReaderList.begin(); it != ReaderList.end(); it++) {
std::cout << *it << std::endl; // shows the elements present in
// the list
}
执行时的输出是:
bjorn@debianHDD:~/Documents/Arjo/products/omnilector_C#_original/trials$ ./listReaders
Established context.
Successfully obtained reader list
Identiv Identiv uTrust 4701 F Dual Interface Reader [uTrust 4701 F CL Reader] (55041546201112) 00 00
Identiv Identiv uTrust 4701 F Dual Interface Reader [uTrust 4701 F Contact Reader] (55041546201112) 01 00
Identiv Identiv uTrust 4!
Identiv Identiv uTrust 4!
208
*** Error in `./listReaders': free(): invalid next size (fast): 0x0000561a18e3d370 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x70bcb)[0x7ffbe2f55bcb]
/lib/x86_64-linux-gnu/libc.so.6(+0x76f96)[0x7ffbe2f5bf96]
/lib/x86_64-linux-gnu/libc.so.6(+0x777de)[0x7ffbe2f5c7de]
./listReaders(+0x1904)[0x561a172f0904]
./listReaders(+0x17bc)[0x561a172f07bc]
./listReaders(+0x1632)[0x561a172f0632]
./listReaders(+0x14b3)[0x561a172f04b3]
./listReaders(+0x12f8)[0x561a172f02f8]
./listReaders(+0x12a4)[0x561a172f02a4]
./listReaders(+0x10dc)[0x561a172f00dc]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1)[0x7ffbe2f052b1]
./listReaders(+0xd3a)[0x561a172efd3a]
======= Memory map: ========
561a172ef000-561a172f2000 r-xp 00000000 08:12 7350775 /home/bjorn/Documents/Arjo/products/omnilector_C#_original/trials/listReaders
561a174f1000-561a174f2000 r--p 00002000 08:12 7350775 /home/bjorn/Documents/Arjo/products/omnilector_C#_original/trials/listReaders
561a174f2000-561a174f3000 rw-p 00003000 08:12 7350775 /home/bjorn/Documents/Arjo/products/omnilector_C#_original/trials/listReaders
561a18e2b000-561a18e5d000 rw-p 00000000 00:00 0 [heap]
7ffbdc000000-7ffbdc021000 rw-p 00000000 00:00 0
7ffbdc021000-7ffbe0000000 ---p 00000000 00:00 0
7ffbe28bc000-7ffbe28d4000 r-xp 00000000 08:12 10488851 /lib/x86_64-linux-gnu/libpthread-2.24.so
7ffbe28d4000-7ffbe2ad3000 ---p 00018000 08:12 10488851 /lib/x86_64-linux-gnu/libpthread-2.24.so
7ffbe2ad3000-7ffbe2ad4000 r--p 00017000 08:12 10488851 /lib/x86_64-linux-gnu/libpthread-2.24.so
7ffbe2ad4000-7ffbe2ad5000 rw-p 00018000 08:12 10488851 /lib/x86_64-linux-gnu/libpthread-2.24.so
7ffbe2ad5000-7ffbe2ad9000 rw-p 00000000 00:00 0
7ffbe2ad9000-7ffbe2ae0000 r-xp 00000000 08:12 10488867 /lib/x86_64-linux-gnu/librt-2.24.so
7ffbe2ae0000-7ffbe2cdf000 ---p 00007000 08:12 10488867 /lib/x86_64-linux-gnu/librt-2.24.so
7ffbe2cdf000-7ffbe2ce0000 r--p 00006000 08:12 10488867 /lib/x86_64-linux-gnu/librt-2.24.so
7ffbe2ce0000-7ffbe2ce1000 rw-p 00007000 08:12 10488867 /lib/x86_64-linux-gnu/librt-2.24.so
7ffbe2ce1000-7ffbe2ce4000 r-xp 00000000 08:12 10486046 /lib/x86_64-linux-gnu/libdl-2.24.so
7ffbe2ce4000-7ffbe2ee3000 ---p 00003000 08:12 10486046 /lib/x86_64-linux-gnu/libdl-2.24.so
7ffbe2ee3000-7ffbe2ee4000 r--p 00002000 08:12 10486046 /lib/x86_64-linux-gnu/libdl-2.24.so
7ffbe2ee4000-7ffbe2ee5000 rw-p 00003000 08:12 10486046 /lib/x86_64-linux-gnu/libdl-2.24.so
7ffbe2ee5000-7ffbe307a000 r-xp 00000000 08:12 10486022 /lib/x86_64-linux-gnu/libc-2.24.so
7ffbe307a000-7ffbe327a000 ---p 00195000 08:12 10486022 /lib/x86_64-linux-gnu/libc-2.24.so
7ffbe327a000-7ffbe327e000 r--p 00195000 08:12 10486022 /lib/x86_64-linux-gnu/libc-2.24.so
7ffbe327e000-7ffbe3280000 rw-p 00199000 08:12 10486022 /lib/x86_64-linux-gnu/libc-2.24.so
7ffbe3280000-7ffbe3284000 rw-p 00000000 00:00 0
7ffbe3284000-7ffbe329a000 r-xp 00000000 08:12 10485968 /lib/x86_64-linux-gnu/libgcc_s.so.1
7ffbe329a000-7ffbe3499000 ---p 00016000 08:12 10485968 /lib/x86_64-linux-gnu/libgcc_s.so.1
7ffbe3499000-7ffbe349a000 r--p 00015000 08:12 10485968 /lib/x86_64-linux-gnu/libgcc_s.so.1
7ffbe349a000-7ffbe349b000 rw-p 00016000 08:12 10485968 /lib/x86_64-linux-gnu/libgcc_s.so.1
7ffbe349b000-7ffbe359e000 r-xp 00000000 08:12 10486050 /lib/x86_64-linux-gnu/libm-2.24.so
7ffbe359e000-7ffbe379d000 ---p 00103000 08:12 10486050 /lib/x86_64-linux-gnu/libm-2.24.so
7ffbe379d000-7ffbe379e000 r--p 00102000 08:12 10486050 /lib/x86_64-linux-gnu/libm-2.24.so
7ffbe379e000-7ffbe379f000 rw-p 00103000 08:12 10486050 /lib/x86_64-linux-gnu/libm-2.24.so
7ffbe379f000-7ffbe3911000 r-xp 00000000 08:12 12584202 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
7ffbe3911000-7ffbe3b11000 ---p 00172000 08:12 12584202 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
7ffbe3b11000-7ffbe3b1b000 r--p 00172000 08:12 12584202 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
7ffbe3b1b000-7ffbe3b1d000 rw-p 0017c000 08:12 12584202 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
7ffbe3b1d000-7ffbe3b21000 rw-p 00000000 00:00 0
7ffbe3b21000-7ffbe3b2b000 r-xp 00000000 08:12 12593178 /usr/lib/x86_64-linux-gnu/libpcsclite.so.1.0.0
7ffbe3b2b000-7ffbe3d2a000 ---p 0000a000 08:12 12593178 /usr/lib/x86_64-linux-gnu/libpcsclite.so.1.0.0
7ffbe3d2a000-7ffbe3d2b000 r--p 00009000 08:12 12593178 /usr/lib/x86_64-linux-gnu/libpcsclite.so.1.0.0
7ffbe3d2b000-7ffbe3d2c000 rw-p 0000a000 08:12 12593178 /usr/lib/x86_64-linux-gnu/libpcsclite.so.1.0.0
7ffbe3d2c000-7ffbe3d4f000 r-xp 00000000 08:12 10485946 /lib/x86_64-linux-gnu/ld-2.24.so
7ffbe3f16000-7ffbe3f1c000 rw-p 00000000 00:00 0
7ffbe3f4b000-7ffbe3f4f000 rw-p 00000000 00:00 0
7ffbe3f4f000-7ffbe3f50000 r--p 00023000 08:12 10485946 /lib/x86_64-linux-gnu/ld-2.24.so
7ffbe3f50000-7ffbe3f51000 rw-p 00024000 08:12 10485946 /lib/x86_64-linux-gnu/ld-2.24.so
7ffbe3f51000-7ffbe3f52000 rw-p 00000000 00:00 0
7ffd8cc53000-7ffd8cc74000 rw-p 00000000 00:00 0 [stack]
7ffd8cc81000-7ffd8cc83000 r--p 00000000 00:00 0 [vvar]
7ffd8cc83000-7ffd8cc85000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted
正如您所看到的,返回时会出现问题,因此在释放列表占用的内存时会发生这种情况。支持这个想法:如果我在while循环中取消注释DELETE行(我真的应该删除该变量以避免内存泄漏),程序将停止使用此 free():无效的下一个大小(快速):错误。 此外,我不明白为什么列表只显示包含一小部分名称,push_back方法应该将 tempReader 的全部内容添加到列表中。 我试图通过Valgrind运行程序,但老实说我看不出问题(我可以发布它的输出,如果它可以帮助) 错误的原因是什么?我该如何解决这种情况?