Hello everyone,
I am pretty new to DepthAI and c++ dll. I am trying to build a application to pass the oak camera's image frame to C# environment. I am using P/Invoke and c++ DLL. The loadRGBImg function can be successfully built and called by C# while comes into the error:
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Repeat 2 times:
--------------------------------
at Program.loadRGBImg(IntPtr ByRef, Int32 ByRef, Int32 ByRef, Int32 ByRef)
--------------------------------
at Program.Main(System.String[])
Here's the function code in cpp
bool loadRGBImg(unsigned char** imageData, int* width, int* height, int* channels) {
// Create pipeline
dai::Pipeline pipeline;
auto colorCam = pipeline.create<dai::node::ColorCamera>();
auto xlinkOut = pipeline.create<dai::node::XLinkOut>();
xlinkOut->setStreamName("preview");
colorCam->setInterleaved(true);
colorCam->preview.link(xlinkOut->input);
std::cout << "pipeline built" << std::endl;
try {
dai::Device device(pipeline);
// Get output queue
auto preview = device.getOutputQueue("preview");
// Receive 'preview' frame from device
auto imgFrame = preview->get<dai::ImgFrame>();
// Show the received 'preview' frame
cv::Mat frame(imgFrame->getHeight(), imgFrame->getWidth(), CV_8UC3, imgFrame->getData().data());
std::cout << "frame getted" << std::endl;
*width = frame.cols;
*height = frame.rows;
*channels = frame.channels();
int dataSize = frame.total() * frame.elemSize();
*imageData = (unsigned char*)CoTaskMemAlloc(dataSize);
if (*imageData == nullptr) {
std::cout << "Memory allocation failed" << std::endl;
return false;
}
memcpy(*imageData, frame.data, dataSize);
std::cout << "Image data copied successfully" << std::endl;
return true;
}
catch (const std::runtime_error& err) {
std::cout << err.what() << std::endl;
return false;
}
}
It supposed to be some memory relative issues based on my understanding, if I cannot do it in this way, is there any other way to achieve this ingestion? Thanks for the support in advanced!