glfwInit() causes access violation - glfw

#include <glfw3.h>
int main()
{
glfwInit();
return 0;
}
When I try to run glfwInit() it throws an access violation and says "Frame not in module". 1

This error is shown because the error is produced in a non debuggable source code.
The most probable cause is that DLL was not loaded.
Try to add GLFW bin path to one of the PATH environment variable, or copy DLL to executable path .
Try calling glfwGetVersion to check DLL is loaded before call glfwInit.
int major;
int minor;
int rev;
glfwGetVersion(&major, &minor, &rev) ;
cout << major << "." << minor << "." << rev<< endl;

Related

fstream fails to write/open files on raspberry pi

I am trying to run a cpp program on raspberry pi 3 b+ (from 'pi' user) but when I try to open a file with 'fstream' library it doesn't work.
I am using the following code (from main):
std::ios::sync_with_stdio(false);
std::string path = "/NbData";
std::ofstream nbData(path);
if (!nbData) {
std::cout << "Error during process...";
return 0;
}
nbData.seekp(std::ios::beg);
The program always fails there and stops because no file is created (I don't get a fatal error but the test fails and it outputs 'Error during process' which means no file was created).
I am compiling with the following command (there are no issues when I compile):
g++ -std=c++0x nbFinder.cpp -o nbFinder
I have already tried my program on Xcode and everything worked perfectly...
The problem is your path. You must put the file, you are using just the path and if the path do not exist will throw an error. In your case you just using std::string path = "/NbData";, that is you path not your file.
To be able to open your file you need make sure your path exist. Try use the code bellow, he will check if the path exist case not will create and then try to open your file.
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
int main() {
std::ios::sync_with_stdio(false);
std::string path = "./test_dir/";
std::string file = "test.txt";
// Will check if thie file exist, if not will creat
struct stat info;
if (stat(path.c_str(), &info) != 0) {
std::cout << "cannot access " << path << std::endl;
system(("mkdir " + path).c_str());
} else if(info.st_mode & S_IFDIR) {
std::cout << "is a directory" << path << std::endl;
} else {
std::cout << "is no directory" << path << std::endl;
system(("mkdir " + path).c_str());
}
std::ofstream nbData(path + file);
if (!nbData) {
std::cout << "Error during process...";
return 0;
}
nbData.seekp(std::ios::beg);
return 0;
}

Getting LNK1107 invalid or corrupt file: cannot read at 0x378 when trying to link .dll for OpenSceneGraph tutorial

I'm trying to get OpenSceneGraph set up on visual studio so I can run through some tutorials and I believe my issue is that I do not know how to correctly set up the environment on visual studios and get the program to look at the library files correctly.
The code in question is just for a osg smart pointer demonstration
#include <osg/ref_ptr>
#include <osg/Referenced>
#include <iostream>
using namespace std;
class AClass : public osg::Referenced
{
public:
AClass(int id) : _id(id)
{
cout << "Constructing object " << _id << endl;
}
protected:
int _id;
virtual ~AClass()
{
cout << "Destroy " << _id << endl;
}
};
int main()
{
osg::ref_ptr<AClass> obj = new AClass(0);
cout << "Reference count before referring: "
<< obj->referenceCount() << endl;
osg::ref_ptr<AClass> anotherObject = object;
cout << "Referenced count after referring: "
<< object->referenceCount() << endl;
}
If I point to osgd.lib in Properties->Linker->additional dependencies this will build but when I try to run it a system error occurs whereby it states the program cannot start because "osgd.ll is missing from your computer", however if I point to osgd.dll it will fail to build and throw up the following error: "LNK1107 invalid or corrupt file: cannot read at 0x378 OSG1 C:\Users\Monkone\source\OpenSceneGraph-3.6.3-VC2017-64-Debug\bin\osgd.dll"
What am I doing wrong here?
You need to link against the .lib, not the .dll. The dll path must be in your PATH to work or in the same folder as the executable.

Function pointer obtained from GetProcAddress crashes the program if it uses the stdlib

I'm trying to dynamically load a dll and call a function from it at runtime. I have succeeded in getting a working pointer with GetProcAddress, but the program crashes if the function from the dll uses the stdlib. Here's the code from the executable that loads the dll:
#include <iostream>
#include <windows.h>
typedef int (*myFunc_t)(int);
int main(void) {
using namespace std;
HINSTANCE dll = LoadLibrary("demo.dll");
if (!dll) {
cerr << "Could not load dll 'demo.dll'" << endl;
return 1;
}
myFunc_t myFunc = (myFunc_t) GetProcAddress(dll, "myFunc");
if (!myFunc) {
FreeLibrary(dll);
cerr << "Could not find function 'myFunc'" << endl;
return 1;
}
cout << "Successfully loaded myFunc!" << endl;
cout << myFunc(3) << endl;
cout << myFunc(7) << endl;
cout << myFunc(42) << endl;
cout << "Successfully called myFunc!" << endl;
FreeLibrary(dll);
return 0;
}
Here's code for the dll that actually works:
#include <iostream>
extern "C" {
__declspec(dllexport) int myFunc(int demo) {
//std::cout << "myFunc(" << demo << ")" << std::endl;
return demo * demo;
}
}
int main(void) {
return 0;
}
(Note that the main method in the dll code is just to appease the compiler)
If I uncomment the line with std::cout however, then the program crashes after the cout << "Sucessfully loaded myFunc!" << endl; line but before anything else gets printed. I know there must be some way to do what I want; what do I need to change for it to work?
As discussed in the comments, it turns out that the compiler's demands for a main function were hints that I was inadvertently making a an exe that decptively used the file extension dll, not an actual dll (because I didn't quite understand the compiler options I was using), which in some way messed up the dynamic loading of that assembly.

