title: SEGFAULT calling DLL functions
body: <p>I'm currently trying to edit a project, that already uses OpenCL.dll, to make it load the library dynamically. I want to be able to use it in no OpenCL systems, with only a error mesage and a feature disabled.</p>
  
  <p>First, I added some wrappers to the functions. (This code in inside a C++ class, and is public)</p>
  
  <pre><code>    typedef cl_int h_clGetPlatformIDs(cl_uint, cl_platform_id *, cl_uint *);
      typedef cl_int h_clGetDeviceIDs(cl_platform_id, cl_device_type, cl_uint, cl_device_id *, cl_uint *);
      typedef cl_context h_clCreateContext(cl_context_properties *, cl_uint, const cl_device_id *, void *(const char *, const void *, size_t, void *), void *, cl_int *);
      typedef cl_command_queue h_clCreateCommandQueue(cl_context, cl_device_id, cl_command_queue_properties, cl_int *);
      typedef cl_program h_clCreateProgramWithSource(cl_context, cl_uint, const char **, const size_t *, cl_int *);
      typedef cl_int (CALLBACK h_clBuildProgram)(cl_program, cl_uint,const cl_device_id *, const char *, void (*)(cl_program, void * ), void * ) CL_API_SUFFIX__VERSION_1_0;
      typedef cl_int h_clGetProgramBuildInfo(cl_program, cl_device_id, cl_program_build_info, size_t, void  *, size_t  *);
      typedef cl_kernel h_clCreateKernel(cl_program, const char *, cl_int *);
      typedef cl_mem h_clCreateBuffer(cl_context, cl_mem_flags, size_t, void *, cl_int *);
      typedef cl_int h_clEnqueueWriteBuffer(cl_command_queue, cl_mem, cl_bool, size_t, size_t, const void *, cl_uint, const cl_event *, cl_event *);
      typedef cl_int h_clSetKernelArg(cl_kernel, cl_uint, size_t, const void *);
      typedef cl_int h_clEnqueueNDRangeKernel(cl_command_queue, cl_kernel, cl_uint, const size_t*, const size_t*, const size_t*, cl_uint, const cl_event*, cl_event*);
      typedef cl_int h_clFlush(cl_command_queue);
      typedef cl_int h_clEnqueueReadBuffer(cl_command_queue, cl_mem, cl_bool, size_t, size_t, void *, cl_uint, const cl_event *, cl_event *);
      typedef cl_int h_clWaitForEvents(cl_uint, const cl_event *);
      typedef cl_int h_clReleaseMemObject(cl_mem );
      typedef cl_int h_clReleaseEvent(cl_event );
      typedef cl_int h_clReleaseProgram(cl_program );
      typedef cl_int h_clReleaseKernel(cl_kernel);
      typedef cl_int h_clReleaseCommandQueue(cl_command_queue );
      typedef cl_int h_clReleaseContext(cl_context );
  
      h_clGetPlatformIDs* clGetPlatformIDs;
      h_clGetDeviceIDs* clGetDeviceIDs;
      h_clCreateContext* clCreateContext;
      h_clCreateCommandQueue* clCreateCommandQueue;
      h_clCreateProgramWithSource* clCreateProgramWithSource;
      h_clBuildProgram* clBuildProgram;
      h_clGetProgramBuildInfo* clGetProgramBuildInfo;
      h_clCreateKernel* clCreateKernel;
      h_clCreateBuffer* clCreateBuffer;
      h_clEnqueueWriteBuffer* clEnqueueWriteBuffer;
      h_clSetKernelArg* clSetKernelArg;
      h_clEnqueueNDRangeKernel* clEnqueueNDRangeKernel;
      h_clFlush* clFlush;
      h_clEnqueueReadBuffer* clEnqueueReadBuffer;
      h_clWaitForEvents* clWaitForEvents;
      h_clReleaseMemObject* clReleaseMemObject;
      h_clReleaseEvent* clReleaseEvent;
      h_clReleaseProgram* clReleaseProgram;
      h_clReleaseKernel* clReleaseKernel;
      h_clReleaseCommandQueue* clReleaseCommandQueue;
      h_clReleaseContext* clReleaseContext;
  </code></pre>
  
  <p>With that, I'm able to directly assign the handlers what GetProcAdress returns, and after that just call the function (of course, I load the DLL first).</p>
  
  <pre><code>    clReleaseContext = (h_clReleaseContext*) GetProcAddress(ocl_lib_handle, "clReleaseContext");
  </code></pre>
  
  <p>And a common call example:</p>
  
  <pre><code>clReleaseContext((cl_context)context);
  </code></pre>
  
  <p>But I always get SEGFAULT calling this one:</p>
  
  <pre><code>clBuildProgram(program, 0, NULL, "-cl-fast-relaxed-math", NULL, NULL);
  </code></pre>
  
  <p>It's strange, since all the other ones work OK.
  I post here the clBuildProgram Args as reference:</p>
  
  <pre><code>    extern CL_API_ENTRY cl_int CL_API_CALL
  clBuildProgram(cl_program           /* program */,
                 cl_uint              /* num_devices */,
                 const cl_device_id * /* device_list */,
                 const char *         /* options */, 
                 void (*pfn_notify)(cl_program /* program */, void * /* user_data */),
                 void *               /* user_data */) CL_API_SUFFIX__VERSION_1_0;
  </code></pre>
  
  <p>Thanks!</p>