1st call to cudaMallocHost fails... ... but next calls are OK. (!?)

I’m having trouble with the cudaMalloc/cudaMallocHost functions. The 1st call always fails with cudaErrorUnknown. The following calls are OK though (with same parameter values). Is there something I’m missing, like an initialization function? I tried moving function calls to put a cudaMalloc before the cudaMallocHost, in that case it’s the 1st call to cudaMalloc that fails.

__host__ void* _AllocateHostBuffer( int width, int height)

{

   unsigned char *hostData = NULL;

   int memSize = width*height;

   cudaError_t errVal;

   errVal = cudaMallocHost( (void**)&hostData, memSize );   // <--- on 1st call, fails

if(errVal == cudaSuccess)

	  return hostData;

   else

   {

	  // Try again (for some reason, the 1st call to cudaMallocxyz fails)

	  errVal = cudaMallocHost( (void**)&hostData, memSize );  // <--- OK

	  if(errVal == cudaSuccess)

		 return hostData;

	  else

		 return NULL;

   }

}

Get the error prior to the first cudaMallocHost() or cudaMalloc() call. For example, you could add the following code right before:

printf("last CUDA error: %s\n", cudaGetErrorString( cudaGetLastError() ) );

My guess is that some call prior to your memory allocations causes the error, which then stays in the state until your call. Do you make any CUDA calls prior to this function?

Paulius