From 4cbb96b396f9423fdc83cc8a927044de3973898c Mon Sep 17 00:00:00 2001 From: kallaballa Date: Thu, 10 Oct 2024 15:14:58 +0200 Subject: [PATCH] use new instead of malloc and guard it --- modules/core/src/opengl.cpp | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/modules/core/src/opengl.cpp b/modules/core/src/opengl.cpp index 5610b97ce8..22173d9495 100644 --- a/modules/core/src/opengl.cpp +++ b/modules/core/src/opengl.cpp @@ -1681,14 +1681,27 @@ Context& initializeContextFromGL() if(extensionSize > 0) { - char* extensions = (char*)malloc(extensionSize); - status = clGetDeviceInfo(devices[j], CL_DEVICE_EXTENSIONS, extensionSize, extensions, &extensionSize); - if (status != CL_SUCCESS) - continue; + char* extensions = nullptr; - std::string devString(extensions); - free(extensions); + try { + extensions = new char[extensionSize]; + status = clGetDeviceInfo(devices[j], CL_DEVICE_EXTENSIONS, extensionSize, extensions, &extensionSize); + if (status != CL_SUCCESS) + continue; + } catch(std::exception& ex) { + CV_Error(cv::Error::OpenCLInitError, "OpenCL: Exception thrown during device extensions gathering"); + } + + std::string devString; + + if(extensions != nullptr) { + devString = extensions; + delete[] extensions; + } + else { + CV_Error(cv::Error::OpenCLInitError, "OpenCL: Unexpected error during device extensions gathering"); + } size_t oldPos = 0; size_t spacePos = devString.find(' ', oldPos); // extensions string is space delimited @@ -1710,8 +1723,7 @@ Context& initializeContextFromGL() } if (!sharingSupported) - CV_Error_(cv::Error::OpenCLInitError, ("OpenCL: OpenGL sharing not supported: %d", status)); - + continue; // Define OS-specific context properties and create the OpenCL context #if defined (__APPLE__)