Jaber
Jaber

Reputation: 17

Delete a cudaMalloc allocated memory inside kernel

I want to delete an array allocated by cudaMalloc inside a kernel using delete[]; but memory checker shows access violation, array is kept in memory and kernel continues to execute.

#include "cuda_runtime.h"
#include "device_launch_parameters.h"

__global__ void kernel(int *a)
{
    int *b = new int[10];

    delete[] b; // no violation

    delete[] a; // Memory Checker detects access violation.
}

int main()
{
    int *d_a;
    cudaMalloc(&d_a, 10 * sizeof(int));

    kernel<<<1, 1>>>(d_a);

    return 0;
}
  1. What is the difference between memory allocated by cudaMalloc and new in device code?
  2. Is it possible to delete memory allocated by cudaMalloc in device code?

Thanks

Upvotes: 1

Views: 668

Answers (1)

Robert Crovella
Robert Crovella

Reputation: 151879

  1. cudaMalloc in host code and new (or malloc) in device code allocate out of logically separate areas. The two areas are not generally interoperable from an API standpoint.

  2. no

You may wish to read the documentation. The description given there for in-kernel malloc and free generally applies to in-kernel new and delete as well.

Upvotes: 5

Related Questions