2

Following this question, I'm trying to compile and test this GitHub Gist. But I get heap memory corruption error at runtime:

                         
Microsoft Visual C++ Runtime Library
Debug Error!
Program: ...

HEAP CORRUPTION DTECTED: after Normal block ... at ...
CRT detected that the application wrote to memory after the end of heap buffer

(Press Retry to debug the application)

But when pressing Retry as instructed the program seem to finish successfully with no erros/warnings or unexpected outputs.

A brief version of the main.c code:

#define CL_TARGET_OPENCL_VERSION 120

#include <CL/cl.h>
#include <stdio.h>
#include <stdlib.h> // exit, EXIT_FAILURE

int main(int argc, char const *argv[])
{
    cl_int err;
    cl_uint numPlatforms = 0;
    cl_platform_id *platforms;

    err = clGetPlatformIDs(0, NULL, &numPlatforms);

    platforms = (cl_platform_id *)malloc(sizeof(cl_platform_id) * numPlatforms);

    cl_uint *numDevices = (cl_uint *)malloc(sizeof(cl_uint) * numPlatforms);
    cl_device_id **devices = (cl_device_id **)malloc(sizeof(cl_device_id **) * numPlatforms);

    for (size_t i = 0; i < numPlatforms; i++)
    {
        err = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL, &numDevices[i]);

        devices[i] = (cl_device_id *)malloc(sizeof(cl_device_id *) * numDevices[i]);

        for (size_t j = 0; j < numDevices[i]; j++)
        {

            err = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, numDevices[i], (devices[i] + j), NULL);
        }
        free(devices[i]);
    }


    free(devices);
    free(numDevices);
    free(platforms);
    return 0;
}

I would appreciate it if you could help me know what is the problem and how I can resolve it.

Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193
  • 1
    `malloc(sizeof(cl_device_id *) * numDevices[i]);` -> `malloc(sizeof(cl_device_id) * numDevices[i]);` – kaylum Jun 09 '20 at 23:44
  • 1
    Similarly the preceding `malloc` is also incorrect but doesn't cause problems because the size ends up to be the same. `malloc(sizeof(cl_device_id **) * numPlatforms);` -> `malloc(sizeof(cl_device_id *) * numPlatforms);` – kaylum Jun 09 '20 at 23:46
  • @kaylum I must have been bran damaged or something last night. Thanks. There were other issues as well. fixed them. Will updated the Gist. – Foad S. Farimani Jun 10 '20 at 08:14

0 Answers0