BUS error during thread invocation - c++

The following code causes bus error in Rasbian distro on a rasberry pi mod2 system.
#include <thread>
#include <iostream>
class bar {
public:
void startFoo() {
std::thread t(&bar::foo, this); //bus error because of this line
t.join();
}
void foo() {
std::cout << "hello from member function" << std::endl;
}
};
int main()
{
bar b;
b.startFoo();
return 0;
}
This link states that bus error occurs when your processor cannot even attempt the memory access requested. But in my code i am accessing the class own member function in thread. I cannot relate how it is causing bus error. Could someone clarify me? P.S. The source code was cross compiled on a Ubuntu OS running on a x86 PC and the binary was tested on Rasberry pi (ARM).

Related

inline variable is initialized more than once

Im seeing some examples of inline const variable getting initialized (and destructed) 3 times with visual studio 2017. Is this is a bug with the linker ? or is this supposed to happend in some other way ?
linker Comdat folding is set to Off.
Example Code:
#pragma once
struct A {
A() {
static int count = 0;
++count;
ASSERT(count == 1);
}
~A() {
}
};
inline const A a = A();
In my solution, I have the assert fire twice (A constructor called 3 times).
Inspecting the call stack shows all call stacks are identical and all calls come from dynamic initializer for a(). Now I know for a fact this class is not used in other parts of the solution since I just created it to investigate this issue.
Im using VS17 15.8.9
Update: Bug report here https://developercommunity.visualstudio.com/content/problem/297876/static-inline-variable-gets-destroyed-multiple-tim.html (you may upvote to help push for the bugfix)
This appears to be an MSVC bug. I'm able to reproduce it with the code below (also with VS2017 15.8.9). Interestingly, I can only reproduce with a Debug build. In Release mode, the optimizer seems to save us.
Common.h
#pragma once
#include <iostream>
class Foo
{
public:
Foo()
{
std::cout << "Constructing a Foo" << std::endl;
}
~Foo()
{
std::cout << "Destructing a Foo" << std::endl;
}
};
inline Foo const Bar;
other.cpp
#include "common.h"
void DoOtherStuff()
{
std::cout << &Bar << std::endl;
}
main.cpp
#include "common.h"
void DoStuff()
{
std::cout << &Bar << std::endl;
}
extern void DoOtherStuff();
int main()
{
DoStuff();
DoOtherStuff();
}
Output (Debug)
Constructing a Foo
Constructing a Foo
00007FF74FD50170
00007FF74FD50170
Destructing a Foo
Destructing a Foo
I get the bug in both debug and release (/Ox) mode using the MS C++ compiler version 19.16 (comes with, e.g., Visual Studio 15.9.4).
Inline.Hpp
#include <iostream>
inline struct Foo
{ Foo() { std::cout << "Constructing a Foo at " << this << std::endl; } }
Instance;
Inline.cpp
#include "Inline.Hpp"
int main() { return 0; }
Inline2.cpp
#include "Inline.Hpp"
After compiling and linking inline.cpp and inline2.cpp, the output on running is:
Constructing a Foo at 00BE4028
Constructing a Foo at 00BE4028
The compiler and linker correctly resolve the two inline definitions to a single object, but incorrectly call the constructor for each definition, instead of just once. This is a serious bug which renders the "inline variable" feature of C++17 unusable. The "workaround" is to regard inline variables as still unsupported by MS C++ as of version 19.16, even when the /std:c++17 switch is used.
As of today there is an update for visual studio 2017 to version 15.9.24 which fixes the problem.
From the release notes:
Fixed C++ compiler bug for proper folding of inline variable dynamic
initializers.

C++ Build success, but run failed

I am new c++ so forgive me to be asking this question. I created a project and run it the first time, it is successful. But when i start another project and i added 4 classes to it (you can see from the tabs) and the main.cpp is unable to run. I am confused as the codes are exactly the same in both projects.
Run Successful:
Success
Build Successful but run failed:
Run Failed
What are the solutions to solve this problem?
Do i have to post codes of all my classes? (there are 8 files)
student.h:
#ifndef CLSSTUDENT_H
#define CLSSTUDENT_H
#include <string>
#include <iostream>
using namespace std;
class clsStudent {
protected:
string name;
string student_no;
string program;
public:
clsStudent(string n, string sn,string prog );
virtual void displayStudentDetails();
};
student.cpp
#include "TutorialClass.h"
void TutorialClass::addStudent(clsStudent std)
{
_students.push_back(std);
}
int TutorialClass::getStudentCount()
{
return _students.size();
}
void TutorialClass::display()
{
}
#endif /* CLSSTUDENT_H */
I open up a new project and added only this class. It is unable to run as well. What is the problem in the codes?
It seems that your program only fails to run when it's compiled with other files. My bet is that in these files you've got buggy code that is running before main() gets to run.
This can happen in cases like this:
int f() {
throw; // bam! Uncaught exception;
}
int x = f(); // this runs before main()
Or this:
class C {
C() {
cout << "This runs before main() too!" << endl;
}
};
C my_c; // calls constructor
In both cases: code was executed before main(). You want this because you want your global variables to be initialized before running main(). If this initialization code manages to crash the program via a segfault or an exit() call or throwing some exception which isn't caught? You've got a crashed program before it ever even gets the chance to run.

C++ Visual Studio 13 using threads: minimal example not working

