Java Object allocation from C++ and references?
Ben Martin
monkeyiq@users.sourceforge.net
Wed Feb 4 04:04:00 GMT 2004
On Wed, 2004年02月04日 at 00:40, Andrew Haley wrote:
> Ben Martin writes:
> > is it correct to assume that if one is to use C++ code and create an
> > object implemented in Java code, for example, java::lang::String, then
> > the C++ code creating the new String* does not call delete?
>> That's right.
>> > I have rolled rpms for jakarta-lucene using gcj from Fedora
> > development and using C++ smart pointers to manage
> > java::lang::string objects seems to work but when I use a C++ smart
> > pointer on one of the Lucene java objects the program crashes on
> > the delete of that object.
>> You mustn't do that. Let the garbage collector do the job.
This is initially the way I thought it would work. The only problem that
I saw that led my to question my initial thoughts are how does the JVM
know that the C++ pointer is still required or is not required?
Example
{
MyFoo* foo = new mytree::MyFoo();
MyBar* bar = new mytree::MyBar();
bar->methodname( foo );
// at this point foo & bar are "locked" by the C++ code,
// the GC can not collect them because I still have C++
// pointers to them and the JVM will have no way of knowing
// when those pointers drop out of C++ scope on the next code
// line so from my understanding the GC can never collect
// those objects. If the GC *does* collect them then a dereference
// of foo or bar which may happen next instead will do very bad
// things.
}
Sorry if this seems a trivial question, but I can't help but think there
are other newbie gcj users who would have the same question.
--
Blogtackular http://advogato.org/person/monkeyiq/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://gcc.gnu.org/pipermail/java/attachments/20040204/367091a7/attachment.sig>
More information about the Java
mailing list