JStefanelli
JStefanelli

Reputation: 143

OpenCV 4.2.0 FileStorage segmentation fault

[This is a repost of this question on the OpenCV Q&A forums.]

Hi! I'm having a big issue with the FileStorage API with this YML file (pastebin) (and any other YML I throw at OpenCV). Basically, any time I try to access information on any FileNode, the program segfaults.

At first, I though it was due to my custom OpenCV build on macOS, but after trying with the pre-built OpenCV 4.2.0 Windows binaries, the problem persists.

Messing around in LLDB i figured that the crash happens in

cv::FileStorage::Impl::getNodePtr(unsigned long, unsigned long)

The relevant code segment is:

cv::FileStorage fs;
try{
    fs = cv::FileStorage("someFile.yml", cv::FileStorage::READ);
}catch(cv::Exception ex)
{
    return 1;
}

cv::FileNode node = fs.root();

std::cout << "Got root node" << std::endl;
std::cout << "Root node is type: " << node.type() << std::endl; //Crash here

I'm really at a loss... any help is appreciated.

EDIT 1

On the opencv forums, someone found out that initializing fs in the try/catch block caused the issue. Initializing it outside of it, as in cv::FileStorage fs = cv::FileStorage("someFile.yml", cv::FileStorage::READ); solves the problem. I still have no idea why this happens...

Upvotes: 2

Views: 1338

Answers (1)

JStefanelli
JStefanelli

Reputation: 143

As written in the OpenCV Q&A answer, this issue can be circumvented like this:

cv::FileStorage fs;
try{
    fs.open("someFile.yml", cv::FileStorage::READ);
}catch(cv::Exception ex)
{
    return 1;
}

//Do stuff with fs

I'm not into the expert c++ stuff (copy constructors and stuff), but probably my issue had something to do with that...

Upvotes: 1

Related Questions