程序在有效缓冲区的删除[]上崩溃..我认为
我有一个文件,我试图在某些函数中使用 cuComplex 和 std::complex 。
当我运行该软件时,我在这段代码中遇到崩溃,这是由于一个函数尝试使用 std::complex 而不是 cuComplex:
else
{ // data is 16 bit complex
offset rowLengthInBytes = numCols * sizeof(std::complex<unsigned short>);
std::complex<unsigned short> *buff16 = new std::complex<unsigned short>[(unsigned int)numCols];
//complex<unsigned short> *buff16 = (complex<unsigned short> *)malloc(numCols * sizeof(complex<unsigned short>));
//printf("Size of buff16: %i \n", sizeof(buff16));
offset startOfData = NitfFile.getSegmentDataOffset(cNitfReader::IMAGE, 0);
startOfData += (rowNum * rowLengthInBytes);
#ifdef _WIN32
_fseeki64(dataFile, startOfData, SEEK_SET);
#else
fseeko64(dataFile, startOfData, SEEK_SET);
#endif
fread((char*)buff16, 1, (size_t)rowLengthInBytes, dataFile);
for (size_t i = 0 ; i < numCols ; i++)
{
int re = buff16[i].real();
int im = buff16[i].imag();
#ifdef IDT_LITTLE_ENDIAN
endianSwap(&re, sizeof(re));
endianSwap(&im, sizeof(im));
#endif
buffer[i] = std::complex<float>((float)re, (float)im);
}
delete[] buff16; //program stops on this line
}
我无法弄清楚为什么 delete[] 会导致问题。我认为可能存在命名冲突,所以我在所有复杂类型前面添加了 std:: ,但这没有帮助。
此外,我编写了一个测试程序来调用这些函数,它运行良好,但是,每当我从 QT 实例化其中一些类时,就会发生崩溃。使用 QT 的调试器是我如何让它向我显示它停止在哪一行,因为通常它只是导致我的程序结束。
我怎样才能找出导致这次崩溃的原因?
编辑:
我已经验证循环后 i = numCols,这是为缓冲区分配了多少个点。
gdb 的输出:
*** glibc detected *** /work/ext/bin/TEST: double free or corruption (top): 0x00000000818dbd60 ***
======= Backtrace: =========
/lib64/libc.so.6[0x34eca7230f]
/lib64/libc.so.6(cfree+0x4b)[0x34eca7276b]
/work/ext/bin/TEST[0x415511]
/work/ext/bin/TEST[0x415543]
/work/ext/bin/TEST[0x415584]
/work/ext/bin/TEST[0x4155db]
/work/ext/bin/TEST[0x414927]
/work/ext/bin/TEST[0x423a5b]
/work/ext/bin/TEST[0x423d30]
/work/ext/bin/TEST[0x408742]
/work/ext/bin/TEST[0x407595]
/lib64/libc.so.6(__libc_start_main+0xf4)[0x34eca1d994]
/work/ext/bin/TEST(__gxx_personality_v0+0x229)[0x4062e9]
======= Memory map: ========
00400000-00466000 r-xp 00000000 fd:00 254345223 /work/ext/bin/TEST
00666000-00668000 rw-p 00066000 fd:00 254345223 /work/ext/bin/TEST
00668000-818f8000 rw-p 00668000 00:00 0 [heap]
343a400000-343a446000 r-xp 00000000 fd:00 49741877 /lib64/libssl.so.0.9.8e
343a446000-343a646000 ---p 00046000 fd:00 49741877 /lib64/libssl.so.0.9.8e
343a646000-343a64c000 rw-p 00046000 fd:00 49741877 /lib64/libssl.so.0.9.8e
343a800000-343a83b000 r-xp 00000000 fd:00 270302783 /usr/lib64/libcurl.so.3.0.0
343a83b000-343aa3b000 ---p 0003b000 fd:00 270302783 /usr/lib64/libcurl.so.3.0.0
343aa3b000-343aa3d000 rw-p 0003b000 fd:00 270302783 /usr/lib64/libcurl.so.3.0.0
34eba00000-34eba1c000 r-xp 00000000 fd:00 49741826 /lib64/ld-2.5.so
34ebc1b000-34ebc1c000 r--p 0001b000 fd:00 49741826 /lib64/ld-2.5.so
34ebc1c000-34ebc1d000 rw-p 0001c000 fd:00 49741826 /lib64/ld-2.5.so
34eca00000-34ecb4e000 r-xp 00000000 fd:00 49741833 /lib64/libc-2.5.so
34ecb4e000-34ecd4d000 ---p 0014e000 fd:00 49741833 /lib64/libc-2.5.so
34ecd4d000-34ecd51000 r--p 0014d000 fd:00 49741833 /lib64/libc-2.5.so
34ecd51000-34ecd52000 rw-p 00151000 fd:00 49741833 /lib64/libc-2.5.so
34ecd52000-34ecd57000 rw-p 34ecd52000 00:00 0
34ece00000-34ece82000 r-xp 00000000 fd:00 49741837 /lib64/libm-2.5.so
34ece82000-34ed081000 ---p 00082000 fd:00 49741837 /lib64/libm-2.5.so
34ed081000-34ed082000 r--p 00081000 fd:00 49741837 /lib64/libm-2.5.so
34ed082000-34ed083000 rw-p 00082000 fd:00 49741837 /lib64/libm-2.5.so
34ed200000-34ed202000 r-xp 00000000 fd:00 49741849 /lib64/libdl-2.5.so
34ed202000-34ed402000 ---p 00002000 fd:00 49741849 /lib64/libdl-2.5.so
34ed402000-34ed403000 r--p 00002000 fd:00 49741849 /lib64/libdl-2.5.so
34ed403000-34ed404000 rw-p 00003000 fd:00 49741849 /lib64/libdl-2.5.so
34ed600000-34ed616000 r-xp 00000000 fd:00 49741839 /lib64/libpthread-2.5.so
34ed616000-34ed815000 ---p 00016000 fd:00 49741839 /lib64/libpthread-2.5.so
34ed815000-34ed816000 r--p 00015000 fd:00 49741839 /lib64/libpthread-2.5.so
34ed816000-34ed817000 rw-p 00016000 fd:00 49741839 /lib64/libpthread-2.5.so
34ed817000-34ed81b000 rw-p 34ed817000 00:00 0
34eda00000-34eda14000 r-xp 00000000 fd:00 270295239 /usr/lib64/libz.so.1.2.3
34eda14000-34edc13000 ---p 00014000 fd:00 270295239 /usr/lib64/libz.so.1.2.3
34edc13000-34edc14000 rw-p 00013000 fd:00 270295239 /usr/lib64/libz.so.1.2.3
34ede00000-34ede07000 r-xp 00000000 fd:00 49741841 /lib64/librt-2.5.so
34ede07000-34ee007000 ---p 00007000 fd:00 49741841 /lib64/librt-2.5.so
34ee007000-34ee008000 r--p 00007000 fd:00 49741841 /lib64/librt-2.5.so
34ee008000-34ee009000 rw-p 00008000 fd:00 49741841 /lib64/librt-2.5.so
34f4800000-34f4815000 r-xp 00000000 fd:00 49742011 /lib64/libnsl-2.5.so
34f4815000-34f4a14000 ---p 00015000 fd:00 49742011 /lib64/libnsl-2.5.so
34f4a14000-34f4a15000 r--p 00014000 fd:00 49742011 /lib64/libnsl-2.5.so
34f4a15000-34f4a16000 rw-p 00015000 fd:00 49742011 /lib64/libnsl-2.5.so
34f4a16000-34f4a18000 rw-p 34f4a16000 00:00 0
34f6800000-34f692d000 r-xp 00000000 fd:00 49742042 /lib64/libcrypto.so.0.9.8e
34f692d000-34f6b2c000 ---p 0012d000 fd:00 49742042 /lib64/libcrypto.so.0.9.8e
34f6b2c000-34f6b4d000 rw-p 0012c000 fd:00 49742042 /lib64/libcrypto.so.0.9.8e
34f6b4d000-34f6b51000 rw-p 34f6b4d000 00:00 0
34f7000000-34f7011000 r-xp 00000000 fd:00 49742019 /lib64/libresolv-2.5.so
34f7011000-34f7211000 ---p 00011000 fd:00 49742019 /lib64/libresolv-2.5.so
34f7211000-34f7212000 r--p 00011000 fd:00 49742019 /lib64/libresolv-2.5.so
34f7212000-34f7213000 rw-p 00012000 fd:00 49742019 /lib64/libresolv-2.5.so
34f7213000-34f7215000 rw-p 34f7213000 00:00 0
34f7400000-34f7402000 r-xp 00000000 fd:00 49742018 /lib64/libkeyutils-1.2.so
34f7402000-34f7601000 ---p 00002000 fd:00 49742018 /lib64/libkeyutils-1.2.so
34f7601000-34f7602000 rw-p 00001000 fd:00 49742018 /lib64/libkeyutils-1.2.so
34f7800000-34f7802000 r-xp 00000000 fd:00 49742040 /lib64/libcom_err.so.2.1
34f7802000-34f7a01000 ---p 00002000 fd:00 49742040 /lib64/libcom_err.so.2.1
34f7a01000-34f7a02000 rw-p 00001000 fd:00 49742040 /lib64/libcom_err.so.2.1
34fdc00000-34fdc0d000 r-xp 00000000 fd:00 49742037 /lib64/libgcc_s-4.1.2-20080825.so.1
34fdc0d000-34fde0d000 ---p 0000d000 fd:00 49742037 /lib64/libgcc_s-4.1.2-20080825.so.1
34fde0d000-34fde0e000 rw-p 0000d000 fd:00 49742037 /lib64/libgcc_s-4.1.2-20080825.so.1
34ff000000-34ff0e6000 r-xp 00000000 fd:00 270284428 /usr/lib64/libstdc++.so.6.0.8
34ff0e6000-34ff2e5000 ---p 000e6000 fd:00 270284428 /usr/lib64/libstdc++.so.6.0.8
34ff2e5000-34ff2eb000 r--p 000e5000 fd:00 270284428 /usr/lib64/libstdc++.so.6.0.8
34ff2eb000-34ff2ee000 rw-p 000eb000 fd:00 270284428 /usr/lib64/libstdc++.so.6.0.8
34ff2ee000-34ff300000 rw-p 34ff2ee000 00:00 0
3bf9c00000-3bf9c31000 r-xp 00000000 fd:00 270298839 /usr/lib64/libidn.so.11.5.19
3bf9c31000-3bf9e30000 ---p 00031000 fd:00 270298839 /usr/lib64/libidn.so.11.5.19
3bf9e30000-3bf9e31000 rw-p 00030000 fd:00 270298839 /usr/lib64/libidn.so.11.5.19
3bfc800000-3bfc808000 r-xp 00000000 fd:00 270298915 /usr/lib64/libkrb5support.so.0.1
3bfc808000-3bfca07000 ---p 00008000 fd:00 270298915 /usr/lib64/libkrb5support.so.0.1
3bfca07000-3bfca08000 rw-p 00007000 fd:00 270298915 /usr/lib64/libkrb5support.so.0.1
3bfcc00000-3bfcc15000 r-xp 00000000 fd:00 25198595 /lib64/libselinux.so.1
3bfcc15000-3bfce15000 ---p 00015000 fd:00 25198595 /lib64/libselinux.so.1
3bfce15000-3bfce17000 rw-p 00015000 fd:00 25198595 /lib64/libselinux.so.1
3bfce17000-3bfce18000 rw-p 3bfce17000 00:00 0
3bfd000000-3bfd091000 r-xp 00000000 fd:00 270299139 /usr/lib64/libkrb5.so.3.3
3bfd091000-3bfd291000 ---p 00091000 fd:00 270299139 /usr/lib64/libkrb5.so.3.3
3bfd291000-3bfd295000 rw-p 00091000 fd:00 270299139 /usr/lib64/libkrb5.so.3.3
3bfd400000-3bfd424000 r-xp 00000000 fd:00 270299138 /usr/lib64/libk5crypto.so.3.1
3bfd424000-3bfd623000 ---p 00024000 fd:00 270299138 /usr/lib64/libk5crypto.so.3.1
3bfd623000-3bfd625000 rw-p 00023000 fd:00 270299138 /usr/lib64/libk5crypto.so.3.1
3bfd800000-3bfd83b000 r-xp 00000000 fd:00 25198594 /lib64/libsepol.so.1
3bfd83b000-3bfda3b000 ---p 0003b000 fd:00 25198594 /lib64/libsepol.so.1
3bfda3b000-3bfda3c000 rw-p 0003b000 fd:00 25198594 /lib64/libsepol.so.1
3bfda3c000-3bfda46000 rw-p 3bfda3c000 00:00 0
3bfdc00000-3bfdc2c000 r-xp 00000000 fd:00 270299140 /usr/lib64/libgssapi_krb5.so.2.2
3bfdc2c000-3bfde2c000 ---p 0002c000 fd:00 270299140 /usr/lib64/libgssapi_krb5.so.2.2
3bfde2c000-3bfde2e000 rw-p 0002c000 fd:00 270299140 /usr/lib64/libgssapi_krb5.so.2.2
2aaaaaaab000-2aaaaaaae000 rw-p 2aaaaaaab000 00:00 0
2aaaaaaae000-2aaaaaab5000 r--s 00000000 fd:00 147620082 /usr/lib64/gconv/gconv-modules.cache
2aaaaaacf000-2aaaaaad0000 rw-p 2aaaaaacf000 00:00 0
2aaaaaad0000-2aaaab25d000 r-xp 00000000 fd:00 270295189 /usr/lib64/libcuda.so.260.19.26
2aaaab25d000-2aaaab45c000 ---p 0078d000 fd:00 270295189 /usr/lib64/libcuda.so.260.19.26
2aaaab45c000-2aaaab528000 rw-p 0078c000 fd:00 270295189 /usr/lib64/libcuda.so.260.19.26
2aaaab528000-2aaaab54c000 rw-p 2aaaab528000 00:00 0
2aaaab54c000-2aaaab902000 r-xp 00000000 fd:00 254312453 /work/ext/lib/Linux/libxerces-c-3.1.so
2aaaab902000-2aaaabb02000 ---p 003b6000 fd:00 254312453 /work/ext/lib/Linux/libxerces-c-3.1.so
2aaaabb02000-2aaaabb43000 rw-p 003b6000 fd:00 254312453 /work/ext/lib/Linux/libxerces-c-3.1.so
2aaaabb43000-2aaaabba2000 r-xp 00000000 fd:00 271156027 /usr/local/cuda/lib64/libcudart.so.3.2.16
2aaaabba2000-2aaaabda2000 ---p 0005f000 fd:00 271156027 /usr/local/cuda/lib64/libcudart.so.3.2.16
2aaaabda2000-2aaaabda3000 rw-p 0005f000 fd:00 271156027 /usr/local/cuda/lib64/libcudart.so.3.2.16
2aaaabda3000-2aaaabdae000 rw-p 2aaaabda3000 00:00 0
2aaaabdae000-2aaaabdb0000 r-xp 00000000 fd:00 147620070 /usr/lib64/gconv/UTF-16.so
2aaaabdb0000-2aaaabfaf000 ---p 00002000 fd:00 147620070 /usr/lib64/gconv/UTF-16.so
2aaaabfaf000-2aaaabfb1000 rw-p 00001000 fd:00 147620070 /usr/lib64/gconv/UTF-16.so
2aaaabfb1000-2aaaabfb6000 rw-p 2aaaabfb1000 00:00 0
2aaaabfda000-2aaaabfdb000 rw-p 2aaaabfda000 00:00 0
7ffffffea000-7ffffffff000 rwxp 7ffffffe9000 00:00 0 [stack]
ffffffffff600000-ffffffffffe00000 ---p 00000000 00:00 0 [vdso]
Program received signal SIGABRT, Aborted.
0x00000034eca30265 in raise () from /lib64/libc.so.6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您尝试执行的操作称为序列化。强烈建议您仅通过读/写存储对象实例的内存缓冲区来实现此目的。这样,一些不相关的内部数据(例如内存指针)可能会被逐字写入文件 - 并且稍后在读入时,它将无效。
考虑使用一些现有的序列化库来操作
vector >
实例,例如 Boost.Serialization。您也可以查看这个问题作为示例。
What you are trying to do is called serialization. You are strongly discouraged to do it by just reading/writing memory buffers in which your objects instances are stored. This way some unrelated internal data like memory pointers may get written to the file verbatim — and later, when read in, it won't be valid.
Consider using some existing serialization library for operating instances of
vector<complex<float> >
, for example Boost.Serialization.You may look at this question for examples too.
发布的代码对我来说似乎也是有效的(尽管不可能确定我们只知道一些的部分 - 就像大多数变量一样)。无缘无故出现的错误可能表明堆损坏 - 由程序中其他地方的某些东西引起。
但是,在这种情况下不需要 new[]/delete[]。为什么不直接使用 std::vector - 它只会更好,并且是为此目的(以及其他目的)而构建的。
The posted code seems valid to me, too (although it's impossible to be certain of the parts that we know only something of - like most variables). A bug that's showing up for no reason could indicate heap corruption - caused by something, elsewhere in the program.
However, there's no need for new[]/delete[] in a case like this. Why not just use std::vector - it's only better, and built for this purpose (among others).
没有什么明显的问题,但最可能的问题是
buffer
不够大,无法容纳numCols
项,并且它实际上溢出并覆盖buff16
。Nothing obvious stands out, but the most likely problem is that
buffer
isn't big enough to holdnumCols
items and it's actually overflowing and overwritingbuff16
.