[1981] in java-interest
Garbage Collection Stomps on my Native Pointer...
daemon@ATHENA.MIT.EDU (Gabriel Lawrence)
Wed Sep 20 22:30:37 1995
Date: Wed, 20 Sep 1995 16:54:30 -0800
To: java-interest@java.sun.com
From: gabe@ucsd.edu (Gabriel Lawrence)
Cc: gabe@ucsd.edu
Howdy,
(sorry, I'm resending this because the version in the digest was not complete)
I have some C code that I am calling from Java. My goal is to maintain a
pointer to a native structure, and pass it back and forth.
I've written the following class declaration:
public
class syb {
public int context;
public native void ctx_alloc();
public native int con_alloc();
public native int connect(int con,String server);
Other methods deleted for brevity...
}
Within ctx_alloc, memory for a struct is allocated by the call to
cs_ctx_alloc, and the pointer that is created is placed into context, the
relevant outtake follows:
void syb_ctx_alloc(struct Hsyb *this)
{
CS_CONTEXT *ccontext;
CS_RETCODE cretcode;
cretcode = cs_ctx_alloc(EX_CTLIB_VERSION, &ccontext);
unhand(this)->context=(long)ccontext;
}
Within con_alloc, memory for yet another struct is allocated by the call to
ct_con_alloc and a pointer is returned, using a similar mechanism. Here is
an outtake of that code:
long syb_con_alloc(struct Hsyb *this)
{
CS_CONTEXT *ccontext;
CS_CONNECTION *cconnection;
CS_RETCODE cretcode;
ccontext=(CS_CONTEXT *)unhand(this)->context;
cretcode=ct_con_alloc(ccontext,&cconnection);
return (long)cconnection;
}
Later on, when the call to connect is issued things work great, unless the
garbage collector fires off. In this case, it goes off into la la land... I
assume I'm doing something wrong with the way I am trying to pass pointers,
or is there some call I should make to protect it from the GC?
Thanx for any help!
-Gabe
-----------------------------------------------------------------------
Scripps Institution of Oceanography
UC San Diego
Wrk: 619.534.8484
-----------------------------------------------------------------------
Hme: 619.455.6506 http://sio.ucsd.edu/~gabe/
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com