Commit 379dbfae authored by Dongjun Kim's avatar Dongjun Kim Committed by Commit Bot

Add finalizers to Web Payment Java objects that own native objects

These C++ objects are destroyed in PaymentRequestImpl.closeUIAndDestroyNativeObjects().
However, if an object is garbage-collected for any reason without
PaymentRequestImpl.closeUIAndDestroyNativeObjects() being called,
then it will cause a memory leak.

A finalize method should be added to these objects to ensure destruction
of the owned C++ objects.

Bug: 1090918
Change-Id: I12df86f9705d81d630b63223d15354d2b022e888
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2392290
Commit-Queue: DongJun Kim <djmix.kim@samsung.com>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#804820}
parent d305c0fa
...@@ -46,6 +46,16 @@ public class CurrencyFormatter { ...@@ -46,6 +46,16 @@ public class CurrencyFormatter {
} }
} }
/**
* Destroys native object if owned object is not destroyed.
*
* @see java.lang.Object#finalize()
*/
@Override
public void finalize() throws Throwable {
destroy();
}
/** @return The currency code formatted for display. */ /** @return The currency code formatted for display. */
public String getFormattedCurrencyCode() { public String getFormattedCurrencyCode() {
return CurrencyFormatterJni.get().getFormattedCurrencyCode( return CurrencyFormatterJni.get().getFormattedCurrencyCode(
......
...@@ -35,6 +35,16 @@ public class JourneyLogger { ...@@ -35,6 +35,16 @@ public class JourneyLogger {
} }
} }
/**
* Destroys native object if owned object is not destroyed.
*
* @see java.lang.Object#finalize()
*/
@Override
public void finalize() throws Throwable {
destroy();
}
/** /**
* Sets the number of suggestions shown for the specified section. * Sets the number of suggestions shown for the specified section.
* *
......
...@@ -153,7 +153,17 @@ public class PaymentDetailsUpdateServiceHelper { ...@@ -153,7 +153,17 @@ public class PaymentDetailsUpdateServiceHelper {
*/ */
public void reset() { public void reset() {
ThreadUtils.assertOnUiThread(); ThreadUtils.assertOnUiThread();
sInstance = null; if (sInstance != null) sInstance = null;
}
/**
* Resets singleton instance if it is not destroyed.
*
* @see java.lang.Object#finalize()
*/
@Override
public void finalize() throws Throwable {
reset();
} }
/** /**
......
...@@ -73,8 +73,20 @@ public class PaymentHandlerHost { ...@@ -73,8 +73,20 @@ public class PaymentHandlerHost {
/** Destroys the native bridge. This object shouldn't be used afterwards. */ /** Destroys the native bridge. This object shouldn't be used afterwards. */
public void destroy() { public void destroy() {
PaymentHandlerHostJni.get().destroy(mNativePointer); if (mNativePointer != 0) {
mNativePointer = 0; PaymentHandlerHostJni.get().destroy(mNativePointer);
mNativePointer = 0;
}
}
/**
* Destroys native object if owned object is not destroyed.
*
* @see java.lang.Object#finalize()
*/
@Override
public void finalize() throws Throwable {
destroy();
} }
/** /**
......
...@@ -80,6 +80,16 @@ public class PaymentRequestSpec { ...@@ -80,6 +80,16 @@ public class PaymentRequestSpec {
mNativePointer = 0; mNativePointer = 0;
} }
/**
* Destroys native object if owned object is not destroyed.
*
* @see java.lang.Object#finalize()
*/
@Override
public void finalize() throws Throwable {
destroy();
}
@CalledByNative @CalledByNative
private long getNativePointer() { private long getNativePointer() {
return mNativePointer; return mNativePointer;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment