cygwin_exception::open_stackdumpfile: Dumping stack trace to *.exe.stackdump - c++

I am getting "cygwin_exception::open_stackdumpfile: Dumping stack trace to TestProject.exe.stackdump" error. My project is nothing but a C++ HalloWorld project that contains an additional class in which I set and get a variable. I am getting this error at the line I try to set a matrix variable of type Eigen. Here is my code:
TestProject.cpp
#include <iostream>
#include "TestClass.hpp"
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
TestClass testClass;
Eigen::MatrixXd XX = testClass.getVariable();
cout << "X = " << XX;
return 0;
}
TestClass.hpp:
#ifndef TESTCLASS_HPP_
#define TESTCLASS_HPP_
#include <Eigen/Core>
#include <Eigen/Eigenvalues>
#include <unsupported/Eigen/MatrixFunctions>
#include <Eigen/Geometry>
class TestClass {
private:
Eigen::MatrixXd X;
public:
TestClass();
void setVariable(Eigen::MatrixXd);
Eigen::MatrixXd getVariable();
virtual ~TestClass();
};
#endif /* TESTCLASS_HPP_ */
and finally the TestClass.cpp:
#include "TestClass.hpp"
using namespace std;
TestClass::TestClass() {
X << 0, 1, 2;
}
TestClass::~TestClass() {
// TODO Auto-generated destructor stub
}
void TestClass::setVariable(Eigen::MatrixXd x){
X = x;
}
/* namespace std */
Eigen::MatrixXd TestClass::getVariable(){
return X;
}
The output I get in the Console is:
!!!Hello World!!!
0 [main] TestProject 8416 cygwin_exception::open_stackdumpfile: Dumping stack trace to TestProject.exe.stackdump
It is worth mentioning that when I change the type of the class variable X (and all related types in the methods and the header file) into an integer I don't get this error and the code compiles and runs.
I would appreciate any help since I didn't find useful info online.
Thanks

You are using a dynamic sized Matrix X, and you try to comma initialize it without setting its size first. This will raise an exception:
As explained here:
Eigen offers a comma initializer syntax which allows the user to
easily set all the coefficients of a matrix, vector or array. Simply
list the coefficients, starting at the top-left corner and moving from
left to right and from the top to the bottom. The size of the object
needs to be specified beforehand.
and here:
The coefficients must be provided in a row major order and exactly
match the size of the matrix. Otherwise an assertion is raised.
So resize your matrix first:
TestClass::TestClass() {
X.resize (1,3);
X << 0, 1, 2;
}

Related

MEX-file implementing Eigen library pseudo-inverse function crashes

