thread_local vector segmentation fault at end of program in C++ - c++

I'm trying to make a multi-threaded program but am having a weird problem with thread local vectors. Here's the (stripped-down to only have the error) code:
#include <vector>
#include <iostream>
thread_local std::vector<int> vec;
int main(){
vec.push_back(3);
std::cout << vec[0];
// Make you push enter to show the error is at the end
std::cin.ignore();
}
The program compiles fine and runs mostly fine, but after I press enter I get a "program has stopped working" message. I ran it in gdb and got this error:
Program received signal SIGSEGV, Segmentation fault.
0x004030b0 in std::vector<int, std::allocator<int> >::~vector() ()
Somehow, there's an error in the vector's destructor. Going through it with a breakpoint shows that this error happens as the program is quitting.
Making the vector not thread local makes the program work, but of course I need it to be thread local. If I don't interact with the vector at all, the program works fine.
I'm thinking I'll have to use some alternative, but does anybody know a way to make this work? Thanks!
EDIT: I'm stupid and forgot more info on my system. I'm using Windows Vista 64-bit. Running g++ -v gives me:
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=C:/MinGW/bin/../libexec/gcc/i686-w64-mingw32/5.2.0/lto-wrapper.exe
Target: i686-w64-mingw32
Configured with [edited out because it's too big and probably not relevant]
Thread model: posix
gcc version 5.2.0 (i686-posix-dwarf-rev0, Built by MinGW-W64 project)
The thread model might actually have a lot to do with it.

It is setup dependent; I have successfully compiled and run your example program on Windows 7 (64 bit) with the 64 bit MinGW. Perhaps it is related to the fact that you use the 32 bit MinGW installation on 64 bit platform? My g++ -v gives the following (same version and thread model as yours, different arch):
Using built-in specs.
COLLECT_GCC=g++
Target: x86_64-w64-mingw32
Thread model: posix
gcc version 5.2.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)

Related

illegal instruction in boost::gregorian::date::date

I have C++ program that uses boost (Logger mainly). This programs compiles and runs well on Windows and Ubuntu. However, when I try to port it to Linux Yocto on an embedded system (Intel Atom processor), I got illegal instructions error at runtime.
The program itself is built on Ubuntu PC with Intel-i5.
I debugged the issue and it was some AVX instructions from another library (OpenCV). I disabled all AVX and the problem solved but another problem occurred.
It now tells me that (after reading the core dumb using gdb):
Program terminated with signal SIGILL, Illegal instruction.
0x00007fe1aed03ade in boost::gregorian::date::date(boost::gregorian::greg_year,
boost::gregorian::greg_month, boost::gregorian::greg_day) ()
I did not use boost::gregorian::date explicitly
Is it possible that boost::gregorian::date use some optimized insruction?! like SSE or AVX? (seems non-logical)
Any clue about the issue?
P.S. the error occurs at run-time before anything else. Even a cout at the first line of the main function is not executed before I got the error. So, I suspect some static constructor inside boost causes the problem since there is no static constructor at my code.
Edit:
All librires and the program itself are compiled with -march=bonnell -mno-avx -O2

std::thread works in cygwin but not in MinGw

So I decided to give c++ a try today. I downloaded MinGw and the g++ compiler that comes with it. I decided to test the following code:
#include <iostream>
#include <thread>
int foo()
{
std::cout << "foo" << std::endl;
}
int main()
{
std::thread t1(foo);
t1.join();
std::cout << "done" << std::endl;
return 0;
}
I then tried to compile it on the command line using the following line:
g++ -std=c++11 main.cpp
Which works for hello world. This time however, it gave me this error:
error: 'thread' is not a member of 'std'
I tried the exact same code using the g++ provided by cygwin, and it works. So why doesn't it work in MinGw? Is it outdated or something? I want to compile stuff using c++11 and c++14 like on the cygwin terminal, but outside of the cygwin environment.
MinGW-w64 (or rather GCC on windows) needs to be compiled with posix thread support if you want to use std::thread, presumably you downloaded a build with native windows threads.
Check out the mingw-builds folders targeting 64 bit or 32 bit and pick a version with posix threads. You'll also need to choose the exception handling method, if you don't have a reason to choose otherwise then stick with the GCC defaults of seh for 64 bit and dwarf for 32.
I tested this in linux (cross-compiling).
This compiles ok,
i686-w64-mingw32-g++-posix -std=c++11 threads.cpp -lwinpthread
this does not
i686-w64-mingw32-g++-win32 -std=c++11 threads.cpp -lwinpthread
The difference between the compilers is --enable-threads=win32 vs. --enable-threads=posix option used when the compilers were built. g++ -v should show what was used.

OS X get process's memory programmatically

