1

I have a file that I am trying to use both cuComplex and std::complex in some functions.

When I run the softwar, I get a crash in this block of code, from a function trying to use std::complex and not 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

    }

I cannot figure out why that delete[] is causing problems. I thoguht maybe there was a naming conflict, so I added std:: in front of all the complex types, but that didnt help.

Additionally, I made a test program to call these functions and it runs fine, however, the crash is happening whenever I instantiate some of these classes from QT. Using QT's debugger is how I got it to show me which line it is stopping at, beacuse normally it is just causing my program to end.

How can I figure out what is causing this crash?

edit:

I have verified that i is = numCols after the loop, which is how many spots were allocated for buffer.

Output from 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
Derek
  • 11,715
  • 32
  • 127
  • 228
  • 1
    What's `buffer`? How and where is it declared/allocated? It is possible that your are overrunning your `buffer` and corrupting the heap. But there's no way to say from the code you posted. – AnT stands with Russia Jan 28 '11 at 19:48
  • buffer is allocated int he calling function, like so: complex *pixelData = new complex[numCols]; – Derek Jan 28 '11 at 20:02
  • I agree with ulidtko answer about serialization. Could you provide us with stacktrace? Is the size of std::complex indeed 4 bytes (e.g. there are no internal pointers; Although on my PC it is 4 bytes)? Also I see a commented malloc line - malloc and delete[] does not go together (but I guess you know this). – user389238 Jan 28 '11 at 20:17
  • 1
    Please note that `std::complex` used with anything other than `float`, `double`, or `long double` is *unspecified*. You might try using a simple `unsigned short[2]` array instead of reading your data as a `complex`. – Rob Kennedy Jan 28 '11 at 20:19
  • I have edited the post with the stacktrace from gdb. Note, this comes after I change the implementation to a vector, like in one of the answers below – Derek Jan 28 '11 at 20:43
  • Also - does anyone have any insight into why this would only show up if I compile with a flag -DUSING_CUDA that allows me to compile the CUDA stuff I wrote? I am not even calling those functions yet, it is just in the same source file. – Derek Jan 28 '11 at 21:00
  • @ROb Kennedy I tried to implement it your way. I ended up using a std::pair(short, short) but I still get the same crash – Derek Jan 28 '11 at 21:22

3 Answers3

4

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.

Community
  • 1
  • 1
ulidtko
  • 14,740
  • 10
  • 56
  • 88
2

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).

{
  std::vector<std::complex<unsigned short> > vector(numCols);
  std::complex<unsigned short>* buff16 = &vector[0];
  /* code */
}
eq-
  • 9,986
  • 36
  • 38
  • well, when I did this, I at least got it to crash in my stand-alone version as well,a nd it actually says *** glibc detected *** /work/ext/bin/TEST: double free or corruption (top): 0x00000000818dbd60 *** – Derek Jan 28 '11 at 19:53
2

Nothing obvious stands out, but the most likely problem is that buffer isn't big enough to hold numCols items and it's actually overflowing and overwriting buff16.

Mark B
  • 95,107
  • 10
  • 109
  • 188