From c0fbf1a3e42a65664a1bca904b28baba0b382b99 Mon Sep 17 00:00:00 2001 From: "Eunki, Hong" Date: Tue, 26 Sep 2023 15:25:43 +0900 Subject: [PATCH] Let registry throw exception at error case Let we throw exception if same object try to register. Moreover, let we print more useful callstack if we got unmatched case. Signed-off-by: Eunki, Hong --- .../src/internal/Common/DaliEnumConstants.cs | 2 +- src/Tizen.NUI/src/internal/Common/Registry.cs | 41 +++++++++++++++++++++- src/Tizen.NUI/src/public/Common/BaseHandle.cs | 4 +-- src/Tizen.NUI/src/public/Utility/Timer.cs | 6 ++-- 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/Tizen.NUI/src/internal/Common/DaliEnumConstants.cs b/src/Tizen.NUI/src/internal/Common/DaliEnumConstants.cs index b97b81e..c24f9e1 100755 --- a/src/Tizen.NUI/src/internal/Common/DaliEnumConstants.cs +++ b/src/Tizen.NUI/src/internal/Common/DaliEnumConstants.cs @@ -251,7 +251,7 @@ namespace Tizen.NUI for (int i = 0; i < st.FrameCount; i++) { global::System.Diagnostics.StackFrame sf = st.GetFrame(i); - Tizen.Log.Error("NUI", " Method " + sf.GetMethod()); + Tizen.Log.Error("NUI", " Method " + sf.GetMethod() + ":" + sf.GetFileName() + ":" + sf.GetFileLineNumber()); } } } diff --git a/src/Tizen.NUI/src/internal/Common/Registry.cs b/src/Tizen.NUI/src/internal/Common/Registry.cs index ecceb81..7604d2e 100755 --- a/src/Tizen.NUI/src/internal/Common/Registry.cs +++ b/src/Tizen.NUI/src/internal/Common/Registry.cs @@ -60,7 +60,46 @@ namespace Tizen.NUI if (Instance._controlMap.TryAdd(refCptr, new WeakReference(baseHandle, false)) != true) { - NUILog.Debug("refCptr is already exist! OR something wrong!"); + WeakReference weakReference; + if(Instance._controlMap.TryGetValue(refCptr, out weakReference)) + { + if (weakReference == null) + { + Tizen.Log.Error("NUI", $"Something Wrong! weakReference is null\n"); + throw new System.InvalidOperationException("Error! NUI Registry weakReference should not be NULL!"); + } + var target = weakReference.Target; + + BaseHandle ret = target as BaseHandle; + if (ret != null) + { + Tizen.Log.Error("NUI", $"refCptr is already exist! input type:{baseHandle.GetType()}, registed type:{ret.GetType()}, refCptr:{refCptr.ToString("X8")}\n"); + throw new System.InvalidOperationException("refCptr is already exist!"); + } + else if(target == null) + { + // Special case. If WeakReference.Target is null, it might be disposed by GC. just Add forcely. + if (Instance._controlMap.TryRemove(refCptr, out weakReference) != true) + { + Tizen.Log.Error("NUI", $"Something Wrong when we try to remove null target\n"); + } + if (Instance._controlMap.TryAdd(refCptr, new WeakReference(baseHandle, false)) != true) + { + Tizen.Log.Error("NUI", $"Something Wrong when we try to replace null target\n"); + throw new System.InvalidOperationException("refCptr register failed"); + } + } + else + { + Tizen.Log.Error("NUI", $"Something Wrong!! target is not BaseHandle! target.GetType() : {target?.GetType()}\n"); + throw new System.InvalidOperationException("refCptr is already exist, but not a BaseHandle!"); + } + } + else + { + Tizen.Log.Error("NUI", $"refCptr is already exist! OR something Wrong!!!\n"); + throw new System.InvalidOperationException("refCptr is already exist, but fail to get handle!"); + } } NUILog.Debug($"[Registry] Register! type:{baseHandle.GetType()} count:{Instance._controlMap.Count} copyNativeHandle:{baseHandle.GetBaseHandleCPtrHandleRef.Handle.ToString("X8")}"); diff --git a/src/Tizen.NUI/src/public/Common/BaseHandle.cs b/src/Tizen.NUI/src/public/Common/BaseHandle.cs index cca7f46..3b2bfaf 100755 --- a/src/Tizen.NUI/src/public/Common/BaseHandle.cs +++ b/src/Tizen.NUI/src/public/Common/BaseHandle.cs @@ -617,7 +617,7 @@ namespace Tizen.NUI for (int i = 0; i < st.FrameCount; i++) { global::System.Diagnostics.StackFrame sf = st.GetFrame(i); - Tizen.Log.Fatal("NUI", " Method " + sf.GetMethod()); + Tizen.Log.Fatal("NUI", " Method " + sf.GetMethod() + ":" + sf.GetFileName() + ":" + sf.GetFileLineNumber()); } Tizen.Log.Fatal("NUI", "Error! just return here with null swigCPtr! this can cause unknown error or crash in next step"); @@ -665,7 +665,7 @@ namespace Tizen.NUI for (int i = 0; i < st.FrameCount; i++) { global::System.Diagnostics.StackFrame sf = st.GetFrame(i); - DebugFileLogging.Instance.WriteLog($"[{i}] {sf.GetMethod()}"); + DebugFileLogging.Instance.WriteLog($"[{i}] {sf.GetMethod()}:{sf.GetFileName()}:{sf.GetFileLineNumber()}"); } } } diff --git a/src/Tizen.NUI/src/public/Utility/Timer.cs b/src/Tizen.NUI/src/public/Utility/Timer.cs index 0bb293d..b17c72c 100755 --- a/src/Tizen.NUI/src/public/Utility/Timer.cs +++ b/src/Tizen.NUI/src/public/Utility/Timer.cs @@ -136,7 +136,7 @@ namespace Tizen.NUI for (int i = 0; i < st.FrameCount; i++) { StackFrame sf = st.GetFrame(i); - Tizen.Log.Error("NUI", " Method " + sf.GetMethod()); + Tizen.Log.Error("NUI", " Method " + sf.GetMethod() + ":" + sf.GetFileName() + ":" + sf.GetFileLineNumber()); } } @@ -166,7 +166,7 @@ namespace Tizen.NUI for (int i = 0; i < st.FrameCount; i++) { StackFrame sf = st.GetFrame(i); - Tizen.Log.Error("NUI", " Method " + sf.GetMethod()); + Tizen.Log.Error("NUI", " Method " + sf.GetMethod() + ":" + sf.GetFileName() + ":" + sf.GetFileLineNumber()); } } @@ -197,7 +197,7 @@ namespace Tizen.NUI for (int i = 0; i < st.FrameCount; i++) { StackFrame sf = st.GetFrame(i); - Tizen.Log.Error("NUI", " Method " + sf.GetMethod()); + Tizen.Log.Error("NUI", " Method " + sf.GetMethod() + ":" + sf.GetFileName() + ":" + sf.GetFileLineNumber()); } } -- 2.7.4