I am trying to implement an Eigen library pseudo-inverse function in a Matlab MEX-file. It compiles successfully but crashes when I run it.
I am trying to follow the FAQ on how to implement a pseudo-inverse function using the Eigen library.
The FAQ suggests adding it as a method to the JacobiSVD class but since you can't do that in C++ I'm adding it to a child class. It compiles successfully but then crashes without an error message. It successfully outputs "hi" without crashing if I comment out the line with the .pinv call so that's where the problem is arising. To run, I just compile it (as test.cpp) and then type test at the command line. I am using Matlab R2019a under MacOS 10.14.5 and Eigen 3.3.7. In my full code I also get lots of weird error messages regarding the pinv code but before I can troubleshoot I need this simple test case to work. This is all at the far limits of my understanding of C++. Any help appreciated.
#include "mex.h"
#include <Eigen/Dense>
#include <Eigen/SVD>
#include <cstdlib>
#include <cmath>
#include <iostream>
using namespace Eigen;
using namespace std;
//https://stackoverflow.com/questions/18804402/add-a-method-to-existing-c-class-in-other-file
class JacobiSVDext : public JacobiSVD<MatrixXf> {
typedef SVDBase<JacobiSVD<MatrixXf>> Base;
public:
using JacobiSVD::JacobiSVD; //inherit constructors //https://stackoverflow.com/questions/347358/inheriting-constructors
MatrixXf pinv() //http://eigen.tuxfamily.org/index.php?title=FAQ
{
eigen_assert(m_isInitialized && "SVD is not initialized.");
double pinvtoler=1.e-6; // choose your tolerance wisely!
JacobiSVDext::SingularValuesType singularValues_inv=m_singularValues;
for ( long i=0; i<m_workMatrix.cols(); ++i) {
if ( m_singularValues(i) > pinvtoler )
singularValues_inv(i)=1.0/m_singularValues(i);
else singularValues_inv(i)=0;
}
return m_matrixV*singularValues_inv.asDiagonal()*m_matrixU.transpose();
};
};
/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
MatrixXf X = MatrixXf::Random(5, 5);
JacobiSVDext svd(X);
MatrixXf Y=svd.pinv();
cout << Y << endl;
cout << "hi" << endl;
}
The expected result is to output the pseudo-inverse of the random matrix and also "hi". Instead it crashes without an error message.
When constructing the Eigen::JacobiSVD object, you fail to request that matrices U and V should be computed. By default, these are not computed. Obviously, accessing these matrices if they are not computed will cause a segmentation violation.
See the documentation to the constructor. A second input argument must specify either ComputeFullU | ComputeFullV, or ComputeThinU | ComputeThinV. The thin ones are preferable when computing the pseudo-inverse, as the rest of the matrices are not needed.
I would not derive from the JacobiSVD class just to add a method. Instead, I would simply write a free function. This is both easier, and allows you to use only the documented portions of the Eigen API.
I wrote the following MEX-file, which works as intended (using code I already had for this computation). It does the same, but in a slightly different way that avoids writing an explicit loop. Not sure this way of writing it is very clear, but it works.
// Compile with:
// mex -v test.cpp -I/usr/local/include/eigen3
#include "mex.h"
#include <Eigen/Dense>
#include <Eigen/SVD>
#include <cstdlib>
#include <cmath>
#include <iostream>
Eigen::MatrixXf PseudoInverse(Eigen::MatrixXf matrix) {
Eigen::JacobiSVD< Eigen::MatrixXf > svd( matrix, Eigen::ComputeThinU | Eigen::ComputeThinV );
float tolerance = 1.0e-6f * float(std::max(matrix.rows(), matrix.cols())) * svd.singularValues().array().abs()(0);
return svd.matrixV()
* (svd.singularValues().array().abs() > tolerance).select(svd.singularValues().array().inverse(), 0).matrix().asDiagonal()
* svd.matrixU().adjoint();
}
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
Eigen::MatrixXf X = Eigen::MatrixXf::Random(5, 5);
Eigen::MatrixXf Y = PseudoInverse(X);
std::cout << Y << '\n';
std::cout << "hi\n";
}

How to create static member variable with Eigen types library

I am trying to implement a class that has some static member from Eigen library types, nevertheless I am getting the following compiler error
kalman.cpp:3:28: error: expected initializer before ‘<<’ token
Eigen::Matrix2d Kalman::AA << 1,2,3,4;
^
which I do not know how to solve it.
Here, in the documentation library there is the section Comma-initialization which describes the initialition format chosen. Of course,
in a simple main source code like this
#include <iostream>
#include "Eigen/Dense"
int main()
{
Eigen::Matrix2d m;
m << 1,2,3,4;
std::cout << m << std::endl;
}
every works as expected. But, when I try to do this with an static member variables of type Eigen::Matrix2d as follows
header
#ifndef KALMAN_H
#define KALMAN_H
#include <iostream>
#include "Eigen/Dense"
class Kalman{
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
static Eigen::Matrix2d AA;
};
#endif /* KALMAN_H */
source
#include "kalman.hpp"
Eigen::Matrix2d Kalman::AA << 1,2,3,4;
I got the mentioned error. I think that this could be related of some sort of how to create static member objects, but to be sincere I do not know. Any tips or recommend content it will be really appreciate.
Thanks
The << is not equivalent to an assignment operator, it writes values into an existing matrix. You can do what you want with this:
Eigen::Matrix2d Kalman::AA = (Eigen::Matrix2d() << 1,2,3,4).finished();

Why is it that when I use an int from a separate class, it gives me random numbers? [duplicate]

