# cudaMemcpy error invalid device pointer

**URL:** <https://forums.developer.nvidia.com/t/cudamemcpy-error-invalid-device-pointer/7377>\
**Category:** CUDA Programming and Performance\
**Created:** [January 23, 2009, 12:58am UTC](https://forums.developer.nvidia.com/t/cudamemcpy-error-invalid-device-pointer/7377 "2009-01-23T00:58:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![springc](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@springc](https://forums.developer.nvidia.com/u/springc)\
**Post date:** [January 23, 2009, 12:58am UTC](https://forums.developer.nvidia.com/t/cudamemcpy-error-invalid-device-pointer/7377/1 "2009-01-23T00:58:12Z")

</div>

I encountered a strange problem. I am compiling my cuda application as a static library so that other application can use it. So I created several functions to allocate and access the device memory. Basically, my code look like this:

float \*result\_dev;

extern “C” {  
void GPUinit()  
{  
…  
cudaError\_t s=cudaMalloc((void\*\*)&result\_dev, size);  
cudaMemset((void\*) result\_dev, 16, size);  
float \*hostbuffer=(float \*) malloc(size);  
s=cudaMemcpy(hostbuffer, result\_dev, size, cudaMemcpyDeviceToHost); // runs successfully  
// save hostbuffer to file  
…  
}

readResult()  
{  
float \*hostbuffer=(float \*) malloc(size);  
s=cudaMemcpy(hostbuffer, result\_dev, size, cudaMemcpyDeviceToHost); // cudaMemcpy error invalid device pointer  
if (s != cudaSuccess)  
{  
printf(“cudaMemcpy error %s\n”, cudaGetErrorString(s));  
}  
//save hostbuffer to file  
}

}

When I save the hostbuffer from the GPUinit(), I can see the result correctly. However, when I call readResult(), the cudaMemcpy returns error code that translate to invalid device pointer.  
I am wondering if anyone else get into this issue. Whether it is the fact that I compile it to static library and link against my application, or it is related to the CUDA release (I believe I am using 2.0).

---

<div class="post-metadata">

**Author:** ![springc](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@springc](https://forums.developer.nvidia.com/u/springc)\
**Post date:** [January 23, 2009, 2:11am UTC](https://forums.developer.nvidia.com/t/cudamemcpy-error-invalid-device-pointer/7377/2 "2009-01-23T02:11:49Z")

</div>

Update, I wrote a simple test program and test library. Seems it works. So I still need to figure out what is wrong.  
BTW, I am wondering if it relates to whether I have control of the video card. I remember remote desktop doesn’t work. This application I am building is a server job invoked by some job scheduler. I am wondering if that is the reason.

---

<div class="post-metadata">

**Author:** ![springc](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@springc](https://forums.developer.nvidia.com/u/springc)\
**Post date:** [January 23, 2009, 8:46pm UTC](https://forums.developer.nvidia.com/t/cudamemcpy-error-invalid-device-pointer/7377/3 "2009-01-23T20:46:36Z")

</div>

Ok, I found out why. My application use threads and the initialization function is run in another thread.

From another thread of discussion http://forums.nvidia.com/lofiversion/index.php?t55838.html

You cannot use Ad, Bd and Cd in a thread that did not CudaMalloc them, each thread has its own context and cannot exchange device-pointers with other threads.

This is exactly my issue.

> [@](#):
>
> Update, I wrote a simple test program and test library. Seems it works. So I still need to figure out what is wrong.
> 
> BTW, I am wondering if it relates to whether I have control of the video card. I remember remote desktop doesn’t work. This application I am building is a server job invoked by some job scheduler. I am wondering if that is the reason.
