diff --git a/core/base/Action.hpp b/core/base/Action.hpp index 576c60bd..3f27f08b 100644 --- a/core/base/Action.hpp +++ b/core/base/Action.hpp @@ -29,7 +29,7 @@ namespace easy2d class ActionManager; class Action - : public RefCounter + : public ObjectBase , protected intrusive::ListItem { friend class ActionManager; diff --git a/core/base/Animation.h b/core/base/Animation.h index 80ddfbfa..054326b4 100644 --- a/core/base/Animation.h +++ b/core/base/Animation.h @@ -25,7 +25,7 @@ namespace easy2d { // 帧动画 class Animation - : public RefCounter + : public ObjectBase { using Images = std::vector< spImage >; diff --git a/core/base/Debuger.cpp b/core/base/Debuger.cpp index f5dc8088..e32bd37c 100644 --- a/core/base/Debuger.cpp +++ b/core/base/Debuger.cpp @@ -43,6 +43,9 @@ namespace easy2d style.wrap = false; style.line_spacing = 18.f; debug_text_->SetStyle(style); + + ObjectBase::__RemoveObjectFromTracingList(this); + ObjectBase::__RemoveObjectFromTracingList(debug_text_.Get()); } DebugerNode::~DebugerNode() @@ -84,6 +87,12 @@ namespace easy2d std::wstringstream ss; ss << "fps=" << frame_time_.size() << std::endl; +#ifdef E2D_DEBUG + + ss << "objects=" << ObjectBase::__GetTracingObjects().size() << std::endl; + +#endif + PROCESS_MEMORY_COUNTERS_EX pmc; GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc)); if (pmc.PrivateUsage > 1024 * 1024) @@ -92,7 +101,7 @@ namespace easy2d ss << "memory=" << pmc.PrivateUsage / 1024 << "Kb"; for (const auto& text : texts_) - ss << text << std::endl; + ss << std::endl << text; debug_text_->SetText(ss.str()); } diff --git a/core/base/Image.h b/core/base/Image.h index 45e27df2..caacf7bf 100644 --- a/core/base/Image.h +++ b/core/base/Image.h @@ -26,7 +26,7 @@ namespace easy2d { // 图片 class Image - : public RefCounter + : public ObjectBase { public: Image(); diff --git a/core/base/Music.h b/core/base/Music.h index 225e8d5b..48e05afb 100644 --- a/core/base/Music.h +++ b/core/base/Music.h @@ -27,7 +27,7 @@ namespace easy2d { // 音乐 class Music - : public RefCounter + : public ObjectBase { public: Music(); diff --git a/core/base/Node.h b/core/base/Node.h index 0490013e..4f3284ff 100644 --- a/core/base/Node.h +++ b/core/base/Node.h @@ -34,7 +34,7 @@ namespace easy2d // 节点 class Node - : public RefCounter + : public ObjectBase , public ActionManager , public TaskManager , protected intrusive::ListItem diff --git a/core/base/ObjectBase.cpp b/core/base/ObjectBase.cpp new file mode 100644 index 00000000..50938cc6 --- /dev/null +++ b/core/base/ObjectBase.cpp @@ -0,0 +1,96 @@ +// Copyright (c) 2016-2018 Easy2D - Nomango +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include "ObjectBase.h" + +namespace easy2d +{ + namespace + { + bool tracing_leaks = true; + std::vector tracing_objects; + } + + ObjectBase::ObjectBase() + : tracing_leak_(false) + { +#ifdef E2D_DEBUG + + ObjectBase::__AddObjectToTracingList(this); + +#endif + } + + ObjectBase::~ObjectBase() + { +#ifdef E2D_DEBUG + + ObjectBase::__RemoveObjectFromTracingList(this); + +#endif + } + + void ObjectBase::StartTracingLeaks() + { + tracing_leaks = true; + } + + void ObjectBase::StopTracingLeaks() + { + tracing_leaks = false; + } + + std::vector const& easy2d::ObjectBase::__GetTracingObjects() + { + return tracing_objects; + } + + void ObjectBase::__AddObjectToTracingList(ObjectBase * obj) + { +#ifdef E2D_DEBUG + + if (tracing_leaks && !obj->tracing_leak_) + { + obj->tracing_leak_ = true; + tracing_objects.push_back(obj); + } + +#endif + } + + void ObjectBase::__RemoveObjectFromTracingList(ObjectBase * obj) + { +#ifdef E2D_DEBUG + + if (tracing_leaks && obj->tracing_leak_) + { + obj->tracing_leak_ = false; + + auto iter = std::find(tracing_objects.begin(), tracing_objects.end(), obj); + if (iter != tracing_objects.end()) + { + tracing_objects.erase(iter); + } + } + +#endif + } + +} diff --git a/core/base/ObjectBase.h b/core/base/ObjectBase.h new file mode 100644 index 00000000..a74d824c --- /dev/null +++ b/core/base/ObjectBase.h @@ -0,0 +1,48 @@ +// Copyright (c) 2016-2018 Easy2D - Nomango +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#pragma once +#include "RefCounter.hpp" + +namespace easy2d +{ + class ObjectBase + : public RefCounter + { + public: + ObjectBase(); + + virtual ~ObjectBase(); + + static void StartTracingLeaks(); + + static void StopTracingLeaks(); + + static std::vector const& __GetTracingObjects(); + + protected: + static void __AddObjectToTracingList(ObjectBase*); + + static void __RemoveObjectFromTracingList(ObjectBase*); + + private: + bool tracing_leak_; + }; +} diff --git a/core/base/Task.h b/core/base/Task.h index 83eab83a..615eb82f 100644 --- a/core/base/Task.h +++ b/core/base/Task.h @@ -30,7 +30,7 @@ namespace easy2d // 定时任务 class Task - : public RefCounter + : public ObjectBase , protected intrusive::ListItem { friend class TaskManager; diff --git a/core/base/Transition.h b/core/base/Transition.h index f157e407..2b38275d 100644 --- a/core/base/Transition.h +++ b/core/base/Transition.h @@ -28,7 +28,7 @@ namespace easy2d // 场景过渡 class Transition - : public RefCounter + : public ObjectBase { friend class Game; diff --git a/core/base/base.hpp b/core/base/base.hpp index 9cd49359..6db1a397 100644 --- a/core/base/base.hpp +++ b/core/base/base.hpp @@ -21,6 +21,7 @@ #pragma once #include "BaseTypes.hpp" #include "RefCounter.hpp" +#include "ObjectBase.h" #include "intrusive/SmartPointer.hpp" #include "d2dres.hpp" diff --git a/core/easy2d.h b/core/easy2d.h index 6417ddd7..973d7210 100644 --- a/core/easy2d.h +++ b/core/easy2d.h @@ -54,6 +54,7 @@ #include "base/intrusive/List.hpp" #include "base/RefCounter.hpp" +#include "base/ObjectBase.h" #include "base/Image.h" #include "base/Node.h" #include "base/Scene.h" @@ -68,6 +69,7 @@ #include "base/Animation.h" #include "base/CallFunc.h" #include "base/Transition.h" +#include "base/Debuger.h" #include "base/KeyEvent.h" #include "base/MouseEvent.h" diff --git a/project/vs2013/Easy2D.vcxproj b/project/vs2013/Easy2D.vcxproj index 533f1680..d41d84fb 100644 --- a/project/vs2013/Easy2D.vcxproj +++ b/project/vs2013/Easy2D.vcxproj @@ -46,6 +46,7 @@ + @@ -97,6 +98,7 @@ + diff --git a/project/vs2013/Easy2D.vcxproj.filters b/project/vs2013/Easy2D.vcxproj.filters index 7b6f223d..d3c7f541 100644 --- a/project/vs2013/Easy2D.vcxproj.filters +++ b/project/vs2013/Easy2D.vcxproj.filters @@ -173,6 +173,9 @@ utils + + base + @@ -306,5 +309,8 @@ utils + + base + \ No newline at end of file diff --git a/project/vs2015/Easy2D.vcxproj b/project/vs2015/Easy2D.vcxproj index fef9c7fc..56456772 100644 --- a/project/vs2015/Easy2D.vcxproj +++ b/project/vs2015/Easy2D.vcxproj @@ -46,6 +46,7 @@ + @@ -97,6 +98,7 @@ + diff --git a/project/vs2015/Easy2D.vcxproj.filters b/project/vs2015/Easy2D.vcxproj.filters index 7b6f223d..d3c7f541 100644 --- a/project/vs2015/Easy2D.vcxproj.filters +++ b/project/vs2015/Easy2D.vcxproj.filters @@ -173,6 +173,9 @@ utils + + base + @@ -306,5 +309,8 @@ utils + + base + \ No newline at end of file diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index 4575fa88..5fc0e0bc 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -46,6 +46,7 @@ + @@ -97,6 +98,7 @@ + diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index 7b6f223d..d3c7f541 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -173,6 +173,9 @@ utils + + base + @@ -306,5 +309,8 @@ utils + + base + \ No newline at end of file