CUDA - thread usage - copy one image after another into a buffer

I have a set of images that come from a camera and I need to store these images in CUDA device memory and render it using OpenGL. These images have to be stored one after another, contiguously, in a buffer. When I wrote the program, compiled and built the project, it worked perfectly without giving me any errors. However, when I implement it in real time on an OCT imaging system, I get access violation errors.

The questions I have are:

  1. Let there be 10 images that I need to store in a buffer, how do I do the correct thread assignment?
  2. I want to know if the program I wrote is correct?

The program I wrote goes like this:

const unsigned int tidx = blockDim.x * blockIdx.x + threadIdx.x;
const unsigned int tidy = blockDim.y * blockIdx.y + threadIdx.y;

const unsigned int adx =  tidx + tidy * blockDim.x * gridDim.x;

const unsigned int bdx = adx; 

int TotalFrames = 10;

for(int a = 1; a<=TotalFrames; a++)
{

int SingleFrame = (m_ImageHeight * m_ImageWidth);

int CurrentFrame = a * (m_ImageHeight * m_ImageWidth);  
// (first/next) frame is stored till the end of this value

int PreviousFrame = (a-1) * (m_ImageHeight * m_ImageWidth); 
// next frame will be stored in memory from the end of previous frame

if (  (a==1) )
{               
if (adx < CurrentFrame)
{
Buffer[adx] = EnergyImg[adx];                                                        
}
}

else if((a > 1) && (a <= TotalFrames))
{
if( ((adx > PreviousFrame) && (adx <= CurrentFrame)) )
{
while ( bdx < SingleFrame)
{ 
Buffer[adx] = EnergyImg[bdx];                                                                        
}
}
}

}