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