I'm not sure if this is a bug in the OpenCL C++ bindinsg or in the Apple implementation I'm using it on. Currently, this code sometimes crashes (race condition) at program shutdown:

Code :
#include <vector>
 
#define __CL_ENABLE_EXCEPTIONS
#include "cl.hpp"
 
using namespace std;
 
int main(int, char *[])
{
  cl::Context context(CL_DEVICE_TYPE_ALL);
 
  vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
 
  cl::CommandQueue queue(context, devices[0], 0);
 
  printf("Context reference count = %d\n", context.getInfo<CL_CONTEXT_REFERENCE_COUNT>());
 
  cl::Context context_copy(context);
 
  printf("Context reference count = %d\n", context.getInfo<CL_CONTEXT_REFERENCE_COUNT>());
 
  cl::Context ccontext = queue.getInfo<CL_QUEUE_CONTEXT>(); 
 
  printf("Context reference count = %d\n", context.getInfo<CL_CONTEXT_REFERENCE_COUNT>());
 
  return 0;
}

I've tracked the problem down to the fact that clReleaseContext is being called one too many times. The output of the program is as follows:
Code :
Context reference count = 2
Context reference count = 3
Context reference count = 3

That is, the copy construction of a cl::Context is properly calling clRetainContext to increment the reference count, but the queue.getInfo<CL_QUEUE_CONTEXT>() call is not. This causes an extra clReleaseContext call to occur, which crashes the Apple implementation sometimes (which it shouldn't, but that's a separate issue).

So should a clGet*Info call that returns another cl_* type automatically increment that reference. I haven't found anything in the standard stating one way or the other.