C Specification

To register a user callback function with a memory object, call the function

cl_int clSetMemObjectDestructorCallback(
    cl_mem memobj,
    void (CL_CALLBACK* pfn_notify)(cl_mem memobj, void* user_data),
    void* user_data);


  • memobj is a valid memory object.

  • pfn_notify is the callback function that can be registered by the application. This callback function may be called asynchronously by the OpenCL implementation. It is the applications responsibility to ensure that the callback function is thread-safe. The parameters to this callback function are:

    • memobj is the memory object being deleted. When the user callback is called by the implementation, this memory object is not longer valid. memobj is only provided for reference purposes.

    • user_data is a pointer to user supplied data.

  • user_data will be passed as the user_data argument when pfn_notify is called. user_data can be NULL.


Each call to clSetMemObjectDestructorCallback registers the specified user callback function on a callback stack associated with memobj. The registered user callback functions are called in the reverse order in which they were registered. The user callback functions are called and then the memory objects resources are freed and the memory object is deleted. This provides a mechanism for the application (and libraries) using memobj to be notified when the memory referenced by host_ptr, specified when the memory object is created and used as the storage bits for the memory object, can be reused or freed.

clSetMemObjectDestructorCallback returns CL_​SUCCESS if the function is executed successfully. Otherwise, it returns one of the following errors:

  • CL_​INVALID_​MEM_​OBJECT if memobj is not a valid memory object.

  • CL_​INVALID_​VALUE if pfn_notify is NULL.

  • CL_​OUT_​OF_​RESOURCES if there is a failure to allocate resources required by the OpenCL implementation on the device.

  • CL_​OUT_​OF_​HOST_​MEMORY if there is a failure to allocate resources required by the OpenCL implementation on the host.

When the user callback function is called by the implementation, the contents of the memory region pointed to by host_ptr (if the memory object is created with CL_​MEM_​USE_​HOST_​PTR) are undefined. The callback function is typically used by the application to either free or reuse the memory region pointed to by host_ptr.

The behavior of calling expensive system routines, OpenCL API calls to create contexts or command-queues, or blocking OpenCL operations from the following list below, in a callback is undefined.

  • clFinish,

  • clWaitForEvents,

  • blocking calls to clEnqueueReadBuffer, clEnqueueReadBufferRect, clEnqueueWriteBuffer, clEnqueueWriteBufferRect,

  • blocking calls to clEnqueueReadImage and clEnqueueWriteImage,

  • blocking calls to clEnqueueMapBuffer, clEnqueueMapImage,

  • blocking calls to clBuildProgram, clCompileProgram or clLinkProgram

If an application needs to wait for completion of a routine from the above list in a callback, please use the non-blocking form of the function, and assign a completion callback to it to do the remainder of your work. Note that when a callback (or other code) enqueues commands to a command-queue, the commands are not required to begin execution until the queue is flushed. In standard usage, blocking enqueue calls serve this role by implicitly flushing the queue. Since blocking calls are not permitted in callbacks, those callbacks that enqueue commands on a command queue should either call clFlush on the queue before returning or arrange for clFlush to be called later on another thread.

The user callback function may not call OpenCL APIs with the memory object for which the callback function is invoked and for such cases the behavior of OpenCL APIs is considered to be undefined.

See Also

Document Notes

For more information, see the OpenCL Specification

This page is extracted from the OpenCL Specification. Fixes and changes should be made to the Specification, not directly.

Copyright (c) 2014-2020 Khronos Group. This work is licensed under a Creative Commons Attribution 4.0 International License.