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