OpenGL interoperabilty: cudaGraphicsSubResourceGetMappedArray and read access violation

Hi,

I am trying to read an opengl texture in cuda. However, I am encountering a read access violation with cudaGraphicsSubResourceGetMappedArray. And I do not see the reason. Here is the code:

GLuint m_textureID;
cudaGraphicsResource* m_resource;
cudaArray_t* m_array;
int argc = 0;
char** argv = NULL;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(512, 512);
glutCreateWindow("CUDA GL Interop");

cudaStatus = cudaGLSetGLDevice(0);
handleCudaError(cudaStatus);

glGenTextures(1, &(m_textureID));
handleOpenGLError();

glBindTexture(GL_TEXTURE_2D, m_textureID);
handleOpenGLError();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
handleOpenGLError();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
handleOpenGLError();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
handleOpenGLError();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
handleOpenGLError();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
handleOpenGLError();

glBindTexture(GL_TEXTURE_2D, 0);
handleOpenGLError();

cudaStatus = cudaGraphicsGLRegisterImage(&m_resource, m_textureID, GL_TEXTURE_2D, ::cudaGraphicsRegisterFlagsReadOnly);
handleCudaError(cudaStatus);

cudaStatus = cudaGraphicsMapResources(1, &m_resource, nullptr);
handleCudaError(cudaStatus);

[b]cudaStatus = cudaGraphicsSubResourceGetMappedArray(m_array, m_resource, 0, 0);
[/b]handleCudaError(cudaStatus);

cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(8, 8, 8, 8, cudaChannelFormatKindUnsigned);

textureReference texRef;
texRef.addressMode[0] = cudaAddressModeBorder;
texRef.addressMode[1] = cudaAddressModeBorder;
texRef.filterMode = cudaFilterModePoint;
texRef.normalized = false;

cudaStatus = cudaBindTextureToArray(&texRef, *m_array, &channelDesc);

I have tested a lot of different things. Normally I am using Qt instead of freeglut, and I have the same issue.

I am under windows 10, with cuda 9, a GTX 1070, optimus (but the application run with the nvidia card). I compile with visual studio 2017 in 64 bits.

Do you see what I am doing wrong ?
Thank you in advance for your help.

Simon

I found the issue:
cudaArray_t is already a pointer. So cudaArray_t* is a pointer of pointer. I was aware of that, but forgot that then I need to create the pointer cudaArray_t.