oclGetDev return a pointer to freed memory
In oclUtils.pas, at line 272, the return value of the oclGetDev function get the address of a structure in an array. On line 274 the array is freed (Length is set to zero) and this makes the returned pointer pointing to invalid data. Here is the code: 272: Result:= @cdDevices[nr]; 273: //free(cdDevices); 274: SetLength(cdDevices,0);
The Delphi version of oclGetDev is not equivament of the C (original) version. A fix is this:
function oclGetDev(cxGPUContext: Pcl_context; nr: TCL_uint): PCL_device_id; var szParmDataBytes : TSize_t; cdDevices : array of PCL_device_id; begin // get the list of GPU devices associated with context clGetContextInfo(cxGPUContext, CL_CONTEXT_DEVICES, 0, nil, @szParmDataBytes);
if ( szParmDataBytes / SizeOf(PCL_device_id) < nr ) then begin
Result := PCL_device_id(-1);
Exit;
end;
SetLength(cdDevices, szParmDataBytes div SizeOf(cdDevices[0]));
clGetContextInfo(cxGPUContext, CL_CONTEXT_DEVICES, szParmDataBytes, cdDevices, nil);
Result := cdDevices[nr];
end;
The function now return a PCL_device_d just like the original C version. This require other changes where oclGetDev is used: in main.pas: a) Declaration of two variables: FCurrentDevice: PCL_device_id; FCurrentInDevice: PCL_device_id; b) Initialisation of the variables at two places: in ComboBoxDeviceChange: FCurrentDevice := oclGetDev(FContext, ComboBoxDevice.ItemIndex); FCurrentInDevice := FCurrentDevice; and in FormCreate: FCurrentDevice := oclGetDev(FContext, i); FCurrentInDevice := FCurrentDevice;
By the way, oclGetFirstDev function (Not used in the demo) must be also changed because it suffers from the exact same issue.
I have now applied your fix. However, I did not (yet) fix oclGetFirstDev.
Thanks a lot.