i got a minimal example from http://www.cplusplus.com/reference/thread/thread/
for the implementation of threads.
Unfortunately this example did not compile and ERROR C2664 is thrown:
// thread example
#include <iostream> // std::cout
#include <thread> // std::thread
void foo()
{
// do stuff...
}
void bar(int x)
{
// do stuff...
}
int main()
{
std::thread first (foo); // spawn new thread that calls foo()
std::thread second (bar,0); // spawn new thread that calls bar(0)
std::cout << "main, foo and bar now execute concurrently...\n";
// synchronize threads:
first.join(); // pauses until first finishes
second.join(); // pauses until second finishes
std::cout << "foo and bar completed.\n";
return 0;
}
error C2664: 'std::thread::thread(const std::thread &)' :
cannot convert argument 1 from 'void' to 'std::thread &&'
Can someone explain what ist wrong with the example // with Visual Studio?
Thanks
The above code works in VS 2013 (but also in VS 2012, VS 2015).
Try to create a new project and copy the code there. VS behaves strange sometimes. Also removing the .sdf file may help together with "build -> clean solution".

cudaErrorIllegalAdress in Profiler

I have a CUDA program that uses thrust in some places but also normal kernels.
The problem is: When I run the program standalone, everything works fine. When I run it in the profiler (Visual profiler or nvprof cmd line) the program crashes in a thrust::inclusive_scan operation with a cudaErrorIllegalAdress error. The crash happens always in the profiler and always at the same position. Furthermore, I have multiple Iterations like:
void foo(){ cudaProfilerStart();
for(...){//...
thrust::inclusive_scan(...);//...
}
cudaProfilerStop();
}
for(...) foo();
The crash always happens on the first call to inclusive_scan in the 2nd iteration.
I'm cusing CUDA 6.5 on Win7 with a Quadro K5000.
Any ideas what can cause this or how to narrow it down? Maybe a way to get the adress of the failed access? cuda-memcheck cannot be used with nvprof AFAIK(?)
If I remove the calls to cudaProfilerStart/Stop it seems to work ok. Strange enough, it DID work today morning with them although I did not introduce any changes (did some code editing but reverted everything via git) Also the behaviour does not change if I disable/enable profile-from-start (with cudaProfilerStart/Stop in place)
Minimal working example:
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <thrust/device_vector.h>
#include <cuda_profiler_api.h>
void foo(){
thrust::device_vector<int> d_in(100), d_out(100);
thrust::inclusive_scan(d_in.begin(), d_in.end(), d_out.begin());
cudaError_t res = cudaDeviceSynchronize();
std::cout << cudaGetErrorString(res) << std::endl;
}
int main(){
cudaProfilerStart();
foo();
cudaProfilerStop();
foo(); // Crash here
cudaDeviceReset();
return 0;
}
Some more scenarios:
Start(); foo(); Stop(); foo() crash
Start(); foo(); Stop(); Start(); foo() OK
Start(); foo(); Stop(); any_other_kernel(); Start(); foo() crash
This behaviour appears to be due to a limitation in the CUDA 7.0 and earlier profiler system. A fix will be available in the CUDA 7.5 release toolkit.
[This answer has been assembled from comments and added as a community wiki entry to get the question off the unanswered queue]

Catching exception from a specialized method

Let's consider the following three files.
tclass.h:
#include <iostream>
#include <vector>
template<typename rt>
class tclass
{
public:
void wrapper()
{
//Storage is empty
for(auto it:storage)
{
}
try
{
thrower();
}
catch(...)
{
std::cout << "Catch in wrapper\n";
}
}
private:
void thrower(){}
std::vector<int> storage;
};
spec.cpp:
#include "tclass.h"
//The exact type does not matter here, we just need to call the specialized method.
template<>
void tclass<long double>::thrower()
{
//Again, the exception may have any type.
throw (double)2;
}
main.cpp:
#include "tclass.h"
#include <iostream>
int main()
{
tclass<long double> foo;
try
{
foo.wrapper();
}
catch(...)
{
std::cerr << "Catch in main\n";
return 4;
}
return 0;
}
I use Linux x64, gcc 4.7.2, the files are compiled with this command:
g++ --std=c++11 *.cpp
First test: if we run the program above, it says:
terminate called after throwing an instance of 'double'
Aborted
Second test: if we comment for(auto it:storage) in the tclass.h file, the program will catch the exception in main function. WATWhy? Is it a stack corruption caused by an attempt to iterate over the empty vector?
Third test: lets uncomment back the for(auto it:storage) line and move the method specialization from spec.cpp to main.cpp. Then the exception is caught in wrapper. How is it possible and why does possible memory corruption not affect this case?
I also tried to compile it with different optimization levels and with -g, but results were the same.
Then I tried it on Windows 7 x64, VS2012 express, compiling with x64 version of cl.exe with no extra command line arguments. At the first test this program produced no output, so I think it just crashed silently, so the result is similar with Linux version. For the second test it produced no output again, so result is different from Linux. For the third test the result was similar with Linux result.
Are there any errors in this code so they can lead to such behavior? May the results of the first test be caused by possible bug in compilers?
With your code, I have with gcc 4.7.1:
spec.cpp:6: multiple definition of 'tclass<long double>::thrower()'
You may correct your code by declaring the specialization in your .h as:
template<> void tclass<long double>::thrower();