I want to keep an array of data index by a string and I thought I'd best use std::map for this purpose. I have below example code:
typedef struct MyType_s {
long long timestamp;
int cnt;
bool parked;
}MyType;
static MyType *list = {0};
static int listcnt = 0;
//-------------------------------------------------------------------------------------------------
int map_add_item(std::map<std::string, MyType*> *pmap, std::string str, long long tmestmp)
{
if (listcnt == 0){
list =(MyType*)malloc(sizeof(MyType));
if (list)
listcnt++;
else
return ENOMEM;
}
if (realloc(list,sizeof(MyType)*(++listcnt))==0)
return ENOMEM;
list->timestamp = tmestmp;
if (!(str.length()&&tmestmp&&pmap))
return EINVAL*-1;
if (pmap->insert(std::make_pair(str, &list[listcnt-1])).second == false){
pmap->find(str)->second->timestamp = tmestmp;
return EEXIST*-1;
}
return OK;
}
which compiles fine but I get a mem dump like this when I run it:
*** Error in `./std_map': double free or corruption (fasttop): 0x000000000226ec20 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f19fb6267e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7f19fb62f37a]
/lib/x86_64-linux-gnu/libc.so.6(+0x83350)[0x7f19fb632350]
/lib/x86_64-linux-gnu/libc.so.6(realloc+0x179)[0x7f19fb633839]
./std_map[0x4013c8]
./std_map[0x40198d]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f19fb5cf830]
./std_map[0x401259]
======= Memory map: ========
00400000-00404000 r-xp 00000000 08:02 6054199 /path/to/src/tmp/std_map
00604000-00605000 r--p 00004000 08:02 6054199 /path/to/src/tmp/std_map
00605000-00606000 rw-p 00005000 08:02 6054199 /path/to/src/tmp/std_map
0225d000-0228f000 rw-p 00000000 00:00 0 [heap]
7f19f4000000-7f19f4021000 rw-p 00000000 00:00 0
7f19f4021000-7f19f8000000 ---p 00000000 00:00 0
7f19fb2a6000-7f19fb3ae000 r-xp 00000000 08:02 28971840 /lib/x86_64-linux-gnu/libm-2.23.so
7f19fb3ae000-7f19fb5ad000 ---p 00108000 08:02 28971840 /lib/x86_64-linux-gnu/libm-2.23.so
7f19fb5ad000-7f19fb5ae000 r--p 00107000 08:02 28971840 /lib/x86_64-linux-gnu/libm-2.23.so
7f19fb5ae000-7f19fb5af000 rw-p 00108000 08:02 28971840 /lib/x86_64-linux-gnu/libm-2.23.so
7f19fb5af000-7f19fb76f000 r-xp 00000000 08:02 28971844 /lib/x86_64-linux-gnu/libc-2.23.so
7f19fb76f000-7f19fb96f000 ---p 001c0000 08:02 28971844 /lib/x86_64-linux-gnu/libc-2.23.so
7f19fb96f000-7f19fb973000 r--p 001c0000 08:02 28971844 /lib/x86_64-linux-gnu/libc-2.23.so
7f19fb973000-7f19fb975000 rw-p 001c4000 08:02 28971844 /lib/x86_64-linux-gnu/libc-2.23.so
7f19fb975000-7f19fb979000 rw-p 00000000 00:00 0
7f19fb979000-7f19fb98f000 r-xp 00000000 08:02 28971397 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f19fb98f000-7f19fbb8e000 ---p 00016000 08:02 28971397 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f19fbb8e000-7f19fbb8f000 rw-p 00015000 08:02 28971397 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f19fbb8f000-7f19fbd01000 r-xp 00000000 08:02 23072621 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f19fbd01000-7f19fbf01000 ---p 00172000 08:02 23072621 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f19fbf01000-7f19fbf0b000 r--p 00172000 08:02 23072621 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f19fbf0b000-7f19fbf0d000 rw-p 0017c000 08:02 23072621 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f19fbf0d000-7f19fbf11000 rw-p 00000000 00:00 0
7f19fbf11000-7f19fbf37000 r-xp 00000000 08:02 28971842 /lib/x86_64-linux-gnu/ld-2.23.so
7f19fc0fc000-7f19fc102000 rw-p 00000000 00:00 0
7f19fc135000-7f19fc136000 rw-p 00000000 00:00 0
7f19fc136000-7f19fc137000 r--p 00025000 08:02 28971842 /lib/x86_64-linux-gnu/ld-2.23.so
7f19fc137000-7f19fc138000 rw-p 00026000 08:02 28971842 /lib/x86_64-linux-gnu/ld-2.23.so
7f19fc138000-7f19fc139000 rw-p 00000000 00:00 0
7ffd68bf4000-7ffd68c16000 rw-p 00000000 00:00 0 [stack]
7ffd68d88000-7ffd68d8b000 r--p 00000000 00:00 0 [vvar]
7ffd68d8b000-7ffd68d8d000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Command terminated
gdb doesn't give me much more information too.
This is after I call the function like this from main():
int main()
{
int ret = 0;
std::map<std::string, MyType*> mapOfWords;
std::cout << map_add_item(&mapOfWords,"earth",time(NULL)+1) << std::endl;
return 0;
}
I'm wondering what I'm doing wrong, double free or corruption obviously is a hint but I'm not sure how to interpret this and I can't pinpoint the problem....
For a working solution, I replaced the global static variable and the pointers in the map with data that is located directly in the map. Plus I removed the typedef from the struct and declared it the C++ way instead, as in:
std::map<std::string, MyType> *pmap
struct MyType {
long long timestamp;
int cnt;
bool parked;
};
Related
I'm trying to execute a very basic C++ program that interacts with MySQL. I implemented the very beginning of the tutorial for MySQL Connector/C++, but I keep getting a segfault when I try to execute that code. Here is the code that I'm trying to execute:
try
{
sql::mysql::MySQL_Driver *driver = NULL;
sql::Connection *conn = NULL;
driver = sql::mysql::get_mysql_driver_instance();
if (driver != NULL)
{
conn = driver->connect("127.0.0.1:3306", "root", "root");
cout << "Test" << endl;
}
delete conn;
conn = NULL;
}
When I execute this code, "Test" gets printed, but I get the following error:
*** Error in `./test': free(): invalid pointer: 0x00007f0a9a292158 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f0a999a87e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7f0a999b137a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f0a999b553c]
./test(_ZN3sql9SQLStringD1Ev+0x18)[0x401296]
/lib/x86_64-linux-gnu/libc.so.6(+0x39ff8)[0x7f0a9996aff8]
/lib/x86_64-linux-gnu/libc.so.6(+0x3a045)[0x7f0a9996b045]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf7)[0x7f0a99951837]
./test[0x400e79]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:01 6167619
/home/lichtsb1/local/src/ddsWork/mysqlTest/test
00601000-00602000 r--p 00001000 08:01 6167619
/home/lichtsb1/local/src/ddsWork/mysqlTest/test
00602000-00603000 rw-p 00002000 08:01 6167619
/home/lichtsb1/local/src/ddsWork/mysqlTest/test
02357000-023aa000 rw-p 00000000 00:00 0
[heap]
7f0a94000000-7f0a94021000 rw-p 00000000 00:00 0
7f0a94021000-7f0a98000000 ---p 00000000 00:00 0
7f0a989a8000-7f0a98ab0000 r-xp 00000000 08:01 43253857
/lib/x86_64-linux-gnu/libm-2.23.so
7f0a98ab0000-7f0a98caf000 ---p 00108000 08:01 43253857
/lib/x86_64-linux-gnu/libm-2.23.so
7f0a98caf000-7f0a98cb0000 r--p 00107000 08:01 43253857
/lib/x86_64-linux-gnu/libm-2.23.so
7f0a98cb0000-7f0a98cb1000 rw-p 00108000 08:01 43253857
/lib/x86_64-linux-gnu/libm-2.23.so
7f0a98cb1000-7f0a98cb4000 r-xp 00000000 08:01 43254080
/lib/x86_64-linux-gnu/libdl-2.23.so
7f0a98cb4000-7f0a98eb3000 ---p 00003000 08:01 43254080
/lib/x86_64-linux-gnu/libdl-2.23.so
7f0a98eb3000-7f0a98eb4000 r--p 00002000 08:01 43254080
/lib/x86_64-linux-gnu/libdl-2.23.so
7f0a98eb4000-7f0a98eb5000 rw-p 00003000 08:01 43254080
/lib/x86_64-linux-gnu/libdl-2.23.so
7f0a98eb5000-7f0a99074000 r-xp 00000000 08:01 7733609
/home/lichtsb1/local/src/ddsWork/mysqlTest/mysql-connector-c++-8.0.13-linux-glibc2.12-x86-64bit/lib64/libcrypto.so.1.0.0
7f0a99074000-7f0a99273000 ---p 001bf000 08:01 7733609
/home/lichtsb1/local/src/ddsWork/mysqlTest/mysql-connector-c++-8.0.13-linux-glibc2.12-x86-64bit/lib64/libcrypto.so.1.0.0
7f0a99273000-7f0a9929a000 rw-p 001be000 08:01 7733609
/home/lichtsb1/local/src/ddsWork/mysqlTest/mysql-connector-c++-8.0.13-linux-glibc2.12-x86-64bit/lib64/libcrypto.so.1.0.0
7f0a9929a000-7f0a9929d000 rw-p 00000000 00:00 0
7f0a9929d000-7f0a99302000 r-xp 00000000 08:01 7733606
/home/lichtsb1/local/src/ddsWork/mysqlTest/mysql-connector-c++-8.0.13-linux-glibc2.12-x86-64bit/lib64/libssl.so.1.0.0
7f0a99302000-7f0a99502000 ---p 00065000 08:01 7733606
/home/lichtsb1/local/src/ddsWork/mysqlTest/mysql-connector-c++-8.0.13-linux-glibc2.12-x86-64bit/lib64/libssl.so.1.0.0
7f0a99502000-7f0a9950c000 rw-p 00065000 08:01 7733606
/home/lichtsb1/local/src/ddsWork/mysqlTest/mysql-connector-c++-8.0.13-linux-glibc2.12-x86-64bit/lib64/libssl.so.1.0.0
7f0a9950c000-7f0a99513000 r-xp 00000000 08:01 43254289
/lib/x86_64-linux-gnu/librt-2.23.so
7f0a99513000-7f0a99712000 ---p 00007000 08:01 43254289
/lib/x86_64-linux-gnu/librt-2.23.so
7f0a99712000-7f0a99713000 r--p 00006000 08:01 43254289
/lib/x86_64-linux-gnu/librt-2.23.so
7f0a99713000-7f0a99714000 rw-p 00007000 08:01 43254289
/lib/x86_64-linux-gnu/librt-2.23.so
7f0a99714000-7f0a9972c000 r-xp 00000000 08:01 43253992
/lib/x86_64-linux-gnu/libpthread-2.23.so
7f0a9972c000-7f0a9992b000 ---p 00018000 08:01 43253992
/lib/x86_64-linux-gnu/libpthread-2.23.so
7f0a9992b000-7f0a9992c000 r--p 00017000 08:01 43253992
/lib/x86_64-linux-gnu/libpthread-2.23.so
7f0a9992c000-7f0a9992d000 rw-p 00018000 08:01 43253992
/lib/x86_64-linux-gnu/libpthread-2.23.so
7f0a9992d000-7f0a99931000 rw-p 00000000 00:00 0
7f0a99931000-7f0a99af1000 r-xp 00000000 08:01 43254051
/lib/x86_64-linux-gnu/libc-2.23.so
7f0a99af1000-7f0a99cf1000 ---p 001c0000 08:01 43254051
/lib/x86_64-linux-gnu/libc-2.23.so
7f0a99cf1000-7f0a99cf5000 r--p 001c0000 08:01 43254051
/lib/x86_64-linux-gnu/libc-2.23.so
7f0a99cf5000-7f0a99cf7000 rw-p 001c4000 08:01 43254051
/lib/x86_64-linux-gnu/libc-2.23.so
7f0a99cf7000-7f0a99cfb000 rw-p 00000000 00:00 0
7f0a99cfb000-7f0a99d11000 r-xp 00000000 08:01 43254905
/lib/x86_64-linux-gnu/libgcc_s.so.1
7f0a99d11000-7f0a99f10000 ---p 00016000 08:01 43254905
/lib/x86_64-linux-gnu/libgcc_s.so.1
7f0a99f10000-7f0a99f11000 rw-p 00015000 08:01 43254905
/lib/x86_64-linux-gnu/libgcc_s.so.1
7f0a99f11000-7f0a9a083000 r-xp 00000000 08:01 20972156
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0a9a083000-7f0a9a283000 ---p 00172000 08:01 20972156
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0a9a283000-7f0a9a28d000 r--p 00172000 08:01 20972156
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0a9a28d000-7f0a9a28f000 rw-p 0017c000 08:01 20972156
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0a9a28f000-7f0a9a293000 rw-p 00000000 00:00 0
7f0a9a293000-7f0a9a6ed000 r-xp 00000000 08:01 7733614
/home/lichtsb1/local/src/ddsWork/mysqlTest/mysql-connector-c++-8.0.13-linux-glibc2.12-x86-64bit/lib64/libmysqlcppconn.so.7.8.0.13
7f0a9a6ed000-7f0a9a8ed000 ---p 0045a000 08:01 7733614
/home/lichtsb1/local/src/ddsWork/mysqlTest/mysql-connector-c++-8.0.13-linux-glibc2.12-x86-64bit/lib64/libmysqlcppconn.so.7.8.0.13
7f0a9a8ed000-7f0a9a8f8000 r--p 0045a000 08:01 7733614
/home/lichtsb1/local/src/ddsWork/mysqlTest/mysql-connector-c++-8.0.13-linux-glibc2.12-x86-64bit/lib64/libmysqlcppconn.so.7.8.0.13
7f0a9a8f8000-7f0a9aab7000 rw-p 00465000 08:01 7733614
/home/lichtsb1/local/src/ddsWork/mysqlTest/mysql-connector-c++-8.0.13-linux-glibc2.12-x86-64bit/lib64/libmysqlcppconn.so.7.8.0.13
7f0a9aab7000-7f0a9aabd000 rw-p 00000000 00:00 0
7f0a9aabd000-7f0a9aae3000 r-xp 00000000 08:01 43253979
/lib/x86_64-linux-gnu/ld-2.23.so
7f0a9ac7b000-7f0a9acb7000 r--s 00000000 08:01 16523805
/var/cache/nscd/services
7f0a9acb7000-7f0a9acbe000 rw-p 00000000 00:00 0
7f0a9ace0000-7f0a9ace2000 rw-p 00000000 00:00 0
7f0a9ace2000-7f0a9ace3000 r--p 00025000 08:01 43253979
/lib/x86_64-linux-gnu/ld-2.23.so
7f0a9ace3000-7f0a9ace4000 rw-p 00026000 08:01 43253979
/lib/x86_64-linux-gnu/ld-2.23.so
7f0a9ace4000-7f0a9ace5000 rw-p 00000000 00:00 0
7ffe3b05c000-7ffe3b07e000 rw-p 00000000 00:00 0
[stack]
7ffe3b0dd000-7ffe3b0e0000 r--p 00000000 00:00 0
[vvar]
7ffe3b0e0000-7ffe3b0e2000 r-xp 00000000 00:00 0
[vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
[vsyscall]
Aborted (core dumped)
This is so basic - what am I doing wrong?
Thank you very much for any help.
I am having this segmentation fault after a colleague of mine committed some new features on our project. The new feature has no impact on the following function, but only increases the QString argument size.
After some debugging, I've figured out that segmentation fault was happening after the following function return.
I need to know why does it is happening and why my workaround worked, and if the workaround is safe?
I must say that the following snippet it is working on older version of Qt (5.3.1 with gcc 4.8) that I have now installed (Qt 5.9 with gcc 7.0).
It also works nicely on Debug rather that Release compiling mode on Qt.
Finally, I don't want to discuss the effectiveness of this encryption method, but the entitled problem.
The crashes happens immediately after function returning, I have no idea why.
QString Utility::encrypt(QString text)
{
QByteArray textUtf8 = text.toUtf8();
// Convert QString to Char
const char *srcString = textUtf8.constData();
char encrypted[ textUtf8.size() ];
// Copy Char by Char
strcpy(encrypted,srcString);
for(int u=0; u<textUtf8.size(); u++ ){
encrypted[u]++;
}
return QString::fromUtf8(encrypted);
}
The output from Segmentation fault is:
*** Error in `/home/user/workspace/build-swFree-Desktop_Qt_5_9_1_GCC_64bit-Release/swFree': free(): invalid pointer: 0x0000000001cfc800 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7eff199067e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7eff1990f37a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7eff1991353c]
/home/user/workspace/build-swFree-Desktop_Qt_5_9_1_GCC_64bit-Release/swFree[0x40171f]
/home/user/workspace/build-swFree-Desktop_Qt_5_9_1_GCC_64bit-Release/swFree[0x401344]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7eff198af830]
/home/user/workspace/build-swFree-Desktop_Qt_5_9_1_GCC_64bit-Release/swFree[0x4014b9]
======= Memory map: ========
00400000-00403000 r-xp 00000000 08:01 2098854 /home/user/workspace/build-swFree-Desktop_Qt_5_9_1_GCC_64bit-Release/swFree
00602000-00603000 r--p 00002000 08:01 2098854 /home/user/workspace/build-swFree-Desktop_Qt_5_9_1_GCC_64bit-Release/swFree
00603000-00604000 rw-p 00003000 08:01 2098854 /home/user/workspace/build-swFree-Desktop_Qt_5_9_1_GCC_64bit-Release/swFree
01cdd000-01d1f000 rw-p 00000000 00:00 0 [heap]
7eff10000000-7eff10021000 rw-p 00000000 00:00 0
7eff10021000-7eff14000000 ---p 00000000 00:00 0
7eff162bb000-7eff16594000 r--p 00000000 08:01 2752945 /usr/lib/locale/locale-archive
7eff16594000-7eff16602000 r-xp 00000000 08:01 529479 /lib/x86_64-linux-gnu/libpcre.so.3.13.2
7eff16602000-7eff16802000 ---p 0006e000 08:01 529479 /lib/x86_64-linux-gnu/libpcre.so.3.13.2
7eff16802000-7eff16803000 r--p 0006e000 08:01 529479 /lib/x86_64-linux-gnu/libpcre.so.3.13.2
7eff16803000-7eff16804000 rw-p 0006f000 08:01 529479 /lib/x86_64-linux-gnu/libpcre.so.3.13.2
7eff16804000-7eff1690c000 r-xp 00000000 08:01 529373 /lib/x86_64-linux-gnu/libm-2.23.so
7eff1690c000-7eff16b0b000 ---p 00108000 08:01 529373 /lib/x86_64-linux-gnu/libm-2.23.so
7eff16b0b000-7eff16b0c000 r--p 00107000 08:01 529373 /lib/x86_64-linux-gnu/libm-2.23.so
7eff16b0c000-7eff16b0d000 rw-p 00108000 08:01 529373 /lib/x86_64-linux-gnu/libm-2.23.so
7eff16b0d000-7eff16c1c000 r-xp 00000000 08:01 529394 /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.2
7eff16c1c000-7eff16e1b000 ---p 0010f000 08:01 529394 /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.2
7eff16e1b000-7eff16e1c000 r--p 0010e000 08:01 529394 /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.2
7eff16e1c000-7eff16e1d000 rw-p 0010f000 08:01 529394 /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.2
7eff16e1d000-7eff16e1e000 rw-p 00000000 00:00 0
7eff16e1e000-7eff16e1f000 r-xp 00000000 08:01 2763515 /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0.4800.2
7eff16e1f000-7eff1701e000 ---p 00001000 08:01 2763515 /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0.4800.2
7eff1701e000-7eff1701f000 r--p 00000000 08:01 2763515 /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0.4800.2
7eff1701f000-7eff17020000 rw-p 00001000 08:01 2763515 /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0.4800.2
7eff17020000-7eff17023000 r-xp 00000000 08:01 529264 /lib/x86_64-linux-gnu/libdl-2.23.so
7eff17023000-7eff17222000 ---p 00003000 08:01 529264 /lib/x86_64-linux-gnu/libdl-2.23.so
7eff17222000-7eff17223000 r--p 00002000 08:01 529264 /lib/x86_64-linux-gnu/libdl-2.23.so
7eff17223000-7eff17224000 rw-p 00003000 08:01 529264 /lib/x86_64-linux-gnu/libdl-2.23.so
7eff17224000-7eff1723d000 r-xp 00000000 08:01 529327 /lib/x86_64-linux-gnu/libz.so.1.2.8
7eff1723d000-7eff1743c000 ---p 00019000 08:01 529327 /lib/x86_64-linux-gnu/libz.so.1.2.8
7eff1743c000-7eff1743d000 r--p 00018000 08:01 529327 /lib/x86_64-linux-gnu/libz.so.1.2.8
7eff1743d000-7eff1743e000 rw-p 00019000 08:01 529327 /lib/x86_64-linux-gnu/libz.so.1.2.8
7eff1743e000-7eff18c21000 r--p 00000000 08:01 2379568 /opt/Qt5.9.1/5.9.1/gcc_64/lib/libicudata.so.56.1
7eff18c21000-7eff18e20000 ---p 017e3000 08:01 2379568 /opt/Qt5.9.1/5.9.1/gcc_64/lib/libicudata.so.56.1
7eff18e20000-7eff18e21000 r--p 017e2000 08:01 2379568 /opt/Qt5.9.1/5.9.1/gcc_64/lib/libicudata.so.56.1
7eff18e21000-7eff18fc6000 r-xp 00000000 08:01 2379575 /opt/Qt5.9.1/5.9.1/gcc_64/lib/libicuuc.so.56.1
7eff18fc6000-7eff191c6000 ---p 001a5000 08:01 2379575 /opt/Qt5.9.1/5.9.1/gcc_64/lib/libicuuc.so.56.1
7eff191c6000-7eff191d6000 r--p 001a5000 08:01 2379575 /opt/Qt5.9.1/5.9.1/gcc_64/lib/libicuuc.so.56.1
7eff191d6000-7eff191d7000 rw-p 001b5000 08:01 2379575 /opt/Qt5.9.1/5.9.1/gcc_64/lib/libicuuc.so.56.1
7eff191d7000-7eff191d9000 rw-p 00000000 00:00 0
7eff191d9000-7eff19462000 r-xp 00000000 08:01 2379569 /opt/Qt5.9.1/5.9.1/gcc_64/lib/libicui18n.so.56.1
7eff19462000-7eff19661000 ---p 00289000 08:01 2379569 /opt/Qt5.9.1/5.9.1/gcc_64/lib/libicui18n.so.56.1
7eff19661000-7eff1966f000 r--p 00288000 08:01 2379569 /opt/Qt5.9.1/5.9.1/gcc_64/lib/libicui18n.so.56.1
7eff1966f000-7eff19671000 rw-p 00296000 08:01 2379569 /opt/Qt5.9.1/5.9.1/gcc_64/lib/libicui18n.so.56.1
7eff19671000-7eff19672000 rw-p 00000000 00:00 0
7eff19672000-7eff1968a000 r-xp 00000000 08:01 529099 /lib/x86_64-linux-gnu/libpthread-2.23.so
7eff1968a000-7eff19889000 ---p 00018000 08:01 529099 /lib/x86_64-linux-gnu/libpthread-2.23.so
7eff19889000-7eff1988a000 r--p 00017000 08:01 529099 /lib/x86_64-linux-gnu/libpthread-2.23.so
7eff1988a000-7eff1988b000 rw-p 00018000 08:01 529099 /lib/x86_64-linux-gnu/libpthread-2.23.so
7eff1988b000-7eff1988f000 rw-p 00000000 00:00 0
7eff1988f000-7eff19a4f000 r-xp 00000000 08:01 529417 /lib/x86_64-linux-gnu/libc-2.23.so
7eff19a4f000-7eff19c4f000 ---p 001c0000 08:01 529417 /lib/x86_64-linux-gnu/libc-2.23.so
7eff19c4f000-7eff19c53000 r--p 001c0000 08:01 529417 /lib/x86_64-linux-gnu/libc-2.23.so
7eff19c53000-7eff19c55000 rw-p 001c4000 08:01 529417 /lib/x86_64-linux-gnu/libc-2.23.so
7eff19c55000-7eff19c59000 rw-p 00000000 00:00 0
7eff19c59000-7eff19c6f000 r-xp 00000000 08:01 529390 /lib/x86_64-linux-gnu/libgcc_s.so.1
7eff19c6f000-7eff19e6e000 ---p 00016000 08:01 529390 /lib/x86_64-linux-gnu/libgcc_s.so.1
7eff19e6e000-7eff19e6f000 rw-p 00015000 08:01 529390 /lib/x86_64-linux-gnu/libgcc_s.so.1
7eff19e6f000-7eff19fe1000 r-xp 00000000 08:01 2764170 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7eff19fe1000-7eff1a1e1000 ---p 00172000 08:01 2764170 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7eff1a1e1000-7eff1a1eb000 r--p 00172000 08:01 2764170 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7eff1a1eb000-7eff1a1ed000 rw-p 0017c000 08:01 2764170 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7eff1a1ed000-7eff1a1f1000 rw-p 00000000 00:00 0
7eff1a1f1000-7eff1a71b000 r-xp 00000000 08:01 2380819 /opt/Qt5.9.1/5.9.1/gcc_64/lib/libQt5Core.so.5.9.1
7eff1a71b000-7eff1a91a000 ---p 0052a000 08:01 2380819 /opt/Qt5.9.1/5.9.1/gcc_64/lib/libQt5Core.so.5.9.1
7eff1a91a000-7eff1a926000 r--p 00529000 08:01 2380819 /opt/Qt5.9.1/5.9.1/gcc_64/lib/libQt5Core.so.5.9.1
7eff1a926000-7eff1a928000 rw-p 00535000 08:01 2380819 /opt/Qt5.9.1/5.9.1/gcc_64/lib/libQt5Core.so.5.9.1
7eff1a928000-7eff1a92b000 rw-p 00000000 00:00 0
7eff1a92b000-7eff1a951000 r-xp 00000000 08:01 524312 /lib/x86_64-linux-gnu/ld-2.23.so
7eff1ab21000-7eff1ab2b000 rw-p 00000000 00:00 0
7eff1ab4d000-7eff1ab50000 rw-p 00000000 00:00 0
7eff1ab50000-7eff1ab51000 r--p 00025000 08:01 524312 /lib/x86_64-linux-gnu/ld-2.23.so
7eff1ab51000-7eff1ab52000 rw-p 00026000 08:01 524312 /lib/x86_64-linux-gnu/ld-2.23.so
7eff1ab52000-7eff1ab53000 rw-p 00000000 00:00 0
7ffe006ae000-7ffe006cf000 rw-p 00000000 00:00 0 [stack]
7ffe006e2000-7ffe006e4000 r--p 00000000 00:00 0 [vvar]
7ffe006e4000-7ffe006e6000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
The workaround I've figured out was the following:
char encrypted[ textUtf8.size() + 1 ];
However, I confess that I don't know why it worked. Could someone explain it?
Is there any better solution?
The problem is that data in the QByteArray returned by text.toUtf8() is not NUL-terminated, and strcpy() expects a NUL-terminated string... since it doesn't get one, it will happily keep copying extra garbage-bytes past the end of your encrypted array until it finally does encounter a zero-byte somewhere, and corrupt your stack, which leads to the crash when the function returns.
Also, your encrypted array is not large enough to hold the NUL byte that strcpy() wants to place in it.
A fix would be something like this:
char encrypted[ textUtf8.size() + 1 ]; // +1 to hold the NUL terminator byte
memcpy(encrypted, srcString, textUtf8.size() );
encrypted[textUtf8.size()] = '\0'; // place NUL-terminator byte
[...]
Also, a portability note: dynamically-sized arrays aren't part of the C++ standard, so your declaring char encrypted[ textUtf8.size() + 1]; is working for you only because your compiler includes a non-standard extension to enable that. If you want your code to be portable (as all self-respecting Qt code should be ;)), you may want to use a std::vector or other similar higher-level mechanism instead of a dynamically-sized array.
char encrypted[ textUtf8.size() + 1 ];
Because of size we have to use a null character to terminate a string array
For example try this
#include<iostream>
#include<string>
using namespace std;
int main()
{
char textUtf8[5] ="hello";
}
You will see warning "Initializer string for char array is too long"
I'm refering to cppreference sample of std::atomic, trying to add a Dtor function for stack:
#include<atomic>
template<class T>
struct node{
T data;
node* next;
node(const T&data):data(data),next(nullptr){}
};
template<class T>
class stack{
std::atomic<node<T>*> head;
public:
void push(const T&data)
{
node<T>* new_node=new node<T>(data);
new_node->next=head.load(std::memory_order_relaxed);
while(!std::atomic_compare_exchange_weak_explicit(
&head,
&new_node->next,
new_node,
std::memory_order_release,
std::memory_order_relaxed));
}
~stack()
{
node<T>* p=head;
while(p)
{
node<T>* next=p->next;
delete p;
p=next;
}
}
};
int main()
{
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
return 0;
}
When the program executes till ~stack,the last delete function prompts crash, like below:
$ g++ myatomic.cpp -std=c++11
$ ./a.out
*** Error in `./a.out': munmap_chunk(): invalid pointer: 0x0000000000400b00 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x77725)[0x7f07173a2725]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x1a8)[0x7f07173aec18]
./a.out[0x4008f5]
./a.out[0x400829]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f071734b830]
./a.out[0x4006d9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:01 2506641 /home/x/cpp/x01/a.out
00601000-00602000 r--p 00001000 08:01 2506641 /home/x/cpp/x01/a.out
00602000-00603000 rw-p 00002000 08:01 2506641 /home/x/cpp/x01/a.out
022f6000-02328000 rw-p 00000000 00:00 0 [heap]
7f0717022000-7f071712a000 r-xp 00000000 08:01 2102313 /lib/x86_64-linux-gnu/libm-2.23.so
7f071712a000-7f0717329000 ---p 00108000 08:01 2102313 /lib/x86_64-linux-gnu/libm-2.23.so
7f0717329000-7f071732a000 r--p 00107000 08:01 2102313 /lib/x86_64-linux-gnu/libm-2.23.so
7f071732a000-7f071732b000 rw-p 00108000 08:01 2102313 /lib/x86_64-linux-gnu/libm-2.23.so
7f071732b000-7f07174eb000 r-xp 00000000 08:01 2102243 /lib/x86_64-linux-gnu/libc-2.23.so
7f07174eb000-7f07176ea000 ---p 001c0000 08:01 2102243 /lib/x86_64-linux-gnu/libc-2.23.so
7f07176ea000-7f07176ee000 r--p 001bf000 08:01 2102243 /lib/x86_64-linux-gnu/libc-2.23.so
7f07176ee000-7f07176f0000 rw-p 001c3000 08:01 2102243 /lib/x86_64-linux-gnu/libc-2.23.so
7f07176f0000-7f07176f4000 rw-p 00000000 00:00 0
7f07176f4000-7f071770a000 r-xp 00000000 08:01 2102281 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f071770a000-7f0717909000 ---p 00016000 08:01 2102281 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0717909000-7f071790a000 rw-p 00015000 08:01 2102281 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f071790a000-7f0717a7c000 r-xp 00000000 08:01 1312401 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0717a7c000-7f0717c7c000 ---p 00172000 08:01 1312401 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0717c7c000-7f0717c86000 r--p 00172000 08:01 1312401 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0717c86000-7f0717c88000 rw-p 0017c000 08:01 1312401 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f0717c88000-7f0717c8c000 rw-p 00000000 00:00 0
7f0717c8c000-7f0717cb2000 r-xp 00000000 08:01 2102215 /lib/x86_64-linux-gnu/ld-2.23.so
7f0717e8e000-7f0717e93000 rw-p 00000000 00:00 0
7f0717eae000-7f0717eb1000 rw-p 00000000 00:00 0
7f0717eb1000-7f0717eb2000 r--p 00025000 08:01 2102215 /lib/x86_64-linux-gnu/ld-2.23.so
7f0717eb2000-7f0717eb3000 rw-p 00026000 08:01 2102215 /lib/x86_64-linux-gnu/ld-2.23.so
7f0717eb3000-7f0717eb4000 rw-p 00000000 00:00 0
7ffec8e19000-7ffec8e3a000 rw-p 00000000 00:00 0 [stack]
7ffec8edb000-7ffec8edd000 r--p 00000000 00:00 0 [vvar]
7ffec8edd000-7ffec8edf000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Terminated
I tried to gdb it, and the 3rd time of delete crashes. Is there anything wrong in my program?
Thanks.
The head member of stack is never initialized, so your program has undefined behaviour, which just happens to manifest by the pointer being non-null and some garbage memory being deleted.
Fix this by initialising head in the constructor for stack:
stack() : head(nullptr){}
I am having trouble deleting my pointers that I have created. The program creates a double pointer to point to the threads. Then it creates threads dynamically. At the end it deletes them but I am getting a glibc error. It uses boost to create the threads. What is really puzzling is that I delete a similar double pointer the same exact way and that executes fine. The issue is at the end of the code block under the heading of /*clean up*/:
boost :: thread** thrds;
//create threads and bind to p_variantforloop_t
thrds = new boost::thread*[numThreads];
for (int i = 1; i <= numThreads; i++)
thrds[i] = new boost::thread(boost::bind(&p_variantforloop_t,
E, A, D, (i*n-n)/i ,(i*n)/n, numThreads, n));
/* join threads */
for (int i = 0; i < numThreads; i++)
thrds[i]->join();
/* cleanup */
for (int i = 0; i < numThreads; i++)
delete thrds[i];
delete[] thrds;
the error is:
*** glibc detected *** ./hw9: munmap_chunk(): invalid pointer: 0x0957d480 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(+0x6b591)[0x264591]
/lib/tls/i686/cmov/libc.so.6(+0x6c80e)[0x26580e]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0x529741]
./hw9[0x804a0d1]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x20fbd6]
./hw9[0x8049871]
======= Memory map: ========
001f9000-0034c000 r-xp 00000000 08:02 1128663 /lib/tls/i686/cmov/libc-2.11.1.so
0034c000-0034d000 ---p 00153000 08:02 1128663 /lib/tls/i686/cmov/libc-2.11.1.so
0034d000-0034f000 r--p 00153000 08:02 1128663 /lib/tls/i686/cmov/libc-2.11.1.so
0034f000-00350000 rw-p 00155000 08:02 1128663 /lib/tls/i686/cmov/libc-2.11.1.so
00350000-00353000 rw-p 00000000 00:00 0
0046e000-00557000 r-xp 00000000 08:02 982712 /usr/lib/libstdc++.so.6.0.13
00557000-00558000 ---p 000e9000 08:02 982712 /usr/lib/libstdc++.so.6.0.13
00558000-0055c000 r--p 000e9000 08:02 982712 /usr/lib/libstdc++.so.6.0.13
0055c000-0055d000 rw-p 000ed000 08:02 982712 /usr/lib/libstdc++.so.6.0.13
0055d000-00564000 rw-p 00000000 00:00 0
005ad000-005d1000 r-xp 00000000 08:02 1130719 /lib/tls/i686/cmov/libm-2.11.1.so
005d1000-005d2000 r--p 00023000 08:02 1130719 /lib/tls/i686/cmov/libm-2.11.1.so
005d2000-005d3000 rw-p 00024000 08:02 1130719 /lib/tls/i686/cmov/libm-2.11.1.so
00950000-00965000 r-xp 00000000 08:02 1130743 /lib/tls/i686/cmov/libpthread-2.11.1.so
00965000-00966000 r--p 00014000 08:02 1130743 /lib/tls/i686/cmov/libpthread-2.11.1.so
00966000-00967000 rw-p 00015000 08:02 1130743 /lib/tls/i686/cmov/libpthread-2.11.1.so
00967000-00969000 rw-p 00000000 00:00 0
00a67000-00a7a000 r-xp 00000000 08:02 176445 /usr/lib/libboost_thread.so.1.40.0
00a7a000-00a7b000 r--p 00013000 08:02 176445 /usr/lib/libboost_thread.so.1.40.0
00a7b000-00a7c000 rw-p 00014000 08:02 176445 /usr/lib/libboost_thread.so.1.40.0
00bc7000-00be2000 r-xp 00000000 08:02 1128318 /lib/ld-2.11.1.so
00be2000-00be3000 r--p 0001a000 08:02 1128318 /lib/ld-2.11.1.so
00be3000-00be4000 rw-p 0001b000 08:02 1128318 /lib/ld-2.11.1.so
00c34000-00c3b000 r-xp 00000000 08:02 1130745 /lib/tls/i686/cmov/librt-2.11.1.so
00c3b000-00c3c000 r--p 00006000 08:02 1130745 /lib/tls/i686/cmov/librt-2.11.1.so
00c3c000-00c3d000 rw-p 00007000 08:02 1130745 /lib/tls/i686/cmov/librt-2.11.1.so
00ccf000-00cd0000 r-xp 00000000 00:00 0 [vdso]
00e85000-00ea2000 r-xp 00000000 08:02 1128359 /lib/libgcc_s.so.1
00ea2000-00ea3000 r--p 0001c000 08:02 1128359 /lib/libgcc_s.so.1
00ea3000-00ea4000 rw-p 0001d000 08:02 1128359 /lib/libgcc_s.so.1
08048000-0804e000 r-xp 00000000 00:1d 10184 /home/tparisi/Desktop/source_code_hw5/hw9
0804e000-0804f000 r--p 00005000 00:1d 10184 /home/tparisi/Desktop/source_code_hw5/hw9
0804f000-08050000 rw-p 00006000 00:1d 10184 /home/tparisi/Desktop/source_code_hw5/hw9
0957d000-0959e000 rw-p 00000000 00:00 0 [heap]
b68bf000-b68c0000 ---p 00000000 00:00 0
b68c0000-b70c0000 rw-p 00000000 00:00 0
b70c0000-b70c1000 ---p 00000000 00:00 0
b70c1000-b78c4000 rw-p 00000000 00:00 0
b78e8000-b78eb000 rw-p 00000000 00:00 0
bfc07000-bfc1c000 rw-p 00000000 00:00 0 [stack]
Aborted
You omit first element of the array when creating thread object. It stays with uninitialized pointer value, causing the invalid pointer error later.
There is also an issue of writing to memory immediately after the last element of array, which you also do in the problematic loop (i.e. the first for in your code).
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
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.
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 */
}
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.