Thread: Problem with a .dll file

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Unhappy Problem with a .dll file

    Hi!

    I have a problem freeing a memory which was allocated in a .DLL file. I found this in a "dbgheap.c" file -> "Pointers from

    non-local heaps cannot be handled. For example, a non-local pointer may come from a DLL that has the CRT linked-in."

    So, my question is, how can I free a memory which was allocated in a .DLL file?

    This is the code:

    in .DLL file I have two files compiled, translate.c and translate.h

    Code:
    /*
    	TRANSLATE.C
    */
    # include <windows.h>
    # include <time.h>
    # define DLL_EXPORT
    # include "translate.h"
    
    
    
    char *ReadLanguage (LPCTSTR SectionName, LPCTSTR KeyName, LPCTSTR lpDefault, DWORD nSize, LPCTSTR FileName)
    {
    	char *ReadedString = NULL;
    	
    
    
    	if ((ReadedString = malloc (sizeof (char) * nSize)) != NULL)
    	{
    		GetPrivateProfileString (SectionName, KeyName, lpDefault, ReadedString, nSize, FileName);
    	}
    	else
    	{
    		// error checking
    		printf ("Error occured!");
    	}
    	return ReadedString;
    }
    Code:
    /*
    	TRANSLATE.H
    */
    # ifndef TRANSLATE_H
    # define TRANSLATE_H
    
    
    
    # ifndef DLL_EXPORT
    # define DLL_SPEC __declspec (dllimport) 
    # else 
    # define DLL_SPEC __declspec (dllexport)
    # endif
    
    
    
    // function prototype
    char DLL_SPEC *ReadLanguage (LPCTSTR SectionName, LPCTSTR KeyName, LPCTSTR lpDefault, DWORD nSize, LPCTSTR FileName);
    
    
    
    # endif // TRANSLATE_H
    Now, everything is compiled into a "cui.dll" file.

    And this is the project that is using the .DLL file.
    Code:
    /*
    	MAIN.C
    */
    # include <windows.h>
    # include <stdio.h>
    # include <time.h>
    # include "translate.h"
    
    
    
    # pragma comment (lib, "cui.lib")
    
    
    int main ()
    {
            char *TempINI = NULL;
    
    
    
    	if ((TempINI = ReadLanguage ("BLA", "KEY_1", "NULL", 80, "./en.lng")) == NULL) // ReadLanguage is in a .DLL file
    	{
    		printf ("Error occured!");
    	}
    	else
    	{
    		if ((short) atoi (TempINI))
    		{
    			printf ("VICTORY!");
    		}
    		free (TempINI); TempINI = NULL; // this is where the unhandled exception raises
    	}
        return 0;
    }
    So, if anyone knows how could this problem be solved, PLEASE tell me.

    Thank you!
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Either provide the DLL with memory to use:
    Code:
    DLL_SPEC void ReadLanguage(LPCTSTR SectionName, LPCTSTR KeyName, LPCTSTR lpDefault, LPTSTR Results, DWORD nSize, LPCTSTR FileName);
    Or provide another API for releasing resources:
    Code:
    DLL_SPEC void FreeString(LPCTSTR str);
    Also, "LPCTSTR" is a pointer to constant TCHAR, but all your code assumes char. If you use TCHAR, you should use it exclusively. Otherwise, use the following Win32 types:
    "LPSTR" = char*
    "LPCSTR" = const char*

    Or don't use Win32 types at all.

    gg

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    I provided DLL with another API for releasing resources and it is working now.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  4. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  5. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM