Search code examples
openclrelease

How to release a cl::Context object [c++ API] - cannot convert ‘cl::Context’ to ‘cl_context’


I am using an OpenCL code, and I want to release several OpenCL objects. To be more exact I want to release the following objects:

extern cl::Context context;                         
extern cl::CommandQueue q;                           
extern cl::Program program;                          
extern cl::Kernel kernel_k;              
extern cl::Event kernel_e;
extern vector<cl::Event> r_events;  
extern cl::Buffer *buffer_a;                    

However, when I run:

  clReleaseCommandQueue(q);
  clReleaseContext(context); 

I obtain:

error: cannot convert ‘cl::CommandQueue’ to ‘cl_command_queue’ {aka ‘_cl_command_queue*’}
   clReleaseCommandQueue(q);
error: cannot convert ‘cl::Context’ to ‘cl_context’ {aka ‘_cl_context*’}
   clReleaseContext(context);

I assume that the functions that I am using to realise the OpenCL objects are from OpenCL C API, and the objects that I have created are from C++ API. How can I realise the OpenCL object successfully with C++ API?


Solution

  • Use the brackets() operator to get the C-bindings pointers from the C++ bindings objects:

    clReleaseCommandQueue(q());
    
    clReleaseContext(context());
    

    Note that the C++ objects are automatically released once they go out of scope.