Error accesing pointer not allocated in the same method


I have class, which members are pointers to device space:


int *m_d_ptr;

In many kernel invocations I will use this memory, but I can determine needed memory space in constructor.

So I call in constructor:

cudaMalloc( (void**) &m_d_ptr, needed_size*sizeof(int));

But in class method on cudaMemcpy I’ve got:

Cuda error in file '' in line 208 : unknown error.

but if I do cudaMalloc in method just before copy, anythink works fine.

Why? I have no idea what am I doing wrong.

Hard to say, can’t say what’s wrong without seeing your code. How did you pass the variable?

Just a blind guess: a miss-definition of the copy-constructor and/or operator=

I have variable as class member.

Upper class creates my class and then calls from time to time computing function.

Computing function takes pointer from class member and operates on it.

I think that CUDA frees resource, when routine ends in constructor and returns to pure C++ class. Is there any way to tell driver, to do not release this resource, because I will need it later?