/* cl::CommandQueue::getInfo() doesn't increase the reference count of the cl::Context object returned by this method. This results in a program crash when the destructur is called for the unreferenced instance. The problem probably exists for other getInfo<...> variants since the code is shared. This file demonstrates the problem. The program crashes in ReferenceHandler::release(), which is invoked from cl::Context::~Context(). If the context's reference count is increased explicitly (by setting "#if 1" in line 47), the program finishes without error. */ #include #define __CL_ENABLE_EXCEPTIONS #include using namespace std; int test_opencl_bug() { try { // get list of platforms and devices: vector platforms; vector devices; cl::Platform::get(&platforms); platforms[0].getDevices(CL_DEVICE_TYPE_ALL, &devices); // create a context: cl::Context context(devices); // create a command queue: cl::CommandQueue queue(context, devices[0], 0); // print current context reference count: cout << "ref count = " << context.getInfo() << endl; // access context associated with command queue: cl::Context context2(queue.getInfo()); #if 0 // If the context's reference count is increased explicitly at this point, // the program finishes without error: ::clRetainContext(context2()); #endif cout << "ref count = " << context.getInfo() << endl; } catch(cl::Error err) { cerr << "ERROR: " << err.what() << "(" << err.err() << ")" << endl; } return EXIT_SUCCESS; }