PCL library basic project returns abort has been called

I have succesfully linked my basic project containing only one ReadPc.cpp file obviously reading point cloud from file in Visual Studio 2010
After running exception is thrown and pop up window "R6010 abort() has been called" is shown
When i was setting up my application i followed this tutorial
Here is my code
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
int
main (int argc, char** argv)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
std::cout << "bejzikl";
if (pcl::io::loadPCDFile<pcl::PointXYZ> ("kitchen.pcd", *cloud) == -1) //* load the file
{
PCL_ERROR ("Couldn't read file test_pcd.pcd \n");
return (-1);
}
std::cout << "Loaded "
<< cloud->width * cloud->height
<< " data points from test_pcd.pcd with the following fields: "
<< std::endl;
for (size_t i = 0; i < cloud->points.size (); ++i)
std::cout << " " << cloud->points[i].x
<< " " << cloud->points[i].y
<< " " << cloud->points[i].z << std::endl;
return (0);
}
Problem line seems to be this one
if (pcl::io::loadPCDFile ("kitchen.pcd", *cloud) == -1)
In debug mode Output says
First-chance exception at 0x7566812f in Meshes.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0030f36c..
Unhandled exception at 0x7566812f in Meshes.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0030f36c..
The program '[15424] Meshes.exe: Native' has exited with code -529697949 (0xe06d7363).
I appreciate any advice
In my case, this is due to difference in the configuration of dependencies and the configuration of the project. Solved by changing the configuration to Release mode as my project dependencies are in release mode.
I don't have experience with PCL library but std::bad_alloc, means that new failed.
"Type of the exceptions thrown by the standard definitions of operator new and operator new[] when they fail to allocate the requested storage space."
source: cplusplus
Maybe the file you are loading is too big, or if that load is requesting continues memory you don't have it available.
Also you can try to catch exception and see what ex.what() will suggest about exception cause.
If you are using the "kitchen.pcd" from the tutorials of the PCL site or github, then you should know that the points of this file is of type .
you can open the ".pcd" file in wordpad and make sure of this.
If so, change the types "PointXYZ" to "PointXYZRGB".
Hope it would be helpful!

C++ - stat(), access() not working under gnu gcc

I've got a pretty basic console program here, to determine if a folder or file exists or not using stat:
#include <iostream>
#include <sys/stat.h>
using namespace std;
int main() {
char path[] = "myfolder/";
struct stat status;
if(stat(path,&status)==0) { cout << "Folder found." << endl; }
else { cout << "Can't find folder." << endl; } //Doesn't exist
cin.get();
return 0;
}
I have also tried the access version:
#include <iostream>
#include <io.h>
using namespace std;
int main() {
char path[] = "myfolder/";
if(access(path,0)==0) { cout << "Folder found." << endl; }
else { cout << "Can't find folder." << endl; } //Doesn't exist
cin.get();
return 0;
}
Neither of them find my folder (which is right there in the same directory as the program). These worked on my last compiler (the default one with DevCpp). I switched to CodeBlocks and am compiling with Gnu GCC now, if that helps. I'm sure it's a quick fix - can someone help out?
(Obviously I'm a noob at this so if you need any other information I've left out please let me know).
UPDATE
The problem was with the base directory. The updated, working program is as follows:
#include <iostream>
#include <sys/stat.h>
using namespace std;
int main() {
cout << "Current directory: " << system("cd") << endl;
char path[] = "./bin/Release/myfolder";
struct stat status;
if(stat(path,&status)==0) { cout << "Directory found." << endl; }
else { cout << "Can't find directory." << endl; } //Doesn't exist
cin.get();
return 0;
}
ANOTHER UPDATE
Turns out that a trailing backslash on the path is big trouble.
Right before your stat call, insert the code:
system("pwd"); // for UNIXy systems
system("cd"); // for Windowsy systems
(or equivalent) to check your current directory. I think you'll find it's not what you think.
Alternatively, run the executable from the command line where you know what directory you're in. IDEs will frequently run your executable from a directory you may not expect.
Or, use the full path name so that it doesn't matter which directory you're in.
For what it's worth, your first code segment works perfectly (gcc under Ubuntu 10):
pax$ ls my*
ls: cannot access my*: No such file or directory
pax$ ./qq
Cannot find folder.
pax$ mkdir myfolder
pax$ ll -d my*
drwxr-xr-x 2 pax pax 4096 2010-12-14 09:33 myfolder/
pax$ ./qq
Folder found.
Are you sure that the current directory of your running program is what you expect it to be? Try changing path to an absolute pathname to see if that helps.
Check your PWD when you running your program. This problem is not caused by compiler. You DevCpp may set a working directory for your program automatically.
You can find out why stat() failed (which is a C function, not C++, by the way), by checking errno:
#include <cerrno>
...
if (stat(path,&status) != 0)
{
std::cout << "stat() failed:" << std::strerror(errno) << endl;
}