Hi there,
i wonder how you guys keep your code AND your memory clean at the same time. since i am working myself through a OpenCL tutorial i found that there is a lot of initialization to be done. each step might end up exiting the main function. what to do with all the stuff allocated before? i tried to add up all necessary cleaning commands within each catch of an error, but somehow that makes the code look a bit bloated.
as an example i'll list a part of my code:
Code:
// retreive OpenCL device CPU or GPU
if (clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, NULL) != CL_SUCCESS) {
printf("unable to get device_id\n");
return 1;
}
// create context
// intenionally set context properties NULL, to keep sample code small
context = clCreateContext(NULL, 1, &device_id, NULL, NULL, NULL);
if (err != CL_SUCCESS) {
printf("unable to create context. ERROR CODE: %i\n", err);
return 1;
}
// create command queue
command_queue = clCreateCommandQueue(context, device_id, 0, &err);
if (err != CL_SUCCESS) {
printf("unable to create command queue\n");
// we should start cleaning up here, since we created a context before..
clReleaseContext(context);
return 1;
}
// create program object
program = clCreateProgramWithSource(context, 1, &ProgramSource, NULL, &err);
if (err != CL_SUCCESS) {
printf("unable to create program\n");
// cleaning up what we already have
clReleaseCommandQueue(command_queue);
clReleaseContext(context);
return 1;
}
as you can see, the further i get, the more clRelease... stuff will add up. so at some point it will look like this (which i find is very much):
Code:
// set kernel arguments
if (clSetKernelArg(kernel, 0, sizeof(cl_mem), &data_buffer_1) ||
clSetKernelArg(kernel, 1, sizeof(cl_mem), &data_buffer_2) != CL_SUCCESS) {
printf("unable to set kernel args\n");
// cleaning up what we already have
clReleaseMemObject(data_buffer_1);
clReleaseMemObject(data_buffer_2);
clReleaseKernel(kernel);
clReleaseProgram(program);
clReleaseCommandQueue(command_queue);
clReleaseContext(context);
return 1;
}
while reading the tutorial i found, that the sample code just cleans up at the end of the main function if everything goes well, but i think that will leave some garbage at the memory in case some error occurs (right?).
thanks in advance for your contribution
kind regards
christian