-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TIMOB-25599] Android: Fix Java object referencing #9675
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8a853bb
[TIMOB-25599] Maintain soft Java reference, let JVM manage our Java o…
de159e6
[TIMOB-25599] Improve ReferenceTable
750bc6c
[TIMOB-25599] Always use reference table
7a357aa
Merge branch 'master' into TIMOB-25599
9f807f5
Merge branch 'master' into TIMOB-25599
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ using namespace v8; | |
|
||
namespace titanium { | ||
|
||
bool JavaObject::useGlobalRefs = true; | ||
bool JavaObject::useGlobalRefs = false; | ||
|
||
#ifdef TI_DEBUG | ||
static struct { | ||
|
@@ -62,8 +62,9 @@ JavaObject::~JavaObject() | |
} | ||
|
||
// Make sure we wipe the persistent, in case we called delete on the proxy and didn't get deleted as a result of the NativeObject WeakCallback | ||
if (persistent().IsEmpty()) | ||
if (persistent().IsEmpty()) { | ||
return; | ||
} | ||
persistent().Reset(); | ||
} | ||
|
||
|
@@ -123,14 +124,17 @@ void JavaObject::MakeJSWeak() | |
// but this time, we say call us back as a finalizer so we can resurrect the | ||
// object (save it from really being GCd by V8) and move it's Java object twin | ||
// to a weak reference in the JVM. (where we can track when that gets GC'd by the JVM to call back and kill this) | ||
persistent().SetWeak(this, DetachCallback, v8::WeakCallbackType::kFinalizer); // MUST BE kFinalizer or our object cannot be resurrected! | ||
persistent().MarkIndependent(); | ||
if (!isDetached()) { | ||
persistent().SetWeak(this, DetachCallback, v8::WeakCallbackType::kFinalizer); // MUST BE kFinalizer or our object cannot be resurrected! | ||
persistent().MarkIndependent(); | ||
} | ||
} | ||
|
||
void JavaObject::detach() | ||
{ | ||
// WAIT A SECOND V8!!! DON'T KILL MY OBJECT YET! THE JVM MAY STILL WANT IT! | ||
persistent().ClearWeak(); // Make JS Strong Again! | ||
persistent().MarkActive(); | ||
|
||
// if the JVM side is a weak reference or we have no object wrapped, don't do anything else | ||
if (isDetached()) { | ||
|
@@ -168,12 +172,13 @@ void JavaObject::MakeJavaStrong() | |
ASSERT(refTableKey_ != 0); | ||
JNIEnv *env = JNIUtil::getJNIEnv(); | ||
ASSERT(env != NULL); | ||
jobject stored = ReferenceTable::clearWeakReference(refTableKey_); | ||
jobject stored = ReferenceTable::clearReference(refTableKey_); | ||
if (stored == NULL) { | ||
// Sanity check. Did we get into a state where it was weak on Java, got GC'd but the C++ proxy didn't get deleted yet? | ||
LOGE(TAG, "!!! OH NO! We tried to move a weak Java object back to strong, but it's aleady been GC'd by JVM! We're in a bad state! Key: %d", refTableKey_); | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this fix should definitely be applied. I assume if the store ref is NULL, trying to delete the local ref will be very bad. |
||
env->DeleteLocalRef(stored); | ||
} | ||
env->DeleteLocalRef(stored); | ||
} else { | ||
// New entry, make sure we have no key, have an object, get a new key | ||
ASSERT(javaObject_ != NULL); | ||
|
@@ -204,7 +209,9 @@ void JavaObject::MakeJavaWeak() | |
javaObject_ = weakRef; | ||
} else { | ||
ASSERT(refTableKey_ != 0); | ||
ReferenceTable::makeWeakReference(refTableKey_); | ||
|
||
// create a soft reference, which is more resiliant than a weak reference | ||
ReferenceTable::makeSoftReference(refTableKey_); | ||
} | ||
|
||
UPDATE_STATS(0, 1); // add one to "detached" counter | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So if you're using this quard, it's specifically checking isWeakRef_ is true, which only happens if #MakeJavaWeak() was called (and you've commented that call out from #detach() now). So I'm not sure this guard will ever be false now.