Using std::atomic to implement a lockfree structure, Dtor crashes - c++

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){}

Related

Why am I getting an invalid pointer error when using MySQL C++ Connector tutorial code?

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.

add pointer to structure array to std::map

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;
};

Sudden Error in '': free(): invalid pointer

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"

C++ Boost program_options crash

I'm using boost program options for my project but I can't get program exit without crashing. It crashes after exiting main function.
*** glibc detected *** bin/poolserver: double free or corruption (fasttop): 0x0000000002562100 ***
======= Backtrace: =========
/lib/libc.so.6(+0x71e16)[0x7f64a7a3be16]
/lib/libc.so.6(cfree+0x6c)[0x7f64a7a40b8c]
/usr/lib/libstdc++.so.6(_ZNSsD1Ev+0x39)[0x7f64a826cee9]
/lib/libc.so.6(__cxa_finalize+0xa5)[0x7f64a7a00995]
/usr/local/lib/libboost_program_options.so.1.53.0(+0x2a6d6)[0x7f64a85026d6]
======= Memory map: ========
00400000-004a5000 r-xp 00000000 08:01 29687876 /var/coins/poolserver/cmake/build/bin/poolserver
006a4000-006a6000 rw-p 000a4000 08:01 29687876 /var/coins/poolserver/cmake/build/bin/poolserver
02562000-025a5000 rw-p 00000000 00:00 0 [heap]
7f64a0000000-7f64a0021000 rw-p 00000000 00:00 0
7f64a0021000-7f64a4000000 ---p 00000000 00:00 0
7f64a5b2e000-7f64a5b2f000 ---p 00000000 00:00 0
7f64a5b2f000-7f64a632f000 rw-p 00000000 00:00 0
7f64a632f000-7f64a633b000 r-xp 00000000 08:01 9166850 /lib/libnss_files-2.11.3.so
7f64a633b000-7f64a653a000 ---p 0000c000 08:01 9166850 /lib/libnss_files-2.11.3.so
7f64a653a000-7f64a653b000 r--p 0000b000 08:01 9166850 /lib/libnss_files-2.11.3.so
7f64a653b000-7f64a653c000 rw-p 0000c000 08:01 9166850 /lib/libnss_files-2.11.3.so
7f64a653c000-7f64a653d000 ---p 00000000 00:00 0
7f64a653d000-7f64a6d3d000 rw-p 00000000 00:00 0
7f64a6d3d000-7f64a6d44000 r-xp 00000000 08:01 9166856 /lib/librt-2.11.3.so
7f64a6d44000-7f64a6f43000 ---p 00007000 08:01 9166856 /lib/librt-2.11.3.so
7f64a6f43000-7f64a6f44000 r--p 00006000 08:01 9166856 /lib/librt-2.11.3.so
7f64a6f44000-7f64a6f45000 rw-p 00007000 08:01 9166856 /lib/librt-2.11.3.so
7f64a6f45000-7f64a6f5c000 r-xp 00000000 08:01 22677552 /usr/lib/libz.so.1.2.3.4
7f64a6f5c000-7f64a715b000 ---p 00017000 08:01 22677552 /usr/lib/libz.so.1.2.3.4
7f64a715b000-7f64a715c000 rw-p 00016000 08:01 22677552 /usr/lib/libz.so.1.2.3.4
7f64a715c000-7f64a7171000 r-xp 00000000 08:01 9166863 /lib/libnsl-2.11.3.so
7f64a7171000-7f64a7370000 ---p 00015000 08:01 9166863 /lib/libnsl-2.11.3.so
7f64a7370000-7f64a7371000 r--p 00014000 08:01 9166863 /lib/libnsl-2.11.3.so
7f64a7371000-7f64a7372000 rw-p 00015000 08:01 9166863 /lib/libnsl-2.11.3.so
7f64a7372000-7f64a7374000 rw-p 00000000 00:00 0
7f64a7374000-7f64a737c000 r-xp 00000000 08:01 9166865 /lib/libcrypt-2.11.3.so
7f64a737c000-7f64a757b000 ---p 00008000 08:01 9166865 /lib/libcrypt-2.11.3.so
7f64a757b000-7f64a757c000 r--p 00007000 08:01 9166865 /lib/libcrypt-2.11.3.so
7f64a757c000-7f64a757d000 rw-p 00008000 08:01 9166865 /lib/libcrypt-2.11.3.so
7f64a757d000-7f64a75ab000 rw-p 00000000 00:00 0
7f64a75ab000-7f64a75ae000 r-xp 00000000 08:01 12394545 /usr/local/lib/libboost_system.so.1.53.0
7f64a75ae000-7f64a77ad000 ---p 00003000 08:01 12394545 /usr/local/lib/libboost_system.so.1.53.0
7f64a77ad000-7f64a77ae000 rw-p 00002000 08:01 12394545 /usr/local/lib/libboost_system.so.1.53.0
7f64a77ae000-7f64a77c5000 r-xp 00000000 08:01 9166854 /lib/libpthread-2.11.3.so
7f64a77c5000-7f64a79c4000 ---p 00017000 08:01 9166854 /lib/libpthread-2.11.3.so
7f64a79c4000-7f64a79c5000 r--p 00016000 08:01 9166854 /lib/libpthread-2.11.3.so
7f64a79c5000-7f64a79c6000 rw-p 00017000 08:01 9166854 /lib/libpthread-2.11.3.so
7f64a79c6000-7f64a79ca000 rw-p 00000000 00:00 0
7f64a79ca000-7f64a7b23000 r-xp 00000000 08:01 9166859 /lib/libc-2.11.3.so
7f64a7b23000-7f64a7d22000 ---p 00159000 08:01 9166859 /lib/libc-2.11.3.so
7f64a7d22000-7f64a7d26000 r--p 00158000 08:01 9166859 /lib/libc-2.11.3.so
7f64a7d26000-7f64a7d27000 rw-p 0015c000 08:01 9166859 /lib/libc-2.11.3.so
7f64a7d27000-7f64a7d2c000 rw-p 00000000 00:00 0
7f64a7d2c000-7f64a7d42000 r-xp 00000000 08:01 9166851 /lib/libgcc_s.so.1
7f64a7d42000-7f64a7f41000 ---p 00016000 08:01 9166851 /lib/libgcc_s.so.1
7f64a7f41000-7f64a7f42000 rw-p 00015000 08:01 9166851 /lib/libgcc_s.so.1
7f64a7f42000-7f64a7fc2000 r-xp 00000000 08:01 9166872 /lib/libm-2.11.3.so
7f64a7fc2000-7f64a81c2000 ---p 00080000 08:01 9166872 /lib/libm-2.11.3.so
7f64a81c2000-7f64a81c3000 r--p 00080000 08:01 9166872 /lib/libm-2.11.3.so
7f64a81c3000-7f64a81c4000 rw-p 00081000 08:01 9166872 /lib/libm-2.11.3.so
7f64a81c4000-7f64a82ba000 r-xp 00000000 08:01 22677469 /usr/lib/libstdc++.so.6.0.13
7f64a82ba000-7f64a84ba000 ---p 000f6000 08:01 22677469 /usr/lib/libstdc++.so.6.0.13
7f64a84ba000-7f64a84c1000 r--p 000f6000 08:01 22677469 /usr/lib/libstdc++.so.6.0.13
7f64a84c1000-7f64a84c3000 rw-p 000fd000 08:01 22677469 /usr/lib/libstdc++.so.6.0.13
7f64a84c3000-7f64a84d8000 rw-p 00000000 00:00 0
7f64a84d8000-7f64a853c000 r-xp 00000000 08:01 12394575 /usr/local/lib/libboost_program_options.so.1.53.0
7f64a853c000-7f64a873b000 ---p 00064000 08:01 12394575 /usr/local/lib/libboost_program_options.so.1.53.0
7f64a873b000-7f64a8740000 rw-p 00063000 08:01 12394575 /usr/local/lib/libboost_program_options.so.1.53.0
7f64a8740000-7f64a8757000 r-xp 00000000 08:01 12394559 /usr/local/lib/libboost_thread.so.1.53.0
7f64a8757000-7f64a8956000 ---p 00017000 08:01 12394559 /usr/local/lib/libboost_thread.so.1.53.0
7f64a8956000-7f64a8958000 rw-p 00016000 08:01 12394559 /usr/local/lib/libboost_thread.so.1.53.0
7f64a8958000-7f64a8969000 r-xp 00000000 08:01 22682328 /usr/lib/libboost_date_time.so.1.42.0
7f64a8969000-7f64a8b68000 ---p 00011000 08:01 22682328 /usr/lib/libboost_date_time.so.1.42.0
7f64a8b68000-7f64a8b6a000 rw-p 00010000 08:01 22682328 /usr/lib/libboost_date_time.so.1.42.0Aborted
I'm using cmake with such options:
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(Boost_ALL_DYN_LINK ON)
I tried static linking and it worked for some time but now it crashes with any settings.
OS: Debian Squeeze
If it helps https://github.com/Intel/poolserver this is my source
Your question doesn't give us the code directly, but looking through github, I suspect the boost program options have nothing to do with it.
The error says
double free or corruption
I suspect it's this:
void DatabaseConnectionMySQL::Close()
{
delete this;
}
Problem was with boost package from debian. Compiling newest boost from source fixed crash.

Deletion of Pointers Issue

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