Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

When call DeleteLocalRef?  RSS feed

 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I'm calling a void java method while in C++ and I created some local object inside the c++ method (e.g. creating NewStringUTF) after calling CallVoidMethod should I ALWAYS call DeleteLocalRef? What about for a method that returns a value? How do i know when to DeleteLocalRef?

(And if you know, is there a diff. between ReleaseStringChars and DeleteLocalRef? Can i just use DeleteLocalRef?)

Thanks!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling DeleteLocalRef is very similar to setting a Java local variable to null. Both can make an object eligible for garbage collection a little earlier; but both are ultimately unnecessary as the JVM will do it automatically when the method returns. You'd use DeleteLocalRef if you had a long-running native method and wanted to clean up your mess as you go -- the same reason given for (occasionally) setting local vars to null.
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Calling DeleteLocalRef is very similar to setting a Java local variable to null. Both can make an object eligible for garbage collection a little earlier; but both are ultimately unnecessary as the JVM will do it automatically when the method returns. You'd use DeleteLocalRef if you had a long-running native method and wanted to clean up your mess as you go -- the same reason given for (occasionally) setting local vars to null.


Ah, I understand. So basically it's more for if you're going to approach/exceed the 16 local ref space in the local frame, right? If my c++ method exits fairly quickly after variable creation, I don't need to worry? Also, what about ReleaseStringChars? Do i still want to do that and why?

Thanks!


Also, one quick question:

* If I'm creating a jobject in a loop and putting it into a jobjectArray, if I call DeleteLocalRef after putting it in the jobjectArray will it null the ref. in the jobjectArray as well? Or just the local variable representing it?
[ April 08, 2008: Message edited by: Dan Bizman ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!