'Strange Interaction between OpenCV DNN and RapidJSON methods

I am working on a project that uses RapidJSON to read a config file, then initialize an OpenCV DNN net with the config values.

//Detector.h

class Detector {

    private:
        std::vector <DetectionNet*> nets;
        std::string outputDir;
        std::string debugDir;
        bool debug = false;
        
    public:
        Detector(Config*, std::string);
        Detector(const Detector&);
        ~Detector();
};

######################################################################
// Detector.cpp
Detector::Detector(Config* projConfig, std::string outputDir)
{
    
    for (rapidjson::Value::ConstMemberIterator itr = projConfig->modelsObject.MemberBegin();
        itr != projConfig->modelsObject.MemberEnd(); ++itr)
    {
          //sets some config values
          DetectionNet* net = new DetectionNet(configFile, weightsFile, architecture, windowSizes, confThres, nmsThres, useCuda);
          nets.push_back(net);
    }
}

The actual DNN neural net is initialized inside the DetectionNet class.

// DetectionNet.h

class DetectionNet {

    public:
        DetectionNet(std::string, std::string, std::string, std::vector<int>, double, double, bool);
        DetectionNet(const DetectionNet &);
        ~DetectionNet();
        void PrintConfig();

    private:
        cv::dnn::Net net;
        std::string modelFile = "";
        std::string configFile = "'";
        std::string architecture = "";
        bool calculateWinsize = false;
        std::vector <int> windowSizes;
        double confThres = 0;
        double nmsThres = 0;
        std::string backend = "";
};

##############################################################
//DetectionNet.cpp

DetectionNet::DetectionNet(std::string configPath, std::string modelPath, std::string _architecture, 
                            std::vector<int> _windowSizes, double _confThres, double _nmsThres, bool useCuda) 
{
    
    // the readNet line causes the problem.
    net = cv::dnn::readNetFromTensorflow(modelPath, configPath);
    net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
    net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);

}

Inside Detector.cpp, the model configs are iterated with a rapidjson::Values object. The actual OpenCV network is initialized in another class (DetectionNet).

In the first iteration everything runs smoothly. However, in the second iteration, RapidJSON exhibits odd behavior. The iterator's key and values have their datatype changed, so function calls like itr->value.GetObject or itr->Value.GetType() would just freeze the program.

If I go to DetectionNet and comment out net.ReadTensorflow(), then the iterator functions in Detector.cpp run smoothly. If I move net.ReadTensorflow() from DetectionNet.cpp to Detector.cpp the iterator runs just fine as well.

Is there a reason behind the strange interaction between rapidJSON and DetectionNet.cpp? Could it be something related to dangling OpenCV pointers that messes up rapidJSON?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source