Cuda calls in DllMain

Hey,

I am building a Dll written using CUDA in the Visual Studio environment. I want to pre-allocate some CUDA memory when the DLL is loaded, and clean them up when the DLL is detached. Here is a sample DllMain call :

BOOL APIENTRY DllMain( HMODULE hModule,

                       DWORD  ul_reason_for_call,

                       LPVOID lpReserved

					 )

{

	switch (ul_reason_for_call)

	{

	case DLL_PROCESS_ATTACH:

		//do cuda memory allocation here

		break;

	case DLL_THREAD_ATTACH:

		break;

	case DLL_THREAD_DETACH:

		break;

	case DLL_PROCESS_DETACH:

		//do cuda memory deallocation here

		break;

	}

	return TRUE;

}

I tried adding the memory allocate and deallocate calls in DllMain. But my application crashes, and it crashes when it tries to do any allocation at DLL_PROCESS_ATTACH, or deallocate at DLL_PROCESS_DETACH.

Can some one help me understand what might be the issue here?

Thanks

Goutham

http://www.lenholgate.com/blog/2004/09/why-does-windows-hold-the-loader-lock-whilst-calling-dllmain.html

do I know this one for sure? nope, but I wouldn’t bet against this

How are you attempting to allocate the memory? Using the driver or runtime API?

I’ll second tmurray on this; there’s a good chance it’s an issue with the loader lock. Unless absolutely necessary, you should avoid making API calls from within DllMain because there are all kinds of semantic edge-cases which can cause problems. Raymond Chen’s blog (see below) is a good source of info on stuff like this:
http://blogs.msdn.com/b/oldnewthing/archive/2004/01/27/63401.aspx
http://blogs.msdn.com/b/oldnewthing/archive/2007/09/04/4731478.aspx

Some info from Microsoft on interactions in DllMain:

Thanks for replying!
Any suggestions for how I can load memory at start up. And then delete/deallocate towards the end!