This question already has answers here:
Why do I see strange values when I print uninitialized variables?
(6 answers)
Closed 7 years ago.
I'm relatively new to coding,I know the basics of Java and C++ so far, but I am trying to organize my cod a bit more and speed up compile time a little. So far I've only figured out how to use functions from other classes. I'm self-learning everything so if anyone could show me an example using the code I've posted, that would be perfect. Feel free to explain it like you're talking to a child because these Header files are VERY confusing to me.
test.h:
#pragma once
class test
{
public:
//functions
test();
void part2();
//variables
int varA;
};
test.cpp
#include "stdafx.h"
#include <iostream>
using namespace std;
#include "test.h"
int varA = 10;
test::test()
{
cout << "this is the test main function" << endl;
}
void test::part2(){
cout << "you got to part 2, also, Variable A is " << varA << " when it's in test.cpp" << endl;
}
ConsoleApplication1
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
#include "test.h"
int main()
{
test objectA;
objectA.part2();
cout << "variable A is " << objectA.varA << "when it's in CA1.cpp" << endl;
system("pause");
return 0;
}
here is the console's output
this is the test main funtion
you got to part 2, also, Variable A is -858993460 when it's in CA1.cpp
variable A is -858993460 in CA1.cpp
Press any key to continue . . .
EDIT: So can I only get the varA that I'm declaring in the header in the test::test constructor? I tested this way, and it worked, it did return 10. however, if i want to use the same variable in the header/class, in a different function apart from the constructor, it gives me the gibberish again. The only way I can seem to use it, is if I create a global variable named something else, and use that. this wouldn't be practical if I needed to refer to it later like in an 'if-statement'.
What I'm trying to do is, make varA in test.cpp, and refer/use it inside of ConsoleApplication1.cpp.
The varA you define on top of test.cpp is different than the one in the test class. It is also never used, because inside the various member functions, the member varA has priority.
If you want to initialize test::varA to 10, do it in the constructor:
test::test()
: varA(10)
{
cout << "this is the test main class" << endl;
}
You can get rid of the global varA in test.cpp completely.
This line:
int varA = 10;
creates a new variable named varA which is independent of its homonym in class test instances.
Primitive types, such as your int varA in the definition of class test are not initialized by default, hence the -858993460 value you obtain. You can set an initial value in the constructor of test:
test::test()
: varA(10)
{}
Or inside the class declaration in C++11:
class test
{
// …
int varA = 10;
}

Eclipse c++11 // vector

