Check whether cl_mem object is valid

Hi all,

what I am trying to do seems rather simple to me, but at the moment I have the feeling it is not possible with OpenCL:

I want to check whether some cl_mem-object is actually valid before releasing it:


cl_mem someimage;
if ( isvalid(someimage) ) clReleaseMemObject(someobject);

The reason is simple: If I call clReleaseMemObject without initializing “someimage”, my program crashes. I could put a “try {…} catch {…}” around the call, but this seems somehow dirty to me.

Any call like “clGetImageInfo(someimage)” also crashes, if it is not initialized.

How could I implement “isvalid(someimage)”?

Many thanks for any hints, Jakob

Querying properties on invalid mem objects should return safely with the CL_INVALID_MEM_OBJECT error. Your program crashing indicates a possible bug in the OpenCL implementation you are using.

Thank you. My impression somehow was, that this should be the desired behavior - but I could not believe that there is such a rather dirty bug in the nVidia OpenCL implementation. I use a nVidia GTS 250 card with the developer driver version 270.81. I just realized that there is a newer driver release - I will check if this helps.

cl_mem is a pointer type.

Normal C conventions are to initialise pointers to NULL (which is always just 0), which will then be the same 0 result as from a failed allocation and treated the same way.

Then just test it before releasing it:

if (someimage) clReleaseMemObject(someimage);

If ‘someimage’ is a value on the stack, it will be initialised with completely random data. Checking the validity of pointers is too expensive to add to every call, so it’s not surprising using a completely invalid pointer causes a crash.

I was not sure whether an initialization with “NULL” was okay for the cl_mem object.

In case cl_mem is a pointer type, this is of course the clean solution.

Thanks a lot!

Even though initialization to NULL is a way to work around this, the implementation should still not segfault on encountering an invalid cl_mem object. I suggest you do report it to the manufacturer, although considering it’s nvidia I don’t know how much they’ll care about it.