---
name: "clSetMemObjectDestructorCallback"
section: "3clc"
platform: "linux"
package: "opencl-1.2-man-doc"
version: "1.0~svn33624-5.1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/clSetMemObjectDestructorCallback"
---

# clSetMemObjectDestructorCallback(3clc)

Registers a user callback function with a memory object.

## NAME

clSetMemObjectDestructorCallback - Registers a user callback function with a memory object.

##

**cl\_int clSetMemObjectDestructorCallback(cl\_mem** *memobj***, void (CL\_CALLBACK \****pfn\_notify***) (cl\_mem *memobj*, void \****user\_data***), void \****user\_data***);**

## PARAMETERS

*memobj*

A valid memory object.

*pfn\_notify*

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

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

• *user\_data*: a pointer to user supplied data.

*user\_data*

Data which will be passed as the *user\_data* argument when *pfn\_notify* is called. *user\_data* can be NULL.

## NOTES

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 object's 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.

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**(3clc)](/clFinish/3clc.md).RE

• [**clWaitForEvents**(3clc)](/clWaitForEvents/3clc.md).RE

•

blocking calls to [**clEnqueueReadBuffer**(3clc)](/clEnqueueReadBuffer/3clc.md), [**clEnqueueReadBufferRect**(3clc)](/clEnqueueReadBufferRect/3clc.md), [**clEnqueueWriteBuffer**(3clc)](/clEnqueueWriteBuffer/3clc.md), [**clEnqueueWriteBufferRect**(3clc)](/clEnqueueWriteBufferRect/3clc.md).RE

•

blocking calls to [**clEnqueueReadImage**(3clc)](/clEnqueueReadImage/3clc.md) and [**clEnqueueWriteImage**(3clc)](/clEnqueueWriteImage/3clc.md).RE

•

blocking calls to [**clEnqueueMapBuffer**(3clc)](/clEnqueueMapBuffer/3clc.md) and [**clEnqueueMapImage**(3clc)](/clEnqueueMapImage/3clc.md).RE

•

blocking calls to [**clBuildProgram**(3clc)](/clBuildProgram/3clc.md), [**clCompileProgram**(3clc)](/clCompileProgram/3clc.md), or [**clLinkProgram**(3clc)](/clLinkProgram/3clc.md).RE

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**(3clc)](/clFlush/3clc.md) on the queue before returning or arrange for [**clFlush**(3clc)](/clFlush/3clc.md) 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.

## ERRORS

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.

## SPECIFICATION

**OpenCL Specification**\[1\]

## SEE ALSO

[**clCreateCommandQueue**(3clc)](/clCreateCommandQueue/3clc.md), [**clGetCommandQueueInfo**(3clc)](/clGetCommandQueueInfo/3clc.md), [**clReleaseCommandQueue**(3clc)](/clReleaseCommandQueue/3clc.md), [**clRetainCommandQueue**(3clc)](/clRetainCommandQueue/3clc.md)

## AUTHORS

**The Khronos Group**

## COPYRIGHT

Copyright © 2007-2011 The Khronos Group Inc.\
 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and/or associated documentation files (the "Materials"), to deal in the Materials without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials, and to permit persons to whom the Materials are furnished to do so, subject to the condition that this copyright notice and permission notice shall be included in all copies or substantial portions of the Materials.

## NOTES

- 1\.

  OpenCL Specification

  page 119, section 5.4.1 - Retaining and Releasing Memory Objects
