Problem with .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
# include
# 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
# include
# include
# 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!

Comments

  • : 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
    : # include
    : # 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
    : # include
    : # include
    : # 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!
    :
    [blue]You have to pass the allocated pointer back into DLL and use 'free()' inside DLL code. In other words: you have to make two way interface for your allocations:

    char* Func1 (); // Allocate it here
    void Func2 (char* pAllocated); // release it here

    Both Func1() and Func2() should reside in your DLL.[/blue]
  • : : 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
    : : # include
    : : # 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
    : : # include
    : : # include
    : : # 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!
    : :
    : [blue]You have to pass the allocated pointer back into DLL and use 'free()' inside DLL code. In other words: you have to make two way interface for your allocations:
    :
    : char* Func1 (); // Allocate it here
    : void Func2 (char* pAllocated); // release it here
    :
    : Both Func1() and Func2() should reside in your DLL.[/blue]
    :


    YUHUHUH!!!

    THANKS MAN!

    Now, I'm gonna drink some beer. :)-~
    Check my programs!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories