Skip to content

Commit

Permalink
Remove AppDelegate memory leak from Android projects
Browse files Browse the repository at this point in the history
AppDelegate object and all its members are never released.
As a solution I propose to use static unique_ptr that could destroy it
at the end of the application.

Issue:
cocos2d#14110
  • Loading branch information
Greg Rynkowski committed Aug 6, 2016
1 parent 2f23ffb commit f01b7d8
Show file tree
Hide file tree
Showing 25 changed files with 175 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
8 changes: 7 additions & 1 deletion tests/cpp-empty-test/proj.android-studio/app/jni/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
8 changes: 7 additions & 1 deletion tests/cpp-empty-test/proj.android/jni/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
8 changes: 7 additions & 1 deletion tests/cpp-tests/proj.android-studio/app/jni/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
8 changes: 7 additions & 1 deletion tests/cpp-tests/proj.android/jni/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
8 changes: 7 additions & 1 deletion tests/game-controller-test/proj.android/jni/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
8 changes: 7 additions & 1 deletion tests/js-memory-gc-tests/project/proj.android/jni/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
8 changes: 7 additions & 1 deletion tests/js-tests/project/proj.android-studio/app/jni/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
8 changes: 7 additions & 1 deletion tests/js-tests/project/proj.android/jni/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
8 changes: 7 additions & 1 deletion tests/lua-empty-test/project/proj.android/jni/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
8 changes: 7 additions & 1 deletion tests/lua-tests/project/proj.android-studio/app/jni/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <memory>

#include <android/log.h>
#include <jni.h>

Expand All @@ -6,7 +8,11 @@
#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

namespace {
std::unique_ptr<AppDelegate> appDelegate;
}

void cocos_android_app_init(JNIEnv* env) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
appDelegate.reset(new AppDelegate());
}
Loading

0 comments on commit f01b7d8

Please sign in to comment.