Jump to content
Excelsior Forums
Sign in to follow this  
mpedzisai

How to free memory allocated in external function

Recommended Posts

I am a recent adoptee of xFunction. I use it in a servlet that calls a WIndow DLL and return all kinds of data including structures. Haveling looked through the docs, I have not been able to find definitive information about how to free memory that has been allocated in an external dll/function using malloc or new.

Does xFunction automatically free pointers returned to it?

Does the responsibility for freeing malloc'ed memory rest with the DLL or xFunction?

Its a bit unclear.

Example below.

External function


typedef struct JNIAction_s {		
        int id;
        char *name;
} JNIAction;

extern "C" DllExport JNIAction  _stdcall DLLGetDefaultAction(LPTSTR as_sessionid) //assumes that session established already 
{	
AFX_MANAGE_STATE(AfxGetStaticModuleState());

JNIAction a;	
a.id = 1;
                a.name = strdup("Action Name");//allocated on heap -- how do I free this when JNIAction is sent back to xFunction
               return a;
}

Java Side


a)  Structure
public class JNIAction extends Structure {
    
    /** Creates a new instance of JNIAction */
    public JNIAction() {        
    }
    
    
    public int id;
    public String name;        

    public String defineLayout() {        
        return "int id, CSTRING name ";
    }
}

 Calling the external function

  xFunction l_func = new  xFunction("SOMEDLL", "JNIAction DLLGetDefaultAction(CSTRING)");
  JNIAction jniaction = (JNIAction)l_func.invoke("1", Argument.CSTRING));

The question is how the name member of JNIAction will be freed.

Does that happen automatically in xFunction

If not what can I call in xFunction to ensure that pointer related member variables like JNIAction.name are freed?

If xFunction does not automatically free the memory and does not have a facility for freeing memory explicitly, does anybody know of a good way to free memory allocated on the heap in an external function.

Thanks,

Melody

Share this post


Link to post
Share on other sites

Thanks for the response. Can you give me an example of how you would do that. If you notice in the example code, the whole structure (JNIAction) is initialized and returned from within a function. In other words it is not stored anywhwre after it has been returned to xFunction. How then would I have access to it so I can free memory.

In any event, are you saying that there is no way to free memory, either explicitly or automatically, from xFunction. If that is the case it seems to me that that would be dumb and xFunction would be no convenience at all because then it would leave the programmer with the most classic of problems when crossing the Java/C++ bridge.

Thanks,

Melody

Share this post


Link to post
Share on other sites

xFunction is not intended to manage memory of your C code.  All what it does is calling external functions (that is why it is called xFunction :)).

As to your question, I have a question for you before answering:

If it were C not Java code, how would you free the memory?

Share this post


Link to post
Share on other sites

>>>

As to your question, I have a question for you before answering:

If it were C not Java code, how would you free the memory?

<<<

To answer your question, I would probably free the memory in the calling functionm after I have used JNAction. Example

void callingFunc() {

JNIAction a= DLLGetDefaultAction"1");

  int id = a.id;

  //etc

///NOw I am done with JNIAction I am ready to free the memory allocated to JNIACtion.name (a char *)

  free(a.name);

}

In the same breath, my expectation was that xFunction would recognize the fact that after returning an object that was allocated in C as shown in my example, I would have no access to it anymore, unless I decided to store it some place in the DLL, only to come back and release memory after I have finished copying the data in Java.

If I have to that for every obect that I allocate using malloc then xFunction totally useless! If I knew that before I bought it I would definitely have not bought it but than the documentation is murky and somehow leaves the impression that any pointers that you do not free using xFunction's free() method are indeed cleaned up for you by the library.

Thanks,

Melody

Share this post


Link to post
Share on other sites

///NOw I am done with JNIAction I am ready to free the memory allocated to JNIACtion.name (a char *)

   free(a.name);

So, why do not you do the same with xFunction? "free" is external function so there is no problem to call it from java with xFunction.

Yes, since "free" takes C pointer as argument, you should have Pointer in Java code to be able to call the function. However, your problem that you choosed "CSTRING" to describe "name" field, which means that you would like to work with "name" as with String not as with Pointer. However it contrudicts with your need to free memory pointed by the pointer. So, if you describe "name" field as "char*", you will be able to free memory form Java with xFunction.  Howewer, it may be inconvinient for you to have "name" as Pointer, if you need it as Java String at some point of your Java code. You may solve this problem by two ways:

1. Write Java code that converts Pointer to char to Java String. It is possible with xFunction methods (I may agree that xFunction might have the direct method for it)

2. Duplicate "name" field in your C code, so the structure would have 2 fields that point to the same memory. After that one of the field could be described as CString and another as "char*" in xFunction, and all you problems would be solved after that.

leaves the impression that any pointers that you do not free using xFunction's free() method are indeed cleaned up for you by the library.

It is technically impossible. xFunction has no knowledge about how you allocate the memory in "C" code thus it can not automatically clenaup it. It would be real magic, if xFunction could make C language  being managed :).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×