[NUI] fix testhub fail, Add Dispose() in BaseHandle.Reset() where the body was delete...
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Common / BaseHandle.cs
1 /*
2  * Copyright(c) 2022 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 using System;
18 using System.ComponentModel;
19 using System.Runtime.CompilerServices;
20 using Tizen.NUI.Binding;
21 using global::System.Diagnostics;
22
23 namespace Tizen.NUI
24 {
25     /// <summary>
26     /// BaseHandle is a handle to an internal Dali resource.
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     public class BaseHandle : Element, global::System.IDisposable
30     {
31         /// <summary>
32         /// swigCMemOwn
33         /// </summary>
34         /// <since_tizen> 3 </since_tizen>
35         [Obsolete("Deprecated in API9, will be removed in API11, Use SwigCMemOwn")]
36         [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "<Pending>")]
37         protected bool swigCMemOwn;
38
39         /// <summary>
40         /// The flag to check if it is already disposed of.
41         /// </summary>
42         /// <since_tizen> 3 </since_tizen>
43         [Obsolete("Deprecated in API9, will be removed in API11, Use Disposed")]
44         [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "<Pending>")]
45         protected bool disposed = false;
46
47         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
48         private global::System.Runtime.InteropServices.HandleRef swigCPtrCopy;
49         private bool registerMe;
50
51         //The flag to check who called Dispose(). (By User or DisposeQueue)
52         private bool isDisposeQueued = false;
53
54         /// <summary>
55         /// Create an instance of BaseHandle.
56         /// </summary>
57         /// <since_tizen> 3 </since_tizen>
58         public BaseHandle() : this(Interop.BaseHandle.NewBaseHandle())
59         {
60             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
61         }
62
63         /// <summary>
64         /// Create an instance of BaseHandle.
65         /// </summary>
66         /// <param name="handle">The BaseHandle instance.</param>
67         /// <since_tizen> 3 </since_tizen>
68         public BaseHandle(BaseHandle handle) : this(Interop.BaseHandle.NewBaseHandle(BaseHandle.getCPtr(handle)))
69         {
70             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
71         }
72
73         internal BaseHandle(global::System.IntPtr cPtr, bool cMemoryOwn)
74         {
75             //to catch derived classes dali native exceptions
76             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
77
78             DebugFileLogging.Instance.WriteLog($"BaseHandle.contructor with cMemeryOwn:{cMemoryOwn} START");
79
80             registerMe = swigCMemOwn = cMemoryOwn;
81             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
82             // using copy constructor to create another native handle so Registry.Unregister works fine.
83             swigCPtrCopy = new global::System.Runtime.InteropServices.HandleRef(this, Interop.BaseHandle.NewBaseHandle(swigCPtr));
84             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
85
86             if (registerMe)
87             {
88                 // Register this instance of BaseHandle in the registry.
89                 Registry.Register(this);
90             }
91
92             disposeDebuggingCtor();
93             DebugFileLogging.Instance.WriteLog($" BaseHandle.contructor with cMemeryOwn END");
94             DebugFileLogging.Instance.WriteLog($"=============================");
95         }
96
97         internal BaseHandle(global::System.IntPtr cPtr)
98         {
99             DebugFileLogging.Instance.WriteLog($"BaseHandle.contructor START");
100
101             registerMe = swigCMemOwn = true;
102
103             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
104
105             // using copy constructor to create another native handle so Registry.Unregister works fine.
106             swigCPtrCopy = new global::System.Runtime.InteropServices.HandleRef(this, Interop.BaseHandle.NewBaseHandle(SwigCPtr));
107             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
108
109             if (registerMe)
110             {
111                 // Register this instance of BaseHandle in the registry.
112                 Registry.Register(this);
113             }
114
115             disposeDebuggingCtor();
116             DebugFileLogging.Instance.WriteLog($"BaseHandle.contructor END");
117             DebugFileLogging.Instance.WriteLog($"=============================");
118         }
119
120         /// <summary>
121         /// Dispose.
122         /// </summary>
123         /// <since_tizen> 3 </since_tizen>
124         // following this guide: https://docs.microsoft.com/ko-kr/dotnet/fundamentals/code-analysis/quality-rules/ca1063?view=vs-2019 (CA1063)
125         ~BaseHandle() => Dispose(false);
126
127         /// <summary>
128         /// Event when a property is set.
129         /// </summary>
130         /// <since_tizen> 5 </since_tizen>
131         /// <seealso cref="BindableObject.PropertyChanged"/>
132         [Obsolete("Deprecated in API9, will be removed in API11, Use BindableObject.PropertyChanged instead.")]
133         public event PropertyChangedEventHandler PropertySet;
134
135         internal global::System.Runtime.InteropServices.HandleRef GetBaseHandleCPtrHandleRef
136         {
137             get
138             {
139                 return swigCPtrCopy;
140             }
141         }
142
143         /// <summary>
144         /// Returns the bool value true to indicate that an operand is true and returns false otherwise.
145         /// </summary>
146         /// <since_tizen> 3 </since_tizen>
147         public static bool operator true(BaseHandle handle)
148         {
149             // if the C# object is null, return false
150             if (BaseHandle.ReferenceEquals(handle, null))
151             {
152                 return false;
153             }
154             // returns true if the handle has a body, false otherwise
155             return handle.HasBody();
156         }
157
158         /// <summary>
159         /// Returns the bool false  to indicate that an operand is false and returns true otherwise.
160         /// </summary>
161         /// <since_tizen> 3 </since_tizen>
162         public static bool operator false(BaseHandle handle)
163         {
164             // if the C# object is null, return true
165             if (BaseHandle.ReferenceEquals(handle, null))
166             {
167                 return true;
168             }
169             return !handle.HasBody();
170         }
171
172         /// <summary>
173         /// Explicit conversion from Handle to bool.
174         /// </summary>
175         /// <since_tizen> 3 </since_tizen>
176         public static explicit operator bool(BaseHandle handle)
177         {
178             // if the C# object is null, return false
179             if (BaseHandle.ReferenceEquals(handle, null))
180             {
181                 return false;
182             }
183             // returns true if the handle has a body, false otherwise
184             return handle.HasBody();
185         }
186
187         /// <summary>
188         /// Equality operator
189         /// </summary>
190         /// <since_tizen> 3 </since_tizen>
191         public static bool operator ==(BaseHandle x, BaseHandle y)
192         {
193             // if the C# objects are the same return true
194             if (BaseHandle.ReferenceEquals(x, y))
195             {
196                 return true;
197             }
198             if (!BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
199             {
200                 // drop into native code to see if both handles point to the same body
201                 return x.IsEqual(y);
202             }
203
204             if (BaseHandle.ReferenceEquals(x, null) && !BaseHandle.ReferenceEquals(y, null))
205             {
206                 if (y.HasBody()) return false;
207                 else return true;
208             }
209             if (!BaseHandle.ReferenceEquals(x, null) && BaseHandle.ReferenceEquals(y, null))
210             {
211                 if (x.HasBody()) return false;
212                 else return true;
213             }
214
215             return false;
216         }
217
218         /// <summary>
219         /// Inequality operator. Returns Null if either operand is Null
220         /// </summary>
221         /// <since_tizen> 3 </since_tizen>
222         public static bool operator !=(BaseHandle x, BaseHandle y)
223         {
224             return !(x == y);
225         }
226
227         /// <summary>
228         /// Logical AND operator.<br />
229         /// It's possible when doing a  operator this function (opBitwiseAnd) is never called due to short circuiting.<br />
230         /// </summary>
231         /// <since_tizen> 3 </since_tizen>
232         public static BaseHandle operator &(BaseHandle x, BaseHandle y)
233         {
234             if (x == y)
235             {
236                 return x;
237             }
238             return null;
239         }
240
241         /// <summary>
242         /// Logical OR operator for ||.<br />
243         /// It's possible when doing a || this function (opBitwiseOr) is never called due to short circuiting.<br />
244         /// </summary>
245         /// <since_tizen> 3 </since_tizen>
246         public static BaseHandle operator |(BaseHandle x, BaseHandle y)
247         {
248             if (!BaseHandle.ReferenceEquals(x, null) || !BaseHandle.ReferenceEquals(y, null))
249             {
250                 if (x != null && x.HasBody())
251                 {
252                     return x;
253                 }
254                 if (y != null && y.HasBody())
255                 {
256                     return y;
257                 }
258                 return null;
259             }
260             return null;
261         }
262
263         /// <summary>
264         /// Logical ! operator
265         /// </summary>
266         /// <since_tizen> 3 </since_tizen>
267         public static bool operator !(BaseHandle x)
268         {
269             // if the C# object is null, return true
270             if (BaseHandle.ReferenceEquals(x, null))
271             {
272                 return true;
273             }
274             if (x.HasBody())
275             {
276                 return false;
277             }
278             return true;
279         }
280
281         /// <summary>
282         /// Equals
283         /// </summary>
284         /// <param name="o">The object should be compared.</param>
285         /// <returns>True if equal.</returns>
286         /// <since_tizen> 5 </since_tizen>
287         public override bool Equals(object o)
288         {
289             return base.Equals(o);
290         }
291
292         /// <summary>
293         /// Gets the hash code of this baseHandle.
294         /// </summary>
295         /// <returns>The hash code.</returns>
296         /// <since_tizen> 5 </since_tizen>
297         public override int GetHashCode()
298         {
299             return base.GetHashCode();
300         }
301
302         /// <summary>
303         /// Dispose.
304         /// </summary>
305         /// <since_tizen> 3 </since_tizen>
306         public void Dispose()
307         {
308             if (isDisposeQueued)
309             {
310                 Dispose(DisposeTypes.Implicit);
311             }
312             else
313             {
314                 Dispose(true);
315             }
316             GC.SuppressFinalize(this);
317         }
318
319         /// <summary>
320         /// Hidden API (Inhouse API).
321         /// Dispose. 
322         /// </summary>
323         /// <remarks>
324         /// Following the guide of https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose.
325         /// This will replace "protected virtual void Dispose(DisposeTypes type)" which is exactly same in functionality.
326         /// </remarks>
327         /// <param name="disposing">true in order to free managed objects</param>
328         // Protected implementation of Dispose pattern.
329         [EditorBrowsable(EditorBrowsableState.Never)]
330         protected virtual void Dispose(bool disposing)
331         {
332             if (disposed)
333             {
334                 return;
335             }
336
337             if (disposing)
338             {
339                 // TODO: dispose managed state (managed objects).
340                 // Explicit call. user calls Dispose()
341
342                 //Throw exception if Dispose() is called in separate thread.
343                 if (!Window.IsInstalled())
344                 {
345                     var process = global::System.Diagnostics.Process.GetCurrentProcess().Id;
346                     var thread = global::System.Threading.Thread.CurrentThread.ManagedThreadId;
347                     var me = this.GetType().FullName;
348
349                     DebugFileLogging.Instance.WriteLog("[NUI][BaseHandle] This API called from separate thread. This API must be called from MainThread. \n" +
350                         $" process:{process} thread:{thread}, disposing:{disposing}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me}\n");
351
352                     Tizen.Log.Fatal("NUI", $"[NUI][BaseHandle] This API called from separate thread. This API must be called from MainThread. \n" +
353                         $" process:{process} thread:{thread}, disposing:{disposing}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me}\n");
354
355                     //to fix ArtApp black screen issue. this will be enabled after talking with ArtApp team and fixing it.
356                     // throw new global::System.InvalidOperationException("[NUI][BaseHandle] This API called from separate thread. This API must be called from MainThread. \n" +
357                     //     $" process:{process} thread:{thread}, disposing:{disposing}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me}\n");
358                 }
359
360                 if (isDisposeQueued)
361                 {
362                     DebugFileLogging.Instance.WriteLog($"should not be here! (dead code) this will be removed!");
363
364                     Tizen.Log.Fatal("NUI", $"[NUI] should not be here! (dead code) this will be removed!");
365
366                     //to fix ArtApp black screen issue. this will be enabled after talking with ArtApp team and fixing it.
367                     // throw new global::System.Exception($"[NUI] should not be here! (dead code) this will be removed!");
368                     Dispose(DisposeTypes.Implicit);
369                 }
370                 else
371                 {
372                     Dispose(DisposeTypes.Explicit);
373                 }
374             }
375             else
376             {
377                 // Implicit call. user doesn't call Dispose(), so this object is added into DisposeQueue to be disposed automatically.
378                 if (!isDisposeQueued)
379                 {
380                     isDisposeQueued = true;
381                     DisposeQueue.Instance.Add(this);
382                 }
383             }
384
385             // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
386             // TODO: set large fields to null.
387         }
388
389
390         /// <summary>
391         /// Performs an action on this object with the given action name and attributes.
392         /// </summary>
393         /// <param name="actionName">The command for the action.</param>
394         /// <param name="attributes">The list of attributes for the action.</param>
395         /// <returns>The action is performed by the object or not.</returns>
396         /// <since_tizen> 3 </since_tizen>
397         public bool DoAction(string actionName, PropertyMap attributes)
398         {
399             bool ret = Interop.BaseHandle.DoAction(swigCPtrCopy, actionName, PropertyMap.getCPtr(attributes));
400             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
401             return ret;
402         }
403
404         /// <summary>
405         /// Returns the type name for the Handle.<br />
406         /// Will return an empty string if the typename does not exist. This will happen for types that
407         /// have not registered with type-registry.
408         /// </summary>
409         /// <returns>The type name. Empty string if the typename does not exist.</returns>
410         /// <since_tizen> 3 </since_tizen>
411         public string GetTypeName()
412         {
413             string ret = Interop.BaseHandle.GetTypeName(swigCPtrCopy);
414             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
415             return ret;
416         }
417
418         /// <summary>
419         /// Returns the type info for the Handle.<br />
420         /// </summary>
421         /// <param name="info">The type information.</param>
422         /// <returns>True If get the type info.</returns>
423         /// <since_tizen> 3 </since_tizen>
424         public bool GetTypeInfo(Tizen.NUI.TypeInfo info)
425         {
426             bool ret = Interop.BaseHandle.GetTypeInfo(swigCPtrCopy, Tizen.NUI.TypeInfo.getCPtr(info));
427             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
428             return ret;
429         }
430
431         /// <summary>
432         /// Resets the handle.
433         /// </summary>
434         /// <since_tizen> 3 </since_tizen>
435         /// <remark>
436         /// This will be deprecated, please use Dispose() instead.
437         /// </remark>
438         public void Reset()
439         {
440             this.Dispose();
441             NUILog.Error("[ERROR] This(BaseHandle.Reset) will be deprecated, please use Dispose() instead!");
442         }
443
444         /// <summary>
445         /// To check the BaseHandle instance is equal or not.
446         /// </summary>
447         /// <param name="rhs">The baseHandle instance.</param>
448         /// <returns>True If equal.</returns>
449         /// <since_tizen> 3 </since_tizen>
450         public bool EqualTo(BaseHandle rhs)
451         {
452             bool ret = Interop.BaseHandle.EqualTo(swigCPtrCopy, BaseHandle.getCPtr(rhs));
453             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
454             return ret;
455         }
456
457         /// <summary>
458         /// To check the BaseHandle instance is equal or not.
459         /// </summary>
460         /// <param name="rhs">The baseHandle instance.</param>
461         /// <returns>True If not equal.</returns>
462         /// <since_tizen> 3 </since_tizen>
463         public bool NotEqualTo(BaseHandle rhs)
464         {
465             bool ret = Interop.BaseHandle.NotEqualTo(swigCPtrCopy, BaseHandle.getCPtr(rhs));
466             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
467             return ret;
468         }
469
470         /// <summary>
471         /// To check the BaseHandle instance has body or not.
472         /// </summary>
473         /// <returns>True If the baseHandle instance has body.</returns>
474         /// <since_tizen> 3 </since_tizen>
475         public bool HasBody()
476         {
477             if (swigCPtrCopy.Handle == IntPtr.Zero)
478             {
479                 return false;
480             }
481
482             if (disposed == true)
483             {
484                 return false;
485             }
486             bool ret = Interop.BaseHandle.HasBody(swigCPtrCopy);
487             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
488             return ret;
489         }
490
491         /// <summary>
492         /// To check the BaseHandle instance is equal or not.
493         /// </summary>
494         /// <param name="rhs">The baseHandle instance.</param>
495         /// <returns>True If equal.</returns>
496         /// <since_tizen> 3 </since_tizen>
497         public bool IsEqual(BaseHandle rhs)
498         {
499             if (disposed == true || rhs == null || !rhs.HasBody())
500             {
501                 return false;
502             }
503
504             bool ret = Interop.BaseHandle.IsEqual(swigCPtrCopy, BaseHandle.getCPtr(rhs));
505             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
506             return ret;
507         }
508
509         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(BaseHandle obj)
510         {
511             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtrCopy;
512         }
513
514         internal void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
515         {
516             PropertySet?.Invoke(this, new PropertyChangedEventArgs(propertyName));
517         }
518
519         /// <summary>
520         /// Dispose.
521         /// </summary>
522         /// <since_tizen> 3 </since_tizen>
523         protected virtual void Dispose(DisposeTypes type)
524         {
525             if (disposed)
526             {
527                 return;
528             }
529
530             DebugFileLogging.Instance.WriteLog($"BaseHandle.Dispose({type}) START");
531
532             if (type == DisposeTypes.Explicit)
533             {
534                 //Called by User
535                 //Release your own managed resources here.
536                 //You should release all of your own disposable objects here.
537
538             }
539
540             //Release your own unmanaged resources here.
541             //You should not access any managed member here except static instance.
542             //because the execution order of Finalizes is non-deterministic.
543
544             //Unreference this instance from Registry.
545             if (registerMe)
546             {
547                 Registry.Unregister(this);
548             }
549
550             disposeDebuggingDispose(type);
551
552             if (SwigCPtr.Handle != IntPtr.Zero)
553             {
554                 if (swigCMemOwn)
555                 {
556                     swigCMemOwn = false;
557                     ReleaseSwigCPtr(SwigCPtr);
558                 }
559                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
560             }
561             else
562             {
563                 var me = this.GetType().FullName;
564                 DebugFileLogging.Instance.WriteLog($"[ERR] SwigCPtr is NULL, need to check! me:{me}");
565                 Log.Error("NUI", $"[ERR] SwigCPtr is NULL, need to check! me:{me}");
566             }
567
568             if (swigCPtrCopy.Handle != global::System.IntPtr.Zero)
569             {
570                 swigCMemOwn = false;
571                 Interop.BaseHandle.DeleteBaseHandle(swigCPtrCopy);
572                 swigCPtrCopy = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
573             }
574             else
575             {
576                 var me = this.GetType().FullName;
577                 DebugFileLogging.Instance.WriteLog($"[ERR] swigCPtrCopy is NULL, need to check! me:{me}");
578                 Log.Error("NUI", $"[ERR] swigCPtrCopy is NULL, need to check! me:{me}");
579             }
580
581             disposed = true;
582             DebugFileLogging.Instance.WriteLog($"BaseHandle.Dispose({type}) END");
583             DebugFileLogging.Instance.WriteLog($"=============================");
584             NUILog.Debug($"BaseHandle.Dispose({type}) END");
585             NUILog.Debug($"=============================");
586         }
587
588         /// <summary>
589         /// Release swigCPtr.
590         /// </summary>
591         /// <since_tizen> 6 </since_tizen>
592         /// This will not be public opened.
593         [EditorBrowsable(EditorBrowsableState.Never)]
594         protected virtual void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
595         {
596         }
597
598         /// <summary>
599         /// Contains event arguments for the FocusChangeRequested event.
600         /// </summary>
601         [Obsolete("Deprecated in API9; Will be removed in API11.")]
602         public class FocusRequestArgs : EventArgs
603         {
604
605             /// <summary>
606             /// Gets or sets a value that indicates the starting focus state of the element for which a focus change is requested.
607             /// </summary>
608             public bool Focus { get; set; }
609
610             /// <summary>
611             /// Gets or sets a value that indicates the ending focus state of the element for which a focus change is requested.
612             /// </summary>
613             public bool Result { get; set; }
614         }
615
616         internal global::System.Runtime.InteropServices.HandleRef SwigCPtr
617         {
618             get
619             {
620                 if (swigCPtr.Handle == IntPtr.Zero)
621                 {
622                     var process = global::System.Diagnostics.Process.GetCurrentProcess().Id;
623                     var thread = global::System.Threading.Thread.CurrentThread.ManagedThreadId;
624                     var me = this.GetType().FullName;
625
626                     Tizen.Log.Fatal("NUI", $"SwigCPtr Error! NUI's native dali object is already disposed. " +
627                         $"OR the native dali object handle of NUI becomes null! \n" +
628                         $" process:{process} thread:{thread}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me}\n");
629
630                     Tizen.Log.Fatal("NUI", $"[ERROR] back trace!");
631                     global::System.Diagnostics.StackTrace st = new global::System.Diagnostics.StackTrace(true);
632                     for (int i = 0; i < st.FrameCount; i++)
633                     {
634                         global::System.Diagnostics.StackFrame sf = st.GetFrame(i);
635                         Tizen.Log.Fatal("NUI", " Method " + sf.GetMethod());
636                     }
637                     Tizen.Log.Fatal("NUI", "Error! just return here with null swigCPtr! this can cause unknown error or crash in next step");
638
639                     //to fix ArtApp black screen issue. this will be enabled after talking with ArtApp team and fixing it.
640                     // throw new ObjectDisposedException(nameof(SwigCPtr), $"Error! NUI's native dali object is already disposed. " +
641                     //     $"OR the native dali object handle of NUI becomes null! \n" +
642                     //     $" process:{process} thread:{thread}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me}\n");
643                 }
644                 return swigCPtr;
645             }
646         }
647
648         internal bool IsNativeHandleInvalid()
649         {
650             return swigCPtr.Handle == IntPtr.Zero;
651         }
652
653         /// <summary>
654         /// swigCMemOwn
655         /// </summary>
656         [EditorBrowsable(EditorBrowsableState.Never)]
657         protected internal bool SwigCMemOwn => swigCMemOwn;
658
659         /// <summary>
660         /// The flag to check if it is already disposed of.
661         /// </summary>
662         [EditorBrowsable(EditorBrowsableState.Never)]
663         protected internal bool Disposed => disposed;
664
665         /// <summary>
666         /// The flag to check if it is disposed by DisposeQueue.
667         /// </summary>
668         [EditorBrowsable(EditorBrowsableState.Never)]
669         protected internal bool IsDisposeQueued => isDisposeQueued;
670
671         [Conditional("NUI_DISPOSE_DEBUG_ON")]
672         private void disposeDebuggingCtor()
673         {
674             DebugFileLogging.Instance.WriteLog($"type:{GetType()} copyNativeHandle:{swigCPtrCopy.Handle.ToString("X8")}");
675             if (this is BaseComponents.View view)
676             {
677                 DebugFileLogging.Instance.WriteLog($"ID:{view.ID} Name:{view.Name}");
678                 //back trace
679                 global::System.Diagnostics.StackTrace st = new global::System.Diagnostics.StackTrace(true);
680                 for (int i = 0; i < st.FrameCount; i++)
681                 {
682                     global::System.Diagnostics.StackFrame sf = st.GetFrame(i);
683                     DebugFileLogging.Instance.WriteLog($"[{i}] {sf.GetMethod()}");
684                 }
685             }
686         }
687
688         [Conditional("NUI_DISPOSE_DEBUG_ON")]
689         private void disposeDebuggingDispose(DisposeTypes type)
690         {
691             DebugFileLogging.Instance.WriteLog($"swigCMemOwn:{swigCMemOwn} type:{GetType()} copyNativeHandle:{swigCPtrCopy.Handle.ToString("X8")} HasBody:{HasBody()}");
692
693             if (swigCPtr.Handle != IntPtr.Zero && swigCPtrCopy.Handle != IntPtr.Zero && HasBody())
694             {
695                 using var currentProcess = global::System.Diagnostics.Process.GetCurrentProcess();
696                 var process = currentProcess.Id;
697                 var thread = global::System.Threading.Thread.CurrentThread.ManagedThreadId;
698                 var me = this.GetType().FullName;
699                 var daliId = "unknown";
700                 var hash = this.GetType().GetHashCode();
701                 var name = "unknown";
702
703                 if (this is BaseComponents.View)
704                 {
705                     daliId = Interop.Actor.GetId(swigCPtrCopy).ToString();
706                     name = Interop.Actor.GetName(swigCPtrCopy);
707                     BaseObject tmp = new BaseObject(Interop.BaseHandle.GetBaseObject(swigCPtrCopy), false);
708                     var refCnt = tmp.ReferenceCount();
709                     tmp.Dispose();
710                     if (refCnt > 2)
711                     {
712                         DebugFileLogging.Instance.WriteLog($"[ERR] reference count is over than 2. Could be a memory leak. Need to check! \n" +
713                             $" process:{process} thread:{thread}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me} \n" +
714                             $" disposeType:{type}, name:{name}, daliID:{daliId}, hash:{hash}, refCnt:{refCnt}");
715
716                         Log.Error("NUI", $"[ERR] reference count is over than 2. Could be a memory leak. Need to check! \n" +
717                             $" process:{process} thread:{thread}, isDisposed:{this.disposed}, isDisposeQueued:{this.isDisposeQueued}, me:{me} \n" +
718                             $" disposeType:{type}, name:{name}, daliID:{daliId}, hash:{hash}, refCnt:{refCnt}");
719                     }
720                 }
721             }
722         }
723
724     }
725 }