Problem debugging SDL + OpenGL code using NSight

Hello everyone,

I have been trying to debug a test application with Nsight 3.2.2 in Visual Studio 2012, but it keeps crashing (nsighttest.exe has stopped working). If I use the debug the application from the error message, I can see that an access violation error is occurring. If I run the code using either a Debug or Release build but without Nsight, there is no error.

I have created a short version of my code to reproduce this issue (I know, it doesn’t do much as is). The access violation happens in the ‘cudaGraphicsGLRegisterImage’ call, and I realized that by removing the ‘SDL_GL_CreateContext’ call, the error no longer occurs. Is there any know incompatibility between Nsight and SDL 2.0?

The error:

Unhandled exception at 0x651B5A17 (nvcuda.dll) in nsighttest.exe: 0xC0000005: Access violation reading location 0x00000010.

Test code:

#include <GL/glew.h>

/* Include windows.h properly on Windows */
#if defined(WIN32) || defined(_WINDOWS)
#  define WIN32_LEAN_AND_MEAN
#  define NOMINMAX
#  include <windows.h>
#endif

#define NO_SDL_GLEXT
#include <SDL.h>
#include <SDL_opengl.h>
#ifndef SDL_MAGIC_MAIN
#  undef main
#endif

#include "cuda_runtime.h"
#include <cuda_gl_interop.h>
#include "device_launch_parameters.h"

#include <iostream>

int main()
{
	// Initialize SDL
	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		std::cerr << "Error initializing SDL: " << SDL_GetError() << std::endl;
		return false;
	}
	
	// Set OpenGL attributes
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
	
	// Create window
	SDL_Window* window = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, 
			SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
	if (!window)
	{
		std::cerr << "Error creating SDL window: " << SDL_GetError() << std::endl;
		return false;
	}
	
	// Create OpenGL context
	SDL_GLContext openGlContext = SDL_GL_CreateContext(window);
	if (!openGlContext)
	{
		std::cerr << "Error creating GL context " << SDL_GetError() << std::endl;
	}
	
	// Clear our buffer
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT);

	// Swap our back buffer to the front
	SDL_GL_SwapWindow(window);

	// Initialize GLEW
	if (glewInit() != GLEW_OK) {
		std::cerr << "Error initializing GLEW" << std::endl;
		return false;
	}
	if (!glewIsSupported("GL_VERSION_2_0 "))
	{
		std::cerr << "Support for necessary OpenGL extensions missing." << std::endl;
		return false;
	}

	// Initialize cuda device
	cudaSetDevice(0);
	
	GLuint texture;
	cudaGraphicsResource_t cudaGLBuffer;

	// Create OpenGL texture
	glEnable(GL_TEXTURE_2D);
	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 800, 600, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
	glBindTexture(GL_TEXTURE_2D, 0);

	// Register texture with CUDA
	cudaGraphicsGLRegisterImage(&cudaGLBuffer, texture, GL_TEXTURE_2D, cudaGraphicsRegisterFlagsSurfaceLoadStore);


	// cudaDeviceReset must be called before exiting in order for profiling and
	// tracing tools such as Nsight and Visual Profiler to show complete traces.
	cudaError_t cudaStatus = cudaDeviceReset();
	if (cudaStatus != cudaSuccess) {
		fprintf(stderr, "cudaDeviceReset failed!");
		return 1;
	}

	return 0;
}

Any ideas?

Hi leonardord,

I have tried your code and I am not able to reproduce the problem on my end, however, I am on the Nsight 4.0 RC1 build. Would you be able to try that (you’ll also need to update to a newer driver build if possible)?

What GPU is this happening on? What part of Nsight are you trying when you see the crash, “Start CUDA Debugging”?

Thanks

PS: I recommend posting on the Nsight Visual Studio Edition forums, I would’ve seen this earlier. Thanks