• Windows DLL issue using global definition in depthAI

Hello, I have a serious issue when I use a following function "test_show()" as DLL C++. Could you help me? A simple and DLL source is below.

#include "opencv2/opencv.hpp"
#include "depthai/depthai.hpp"
dai::Pipeline pipeline;
dai::Device device;
std::shared_ptrdai::node::ColorCamera colorCam;
std::shared_ptrdai::node::XLinkOut xlinkOut;
std::shared_ptrdai::DataOutputQueue preview

void __declspec(dllexport) test_show() {
  colorCam = pipeline.create<dai::node::ColorCamera>();
  xlinkOut = pipeline.create<dai::node::XLinkOut>();
  xlinkOut->setStreamName("preview");
  colorCam->setInterleaved(true);
  colorCam->preview.link(xlinkOut->input);
  device.startPipeline(pipeline);
  preview = device.getOutputQueue("preview");
  for (int i = 0; i < 400; ++i){
     auto inRgb = preview->get<dai::ImgFrame>();
     cv::imshow("rgb", inRgb->getCvFrame())
  }
  cv::destroyAllWindows();
  device.close();
}

Above simple source defines device, pipeline .. as global variables. When I build this dll and calls test_show() from other C++ source, The DepthAI camera doesn't work at all. The same applies when declaring a global pointer for a variable and instantiating it inside a function. On the other hands, if there is no global variables (device, pipeline … are defined within test_show()), the software works well.

My development environment is as follows.

windows visual studio 2022,depthai-core-2.21.2 opencv_world460

When I call this dll function, the source is simple.

void __declspec(dllimport) test_show();

int main(){ test_show(); }

I hope your quick response for us and our customer.

Best Regards,

    Hi JunSato
    Can you try refractoring your code to include everything inside the test_show() function?

    #include "opencv2/opencv.hpp"
    #include "depthai/depthai.hpp"
    
    void __declspec(dllexport) test_show() {
        dai::Pipeline pipeline;
        dai::Device device;
        std::shared_ptr<dai::node::ColorCamera> colorCam;
        std::shared_ptr<dai::node::XLinkOut> xlinkOut;
        std::shared_ptr<dai::DataOutputQueue> preview;
    
        pipeline = dai::Pipeline();
        colorCam = pipeline.create<dai::node::ColorCamera>();
        xlinkOut = pipeline.create<dai::node::XLinkOut>();
        xlinkOut->setStreamName("preview");
        colorCam->setInterleaved(true);
        colorCam->preview.link(xlinkOut->input);
        device = dai::Device(pipeline);
        device.startPipeline();
        preview = device.getOutputQueue("preview");
    
        for (int i = 0; i < 400; ++i){
            auto inRgb = preview->get<dai::ImgFrame>();
            cv::imshow("rgb", inRgb->getCvFrame());
            cv::waitKey(1);
        }
    
        cv::destroyAllWindows();
        device.close();
    }

    Let me know if there is a difference.

    Regards,
    Jaka

      jakaskerl

      Hi Jaka,

      There is no difference compared with my source. To clarify our issue, I wrote a simple source and tested itself. At first glance, you should be able to tell if this source looks strange. Unfortunately, I have failed to upload my source file. If you are acceptable, I can send my source to your address. May I send my-emails to support team?

      Our issue should be resolved since this makes difficult to provide DLL to our customer. I think that this global issue happens in DLL. When I use global variables locally, there is no problem.


      • erik replied to this.

        JunSato I believe it would be best to create an issue on depthai-core github repo.