This is really driving me crazy:
#include <iostream>
#include <vector>
#include <string.h>
#include <thread>
using namespace std;
void test() {
vector<string> myvector;
string a("Teststring");
myvector.push_back(a);
cout << myvector.begin()->length() << endl;
}
int main() {
thread(test).join();
return 0;
}
The code compiles fine with the -std=c++11 flag to the compiler and the -pthread flag to the linker.
BUT: Eclipse does either know the std::thread or the myvector.begin()->length(), even if the code runs fine eclipse warns me "Method 'length' could not be resolved".
I tried every possible solution in here: Eclipse CDT C++11/C++0x support without any success. This took me so many hours now, what am I doing wrong?!
Is there anybody getting a project setup without problems with this code?
EDIT: Other code example - same problem:
#include <iostream>
#include <vector>
#include <thread>
using namespace std;
class TestClass {
public:
void test() {
cout << "test" << endl;
}
};
void test() {
vector<TestClass> testClassVector;
TestClass x;
testClassVector.push_back(x);
testClassVector.begin()->test();
}
int main() {
thread(test).join();
return 0;
}
Compiles and runs correct, but returns in eclipse: Method 'test' could not be resolved
EDIT:
working versions:
((TestClass)*(testClassVector.begin())).test();
TestClass foo2 = *(testClassVector.begin());
foo2.test();
still not working:
testClassVector.begin()->test();
The last compiles and works like the two above, but eclipse still claims:
Method 'test' could not be resolved
Maybe I'm wrong, but I think your problem don't come from Eclypse. Juste, begin() on a vector return a std::vector<T>::iterator first, this is not a pointer and there is no method length, but you can ask for the vector size with myvector.size(); if this is what you want.
The problem could come from your #include <string.h> that is not the same as #include <string>, string.h is for string operation like strcmp, strstr, etc... juste string will define the std::string object.
I don't have Eclipse set up but the problem appears to be around std::string. Does the problem go away if you remove the threading from the example? (I also changed to #include <string> instead of string.h)
#include <iostream>
#include <vector>
#include <string>
#include <thread>
using namespace std;
#if 0
void test() {
vector<string> myvector;
string a("Teststring");
myvector.push_back(a);
cout << myvector.begin()->length() << endl;
}
#endif
int main() {
//thread(test).join();
vector<string> myvector;
string a("Teststring");
myvector.push_back(a);
cout << myvector.begin()->length() << endl;
return 0;
}
That should hopefully print out 10.
Update from comment:
Does this generate the Eclipse warning?
auto tmp = *(myvector.begin());
std::cout << tmp.length() << std::endl;
What about this?
std::string foo("abc123");
std::cout << foo.length() << std::endl;
I guess one more too:
std::string foo2 = *(myvector.begin());
std::cout << foo2.length() << std::endl;
The solution found:
I downloaded eclipse kepler Kepler
Created a new project and tried to compile this source code (like above):
#include <iostream>
#include <vector>
#include <thread>
using namespace std;
class TestClass {
public:
void test() {
cout << "test" << endl;
}
};
void test() {
vector<TestClass> testClassVector;
TestClass x;
testClassVector.push_back(x);
testClassVector.begin()->test();
}
int main() {
thread(test).join();
return 0;
}
On the first run eclipse told me, thread belongs to the new c++11 standard and I have to add -std=c++11 to the compiler flags. To use thread I also added -pthread to the linker flags. With this steps the code could be compiled, but eclipse marks the thread still as unknown. To fix this I proceeded the following step:
Under C/C++ Build (at project settings), find the Preprocessor Include Path and go to the Providers Tab. Deselect all except CDT GCC Builtin Compiler Settings. Then untag Share settings entries … . Add the option -std=c++11 to the text box called Command to get compiler specs.
Found here.
Now - unbelievable but true - it works, even without any errors marked by eclipse. The solution is using the (beta) version of eclipse, wich seems to handle this in a better way.
Thanks for all your help!

Insert into multimap causes segfault

I am working on a project using multimaps inside of my own class, and I have run into a segfault. Here are the parts of my code relating to the issue. I would really appreciate some help. Thanks.
Here is database.h
#include <iostream>
#include <map>
using namespace std;
class database{
public:
database(); // start up the database
int update(string,int); // update it
bool is_word(string); //advises if the word is a word
double prox_mean(string); // finds the average prox
private:
multimap<string,int> *data; // must be pointer
protected:
};
Here is the database.cpp
#include <iostream>
#include <string>
#include <map>
#include <utility>
#include "database.h"
using namespace std;
// start with the constructor
database::database()
{
data = new multimap<string,int>; // allocates new space for the database
}
int database::update(string word,int prox)
{
// add another instance of the word to the database
cout << "test1"<<endl;
data->insert( pair<string,int>(word,prox));
cout << "test2" <<endl;
// need to be able to tell if it is a word
bool isWord = database::is_word(word);
// find the average proximity
double ave = database::prox_mean(word);
// tells the gui to updata
// gui::update(word,ave,isWord); // not finished yet
return 0;
}
Here is test.cpp
#include <iostream>
#include <string>
#include <map>
#include "database.h" //this is my file
using namespace std;
int main()
{
// first test the constructor
database * data;
data->update("trail",3);
data->update("mix",2);
data->update("nut",7);
data->update("and",8);
data->update("trail",8);
data->update("and",3);
data->update("candy",8);
// cout<< (int) data->size()<<endl;
return 0;
}
Thanks very much. It compiles and runs up to cout << "test1" << endl; but segfaults on the next line.
Rusty
You never actually created a database object, just a pointer to nowhere (perhaps you're used to another language).
Try creating one like this database data;
And then change your -> to . to access the members.
Consider obtaining one of the books at The Definitive C++ Book Guide and List as well.
You need to allocate your database before starting to insert data on it.
Change:
database *data;
to:
database *data = new database();
or:
database data;
in main().
EDIT: if you use the latter, change -> to . on the subsequent method calls. Otherwise, remember to delete your data object after using it.