-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
fixed #17594: [android] fix crash and memory leaks if launching game multiple times on Android platform with PhysX module enabled. #17602
Changes from 8 commits
6f099c9
ba62d1d
ee78f83
08ca142
96f85e5
f832734
4820a00
04ca338
8271229
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,7 @@ PhysXSharedBody *PhysXSharedBody::getSharedBody(const Node *node, PhysXWorld *co | |
PhysXSharedBody *newSB; | ||
if (iter != sharedBodesMap.end()) { | ||
newSB = iter->second; | ||
CC_ASSERT_EQ(newSB->_mWrappedWorld, world); | ||
} else { | ||
newSB = ccnew PhysXSharedBody(const_cast<Node *>(node), world, body); | ||
newSB->_mFilterData.word0 = 1; | ||
|
@@ -84,6 +85,16 @@ PhysXSharedBody *PhysXSharedBody::getSharedBody(const Node *node, PhysXWorld *co | |
return newSB; | ||
} | ||
|
||
void PhysXSharedBody::clearCache() { | ||
// Move the map to avoid erase operation in the following for loop since 'delete' will trigger ~PhysxSharedBody. | ||
// clearCache is invoked only in the destructor of PhysXWorld. | ||
auto tmpMap = std::move(sharedBodesMap); | ||
Comment on lines
+89
to
+91
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. style: Consider using a unique_ptr for automatic memory management of PhysXSharedBody instances |
||
for (auto &e : tmpMap) { | ||
delete e.second; | ||
} | ||
sharedBodesMap.clear(); | ||
} | ||
|
||
PhysXSharedBody::~PhysXSharedBody() { | ||
sharedBodesMap.erase(_mNode); | ||
if (_mStaticActor != nullptr) PX_RELEASE(_mStaticActor); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,12 @@ void ADPFManager::initialize() { | |
} | ||
} | ||
|
||
void ADPFManager::destroy() { | ||
JNIEnv *env = cc::JniHelper::getEnv(); | ||
env->DeleteGlobalRef(obj_power_service_); | ||
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. style: Consider checking if obj_power_service_ is not null before deleting 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. style: Consider checking if obj_power_service_ is not null before deleting |
||
obj_power_service_ = nullptr; | ||
} | ||
|
||
// Initialize JNI calls for the powermanager. | ||
bool ADPFManager::initializePowerManager() { | ||
#if __ANDROID_API__ >= 31 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,8 +53,10 @@ public static boolean init(String dbName, String tableName) { | |
} | ||
|
||
public static void destroy() { | ||
mDatabaseOpenHelper = null; | ||
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. style: Consider adding a null check before setting mDatabaseOpenHelper to null |
||
if (mDatabase != null) { | ||
mDatabase.close(); | ||
mDatabase = null; | ||
} | ||
Comment on lines
55
to
60
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. style: Consider adding a null check before closing mDatabase to prevent potential NullPointerException |
||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,11 @@ public CocosSensorHandler(final Context context) { | |
mSensorHandler = this; | ||
} | ||
|
||
public static void resetStaticVariables() { | ||
mSensorHandler = null; | ||
mEnableSensor = false; | ||
} | ||
Comment on lines
+61
to
+64
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. style: Consider adding synchronization to prevent potential race conditions when resetting static variables. |
||
|
||
// =========================================================== | ||
// Getter & Setter | ||
// =========================================================== | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,6 @@ public class CocosVideoHelper { | |
private Activity mActivity = null; | ||
private static SparseArray<CocosVideoView> sVideoViews = null; | ||
static VideoHandler mVideoHandler = null; | ||
private static Handler sHandler = null; | ||
|
||
CocosVideoHelper(Activity activity, FrameLayout layout) | ||
{ | ||
|
@@ -57,7 +56,16 @@ public class CocosVideoHelper { | |
|
||
mVideoHandler = new VideoHandler(this); | ||
sVideoViews = new SparseArray<CocosVideoView>(); | ||
sHandler = new Handler(Looper.myLooper()); | ||
} | ||
|
||
public void destroy() { | ||
if (mVideoHandler != null) { | ||
mVideoHandler.removeCallbacksAndMessages(null); | ||
mVideoHandler = null; | ||
} | ||
Comment on lines
+62
to
+65
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. style: Consider using a more specific method like removeCallbacks() instead of removeCallbacksAndMessages(null) if possible
Comment on lines
+62
to
+65
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. style: Consider using a more specific method like removeCallbacks() instead of removeCallbacksAndMessages(null) if possible |
||
videoEventListener = null; | ||
mLayout = null; | ||
mActivity = null; | ||
} | ||
|
||
private static int videoTag = 0; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,15 @@ public CocosWebViewHelper(FrameLayout layout) { | |
CocosWebViewHelper.webViews = new SparseArray<CocosWebView>(); | ||
} | ||
|
||
public static void resetStaticVariables() { | ||
sLayout = null; | ||
if (sHandler != null) { | ||
sHandler.removeCallbacksAndMessages(null); | ||
sHandler = null; | ||
Comment on lines
+62
to
+63
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. style: Ensure all callbacks are removed before setting sHandler to null to prevent potential memory leaks |
||
} | ||
webViews = null; | ||
} | ||
|
||
private static native boolean shouldStartLoading(int index, String message); | ||
private static native void didFinishLoading(int index, String message); | ||
private static native void didFailLoading(int index, String message); | ||
|
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.
style: Consider using a unique_ptr for automatic memory management of PhysXSharedBody instances