I am trying to get the memory used by another process. From what I've read it seems like I need to use mach_vm_regeion. I found some code on a random forum and tried compiling to make sure I understood how it was working, but I get this error.
error: use of undeclared identifier 'mach_vm_region'
kret = mach_vm_region(task, &address, &size, VM_REGION_BASIC_INFO, (vm_regio...
^
1 error generated.
I am on OS X 10.11.2 compiling using clang++ --std=c++11 file.cpp.
clang --version returns
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.2.0
Thread model: posix
You don't have the correct includes. I find that
#include <mach/mach.h>
#include <mach/mach_vm.h>
works. You also need to change: vm_size_t size to mach_vm_size_t size, which is the type the function expects. Then it compiles and works as expected (when run as root, as the comment suggests).

Jsoncpp - very simple test crashes when Json::reader goes ot of scope

I have downloaded and installed the jsoncpp library. I then try to use the library in my own application:
#include <json/json.h>
void parseJson() {
Json::Reader reader;
}
int main(int argc, char ** argv) {
parseJson();
exit(0);
}
The program compiles and links fine, but it crashes with SIGSEGV when running. The gdb backtrace looks like this:
(gdb) bt
#0 0x0000003a560b7672 in __gnu_cxx::__exchange_and_add () from /usr/lib64/libstdc++.so.6
#1 0x00000000004031e9 in std::string::_Rep::_M_dispose (this=0xffffffffffffffe9, __a=#0x7fffbfe60e57)
at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:232
#2 0x0000000000403236 in ~basic_string (this=0x7fffbfe60fb0)
at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:478
#3 0x00000000004038d4 in ~Reader (this=0x7fffbfe60eb0) at /private/joaho/Parser/opm-parser/external/json/json-cpp/include/json/reader.h:23
#4 0x0000000000402990 in parseJson () at /private/joaho/Parser/opm-parser/opm/parser/eclipse/ExternalTests/ExternalTests.cpp:51
#5 0x00000000004029ab in main (argc=1, argv=0x7fffbfe610c8)
at /home/user/Parser/opm-parser/opm/parser/eclipse/ExternalTests/ExternalTests.cpp:56
I.e. to me it seems to crash in the destructor. As far as I can tell the Json::Reader does not have it's own dstructor, so this must be a default destructor. As you can see I am running a quite old version of g++ - could that be the problem?
As I commented:
When compiled with GCC version 4.8.1 on Debian/Sid (so libjsoncpp-dev 0.6.0~rc2-3) as g++-4.8 -g -Wall -I/usr/include/jsoncpp/ esjson.cc -ljsoncpp -o esjson your program is compiled without warnings, and does not crash when running.
And GCC 4.1.2 is really old (febr. 2007 !) and is not supported anymore, and not very well C++ standard conforming (GCC, now at version 4.8.1, has made huge progress on C++ standard conformance since 4.1).
So I am not sure GCC 4.1. is faulty, but I won't be surprised it is: it had bad C++ reputation, and both the C++ standard and the GCC compiler have been improved a lot since that. Upgrading your GCC is worth the effort, both for better support of C++ and for improved diagnostics and optimizations.
So I suggest you to use a newer GCC; if you don't have root access, consider compiling its from its source tarball; build it outside of the source tree with e.g. ../gcc-4.8.1/configure --program-suffix=-4.8 --prefix=$HOME/pub then make then make install - after having installed its dependencies

MinGW and std::thread

So I've been trying to get the following code to compile and run on Windows by using a MinGW compiler.
#include <iostream>
#include <thread>
void test()
{
std::cout << "test" << std::endl;
}
int main()
{
std::thread t(test);
}
I'm compiling with the following command:
g++ -std=c++11 test.cpp -o test.exe
Now the problem is the version of MinGW one should use and I've tried about all the versions I know of.
MinGW-builds: thread-win32
MinGW-builds: thread-posix
MinGW-w64: stdthread experimental rubenvb
MinGW-w64: stdthread experimental rubenvb 4.7
Number 1 doesn't work, since GCC apparently only supports pthread stuff internally.
Number 2 does compile and it essentially even outputs test (see the last line of the output), but it also crashes with the error:
terminate called without an active exception
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
test
Number 3 and 4 again do compile, but they don't output test and instead instantly crashes, but with a more descriptive output:
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Google brought me of course to the GCC bug tracker and some other posts, that suggested to use -pthread, which doesn't help at all.
I've also tried manually linking against winpthread and pthread, but that doesn't do anything either.
There's also no difference between -std=c++11 and -std=gnu++11...
I'm really lost right now and don't know, if it's at all possible to get a MinGW version, that supports std::thread, but maybe I'm just overlooking some compiler flags. I hope someone out there can help me!
You forgot to join your thread:
t.join();
FYI, there is already a native win32 implementation of std::thread and sync primitives. It is a header-only library and works on any C++11 compliant version of MinGW.
https://github.com/meganz/mingw-std-threads