程序崩溃在这个有效的缓冲区的删除[] ..我想

时间:2011-01-28 19:33:47

标签: c++ qt cuda

我有一个文件,我试图在某些函数中使用cuComplex和std :: complex。

当我运行softwar时,我在这段代码中遇到了崩溃,试图使用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

    }

我无法弄清楚删除[]导致问题的原因。我觉得可能存在命名冲突,所以我在所有复杂类型的前面添加了std ::但这并没有帮助。

此外,我制作了一个测试程序来调用这些函数并运行正常,但每当我从QT实例化其中一些类时,崩溃就会发生。使用QT的调试器是我如何让它向我展示它停在哪一行,因为通常它只是导致我的程序结束。

我怎样才能找出导致此次崩溃的原因?

编辑:

我已经确认循环后我是= 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

3 个答案:

答案 0 :(得分:4)

您要做的是serialization。强烈建议您不要只读取/写入存储对象实例的内存缓冲区。这样一些不相关的内部数据(如内存指针)可能会逐字写入文件 - 稍后,在读入时,它将无效。

考虑使用一些现有的序列化库来操作vector<complex<float> >的实例,例如Boost.Serialization

您也可以查看this问题。

答案 1 :(得分:2)

发布的代码对我来说似乎也是有效的(虽然不可能确定我们只知道某些部分 - 就像大多数变量一样)。一个无缘无故地出现的错误可能表明堆损坏 - 由程序中其他地方引起的。

但是,在这种情况下,不需要新的[] / delete []。为什么不使用std :: vector - 它只是更好,并且为此目的而构建(以及其他)。

{
  std::vector<std::complex<unsigned short> > vector(numCols);
  std::complex<unsigned short>* buff16 = &vector[0];
  /* code */
}

答案 2 :(得分:2)

没有什么明显的突出,但最可能的问题是buffer不足以容纳numCols项,而实际上是溢出并覆盖buff16