Skip to content

Commit

Permalink
Fix memory leaks
Browse files Browse the repository at this point in the history
1. DriverBase no destroyInstance
2. Every object inherited from ax::Ref shoud destory before ScriptEngineManager, otherwise will trigger it's reinit and leak
3.  QuadCommand always leak isolated indices
4. static singleton should'nt inhert from ax::Ref due to destory it before ScriptEngineManager impossible
5. Make Director inherit from ax::Ref due to it also cause ScriptEngineManager re-init, because we destroy ScriptEngineManger in destructor of Director
6. Explicit Director managed by Application and don't delete self at purgeDirector, since it will release at mainLoop and re-init in glView->pollEvents then leak
7. Rename ApplicationProtocol to ApplicationBase
8. TODO:
     a. rename purgeDirector since we don't delete director self at it, maye rename to cleanup
     b. change ShaderCache to new/delete singleton, Managed by DriverBase?
     c. other singleton checks (i.e dependency check)
  • Loading branch information
halx99 committed Jan 27, 2024
1 parent 553f23d commit 5b98d33
Show file tree
Hide file tree
Showing 24 changed files with 76 additions and 37 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- Fix shaders not copying to final build on macOS for non Xcode builds by @smilediver
- Fix `Scheduler::pauseTarget` can't timer targets, reported by @shaniamjad6
- Fix build lua projects with engine prebuilts lib doesn't work, reported by @grif-on
- Fix GUI ScrollView clipping bug and add lua support by @bintester
- Ensure scissor state is set to the correct value prior to a buffer clear by @rh101

## Improvements

Expand Down
2 changes: 0 additions & 2 deletions cmake/Modules/AXBuildHelpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,6 @@ function(ax_setup_app_config app_name)
# compile app shader to ${CMAKE_BINARY_DIR}/runtime/axslc/custom/
ax_target_compile_shaders(${app_name} FILES ${app_shaders} CUSTOM)
source_group("Source Files/Source/shaders" FILES ${app_shaders})
else()
message(STATUS "No shader found in ${app_shaders_dir}")
endif()

if (IS_DIRECTORY ${GLSLCC_OUT_DIR})
Expand Down
2 changes: 1 addition & 1 deletion core/2d/ActionCoroutine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ bool Coroutine::moveNext() const
//
ActionCoroutine* ActionCoroutine::create(const std::function<Coroutine()>& function)
{
auto ret = new (std::nothrow) ActionCoroutine();
auto ret = new ActionCoroutine();
if (ret && ret->initWithCoroutine(function))
{
ret->autorelease();
Expand Down
17 changes: 13 additions & 4 deletions core/base/Director.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,19 @@ Director* Director::getInstance()
{
if (!s_SharedDirector)
{
s_SharedDirector = new Director;
s_SharedDirector = new Director();
AXASSERT(s_SharedDirector, "FATAL: Not enough memory");
s_SharedDirector->init();
}

return s_SharedDirector;
}

void Director::destroyInstance()
{
AX_SAFE_DELETE(s_SharedDirector);
}

Director::Director() {}

bool Director::init()
Expand Down Expand Up @@ -193,9 +198,15 @@ Director::~Director()

s_SharedDirector = nullptr;

backend::DriverBase::destroyInstance();
QuadCommand::destroyIsolatedIndices();

#if AX_ENABLE_SCRIPT_BINDING
ScriptEngineManager::destroyInstance();
#endif

/** clean auto release pool. */
PoolManager::destroyInstance();
}

void Director::setDefaultValues()
Expand Down Expand Up @@ -1044,6 +1055,7 @@ void Director::reset()
SpriteFrameCache::destroyInstance();
FileUtils::destroyInstance();
AsyncTaskPool::destroyInstance();
backend::ProgramStateRegistry::destroyInstance();
backend::ProgramManager::destroyInstance();

// axmol specific data structures
Expand All @@ -1066,9 +1078,6 @@ void Director::purgeDirector()
_glView = nullptr;
}

// delete Director
release();

