+ Reply to Thread
Results 1 to 2 of 2

Thread: application crash whie deleting an object returned from a DLL

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    26

    application crash whie deleting an object returned from a DLL

    I have following DLL code, exported DLL function:
    Code:
    extern "C" __declspec(dllexport) char * GetArray()
    {
    	return new char [100] ;
    }

    And client Application code is:
    Code:
    void UseLoadLibrary()
    {
    	HMODULE hDLL = LoadLibrary("MyDLL.dll") ;
    	if (NULL != hDLL)
    	{
    		typedef char * (*PFNGETARRAY) () ;
    		PFNGETARRAY pfnGetArray = NULL ;
    		pfnGetArray = (PFNGETARRAY) GetProcAddress(hDLL, "GetArray") ;
    		char *pArray = pfnGetArray() ;
    		delete pArray ;
    		FreeLibrary(hDLL) ;
    		hDLL = NULL ;
    	}
    }

    What could be the reason for this?

  2. #2
    Your, DLL should export another method like:
    Code:
    extern "C" __declspec(dllexport) void ReleaseArray(char *pArray)
    {
    	delete []pArray ;
    	pArray = NULL ;
    }
    Code:
    void UseLoadLibrary()
    {
    	HMODULE hDLL = LoadLibrary("MyDLL.dll") ;
    
    	if (NULL != hDLL)
    	{
    		typedef char * (*PFNGETARRAY) () ;
    		PFNGETARRAY pfnGetArray = NULL ;
    		pfnGetArray = (PFNGETARRAY) GetProcAddress(hDLL, "GetArray") ;
    		char *pArray = pfnGetArray() ;
    		
    		//delete []pArray ; DO NOT CALL THIS FROM CLIENT APPLICATION
    
    		typedef void (*PFNRELEASEARRAY) (char *) ;
    		PFNRELEASEARRAY pfnReleaseArray = NULL ;
    		pfnReleaseArray = (PFNRELEASEARRAY) GetProcAddress(hDLL, "ReleaseArray") ;
    		pfnReleaseArray(pArray) ;
    		
    		FreeLibrary(hDLL) ;
    		hDLL = NULL ;
    	}
    }
    Hope this solves your issue.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts