2011-01-28 83 views
1

我有一個文件,我想在一些函數中使用cuComplex和std :: complex。程序崩潰這是有效的緩衝區[]的緩衝區..我認爲

當我運行softwar,我得到一個崩潰的代碼塊,從函數試圖使用的std ::複雜,不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 
+1

什麼是「緩衝區」?它如何以及在哪裏宣佈/分配?有可能你的緩衝區溢出並破壞了堆。但是你無法從你發佈的代碼中說出來。 – AnT 2011-01-28 19:48:52

+0

緩衝區被分配給他調用函數,如下所示:complex * pixelData = new complex [numCols]; – Derek 2011-01-28 20:02:16

回答

2

張貼的代碼看起來有效的對我來說,太(雖然它不可能是一定的,我們只知道事情的部分 - 像大多數變量)。無緣無故顯示的錯誤可能表示堆損壞 - 由程序中的其他位置導致。

但是,在這種情況下不需要新的[]/delete []。爲什麼不使用std :: vector - 它只是更好,併爲此目的而構建(等等)。

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

沒有明顯突出,但最可能的問題是buffer是不是大到足以容納numCols項目,它實際上四溢和覆蓋buff16

4

你要做的是叫做serialization。強烈建議您不要只讀/寫存儲對象實例的內存緩衝區。通過這種方式,一些無關的內部數據(如內存指針)可能會逐字寫入文件 - 稍後,在讀入時,它將無效。

考慮使用一些現有的序列化庫來操作vector<complex<float> >實例,例如Boost.Serialization

你也可以看看this問題。