#if AX_TARGET_PLATFORM == AX_PLATFORM_IOS || AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID
utils::killCurrentProcess();
#endif
Expand Down
3 changes: 2 additions & 1 deletion core/base/Director.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Console;
Since the Director is a singleton, the standard way to use it is by calling:
_ Director::getInstance()->methodName();
*/
class AX_DLL Director : public Ref
class AX_DLL Director
{
public:
/** Director will trigger an event before set next scene. */
Expand Down Expand Up @@ -123,6 +123,7 @@ class AX_DLL Director : public Ref
* @js _getInstance
*/
static Director* getInstance();
static void destroyInstance();

/**
* @js ctor
Expand Down
11 changes: 11 additions & 0 deletions core/platform/ApplicationBase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "ApplicationBase.h"
#include "base/Director.h"

NS_AX_BEGIN

ApplicationBase::~ApplicationBase()
{
Director::destroyInstance();
}

NS_AX_END
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

#ifndef __AX_APPLICATION_PROTOCOL_H__
#define __AX_APPLICATION_PROTOCOL_H__
#ifndef __AX_APPLICATION_BASE_H__
#define __AX_APPLICATION_BASE_H__

#include "platform/PlatformMacros.h"
#include "base/AutoreleasePool.h"
Expand All @@ -38,7 +38,7 @@ NS_AX_BEGIN
* @{
*/

class AX_DLL ApplicationProtocol
class AX_DLL ApplicationBase
{
public:
/** Since WINDOWS and ANDROID are defined as macros, we could not just use these keywords in enumeration(Platform).
Expand All @@ -65,11 +65,7 @@ class AX_DLL ApplicationProtocol
* @js NA
* @lua NA
*/
virtual ~ApplicationProtocol()
{
/** clean auto release pool. */
PoolManager::destroyInstance();
}
virtual ~ApplicationBase();

/**
* @brief Implement Director and Scene init code here.
Expand Down Expand Up @@ -152,6 +148,8 @@ class AX_DLL ApplicationProtocol
virtual bool openURL(std::string_view url) = 0;
};

using ApplicationProtocol = ApplicationBase;

// end of platform group
/** @} */

Expand Down
3 changes: 2 additions & 1 deletion core/platform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ endif()
set(_AX_PLATFORM_HEADER
${_AX_PLATFORM_SPECIFIC_HEADER}
platform/Application.h
platform/ApplicationProtocol.h
platform/ApplicationBase.h
platform/Common.h
platform/Device.h
platform/FileUtils.h
Expand All @@ -194,4 +194,5 @@ set(_AX_PLATFORM_SRC
platform/FileUtils.cpp
platform/Image.cpp
platform/FileStream.cpp
platform/ApplicationBase.cpp
)
4 changes: 2 additions & 2 deletions core/platform/android/Application-android.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ THE SOFTWARE.
#pragma once

#include "platform/Common.h"
#include "platform/ApplicationProtocol.h"
#include "platform/ApplicationBase.h"

NS_AX_BEGIN

class AX_DLL Application : public ApplicationProtocol
class AX_DLL Application : public ApplicationBase
{
public:
/**
Expand Down
2 changes: 1 addition & 1 deletion core/platform/android/EnhanceAPI-android.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#pragma once

#include "platform/Common.h"
#include "platform/ApplicationProtocol.h"
#include "platform/ApplicationBase.h"

NS_AX_BEGIN

Expand Down
4 changes: 2 additions & 2 deletions core/platform/ios/Application-ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ THE SOFTWARE.
#pragma once

#include "platform/Common.h"
#include "platform/ApplicationProtocol.h"
#include "platform/ApplicationBase.h"

NS_AX_BEGIN

class AX_DLL Application : public ApplicationProtocol
class AX_DLL Application : public ApplicationBase
{
public:
/**
Expand Down
4 changes: 2 additions & 2 deletions core/platform/linux/Application-linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ THE SOFTWARE.
#pragma once

#include "platform/Common.h"
#include "platform/ApplicationProtocol.h"
#include "platform/ApplicationBase.h"
#include <string>
#include <chrono>

NS_AX_BEGIN
class Rect;

class Application : public ApplicationProtocol
class Application : public ApplicationBase
{
public:
/**
Expand Down
4 changes: 2 additions & 2 deletions core/platform/mac/Application-mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ THE SOFTWARE.
#pragma once

#include "platform/Common.h"
#include "platform/ApplicationProtocol.h"
#include "platform/ApplicationBase.h"
#include <string>
#include <chrono>

NS_AX_BEGIN

class AX_DLL Application : public ApplicationProtocol
class AX_DLL Application : public ApplicationBase
{
public:
/**
Expand Down
4 changes: 2 additions & 2 deletions core/platform/wasm/Application-wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ THE SOFTWARE.
#if AX_TARGET_PLATFORM == AX_PLATFORM_WASM

#include "platform/Common.h"
#include "platform/ApplicationProtocol.h"
#include "platform/ApplicationBase.h"
#include <string>

NS_AX_BEGIN
class Rect;

class Application : public ApplicationProtocol
class Application : public ApplicationBase
{
public:
/**
Expand Down
1 change: 1 addition & 0 deletions core/platform/win32/Application-win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ int Application::run()
}
glView->release();


return 0;
}

Expand Down
4 changes: 2 additions & 2 deletions core/platform/win32/Application-win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ THE SOFTWARE.

#include "platform/StdC.h"
#include "platform/Common.h"
#include "platform/ApplicationProtocol.h"
#include "platform/ApplicationBase.h"
#include <string>

NS_AX_BEGIN

class AX_DLL Application : public ApplicationProtocol
class AX_DLL Application : public ApplicationBase
{
public:
/**
Expand Down
4 changes: 2 additions & 2 deletions core/platform/winrt/Application-winrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ THE SOFTWARE.

# include "platform/StdC.h"
# include "platform/Common.h"
# include "platform/ApplicationProtocol.h"
# include "platform/ApplicationBase.h"
# include "platform/winrt/InputEvent.h"
# include <string>
# include <functional>

NS_AX_BEGIN

class AX_DLL Application : public ApplicationProtocol
class AX_DLL Application : public ApplicationBase
{
public:
Application();
Expand Down
6 changes: 6 additions & 0 deletions core/renderer/QuadCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ NS_AX_BEGIN
int QuadCommand::__indexCapacity = -1;
uint16_t* QuadCommand::__indices = nullptr;

void QuadCommand::destroyIsolatedIndices()
{
AX_SAFE_DELETE_ARRAY(__indices);
__indexCapacity = -1;
}

QuadCommand::QuadCommand() : _indexSize(-1), _ownedIndices() {}

QuadCommand::~QuadCommand()
Expand Down
2 changes: 2 additions & 0 deletions core/renderer/QuadCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class AX_DLL QuadCommand : public TrianglesCommand
const Mat4& mv,
uint32_t flags);

static void destroyIsolatedIndices();

protected:
void reIndex(int indices);

Expand Down
1 change: 1 addition & 0 deletions core/renderer/backend/DriverBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class AX_DLL DriverBase : public ax::Ref
* Returns a shared instance of the DriverBase.
*/
static DriverBase* getInstance();
static void destroyInstance();

virtual ~DriverBase() = default;

Expand Down
2 changes: 1 addition & 1 deletion core/renderer/backend/ShaderCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ NS_AX_BACKEND_BEGIN
/**
* Create and reuse shader module.
*/
class AX_DLL ShaderCache : public Ref
class AX_DLL ShaderCache
{
public:
static ShaderCache* getInstance();
Expand Down
11 changes: 8 additions & 3 deletions core/renderer/backend/metal/DriverMTL.mm
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,15 @@ bool supportS3TC(FeatureSet featureSet)

DriverBase* DriverBase::getInstance()
{
if (!DriverBase::_instance)
DriverBase::_instance = new DriverMTL();
if (!_instance)
_instance = new DriverMTL();

return DriverBase::_instance;
return _instance;
}

void DriverBase::destroyInstance()
{
AX_SAFE_DELETE(_instance);
}

void DriverMTL::setCAMetalLayer(CAMetalLayer* metalLayer)
Expand Down
4 changes: 4 additions & 0 deletions core/renderer/backend/opengl/DriverGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ DriverBase* DriverBase::getInstance()
return _instance;
}

void DriverBase::destroyInstance() {
AX_SAFE_DELETE(_instance);
}

DriverGL::DriverGL()
{
/// driver info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ cc.UNIFORM_SAMPLER_S = 'AX_Texture0'
cc.UNIFORM_SIN_TIME_S = 'AX_SinTime'
cc.UNIFORM_TIME_S = 'AX_Time'

-- refer to: ApplicationProtocol.h: enum class Platform
-- refer to: ApplicationBase.h: enum class Platform
cc.PLATFORM_UNKNOWN = 0
cc.PLATFORM_WIN32 = 1
cc.PLATFORM_WINUWP = 2
Expand Down

0 comments on commit 5b98d33

Please sign in to comment.