global device pointers in shared library

I’ve built a library which contains global pointers to device memory, which are only accessed from a couple of functions in that library contained in file fileA.cu and compiled using nvcc --compile --compiler-options -fPIC:

fileA.cu

[codebox]void *d_ptr;

extern “C” {

void foo1() {

...

cudaMalloc(d_ptr);

...

}

void foo2() {

...

cudaMemcpy(d_ptr);

...

}

}[/codebox]

The object file is then included in a library (gcc -shared -o libtest.so fileA.o fileB.o fileC.o-lcudart). However, if I successfully malloc d_ptr (returns cudaSuccess) with a call to foo1 at one point outside of the library, a subsequent call to foo2() results in an invalid device pointer error, even though the address that pointer contains is the same as when it was malloced. Is there some reason that d_ptr would be automatically freed at some point because it is in a library?

Thanks very much for any help.