Skip to content

Commit d7194f8

Browse files
committed
logging
1 parent df0c682 commit d7194f8

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/Foundation/NSObject2.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2020
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2121
//
22+
#define LOGGING
23+
#define CONSISTENCY_CHECKS
24+
2225
using System.Diagnostics.CodeAnalysis;
2326
using System.Reflection;
2427
using System.Collections.Generic;
@@ -76,6 +79,10 @@ struct NSObjectData {
7679
}
7780

7881
class NSObjectDataHandle : CriticalHandle {
82+
#if LOGGING
83+
static int counter;
84+
public int ID = Interlocked.Increment (ref counter);
85+
#endif
7986
bool invalidated;
8087
public NSObjectDataHandle ()
8188
: base (IntPtr.Zero)
@@ -88,11 +95,17 @@ public NSObjectDataHandle ()
8895
public NSObjectDataHandle (IntPtr handle)
8996
: base (handle)
9097
{
98+
#if LOGGING
99+
Runtime.NSLog ($"NSObjectDataHandle (0x{handle:x}): wrapped existing pointer");
100+
#endif
91101
}
92102

93103
public void Invalidate ()
94104
{
95105
invalidated = true;
106+
#if LOGGING
107+
Runtime.NSLog ($"NSObjectDataHandle.Invalidate (): invalidated 0x{handle:x}");
108+
#endif
96109
}
97110

98111
public unsafe NSObjectData* Data {
@@ -108,10 +121,16 @@ protected override bool ReleaseHandle ()
108121
if (handle != IntPtr.Zero) {
109122
if (invalidated) {
110123
// nothing to do here.
124+
#if LOGGING
125+
Runtime.NSLog ($"NSObjectDataHandle.ReleaseHandle (): NOT released 0x{handle:x}; not owned");
126+
#endif
111127
} else {
112128
unsafe {
113129
NativeMemory.Free ((void*) handle);
114130
}
131+
#if LOGGING
132+
Runtime.NSLog ($"NSObjectDataHandle.ReleaseHandle (): released 0x{handle:x}");
133+
#endif
115134
}
116135
}
117136
handle = IntPtr.Zero;
@@ -169,14 +188,23 @@ unsafe NativeHandle handle {
169188
var rv = AllocateData ().Data;
170189

171190
if (rv is null) {
191+
#if LOGGING
192+
Runtime.NSLog ($"{GetType ().Name}.GetData (): id={objectId} no handle\n{Environment.StackTrace}");
193+
throw new ObjectDisposedException ($"{GetType ().Name}.GetData (): id={objectId} No data???");
194+
#else
172195
// Throwing an exception here is better than returning a null pointer, because that will crash the process when the pointer is dereferenced
173196
// (and none of the callers can do anything useful with a null pointer anyway).
174197
throw new ObjectDisposedException ($"This object (of type {GetType ().Name}) does not have a data pointer anymore, possibly because of a race condition. Please file a bug at https://github.com/dotnet/macios/issues.");
198+
#endif
175199
}
176200

177201
return rv;
178202
}
179203

204+
#if LOGGING
205+
static int objectIdCounter;
206+
int objectId;
207+
#endif
180208
unsafe NSObjectDataHandle AllocateData ()
181209
{
182210
var dh = data_handle;
@@ -191,6 +219,10 @@ unsafe NSObjectDataHandle AllocateData ()
191219
return previousValue;
192220
}
193221

222+
#if LOGGING
223+
objectId = Interlocked.Increment (ref objectIdCounter);
224+
Runtime.NSLog ($"{GetType ().Name}.AllocateData () id={objectId} allocated 0x{((IntPtr) data.Data):x}");
225+
#endif
194226
if (!Runtime.IsCoreCLR) // This condition (and the assignment to __handle_for_mono if applicable) is trimmed away by the linker.
195227
__data_for_mono = data.Data;
196228

@@ -1053,7 +1085,7 @@ void RecreateDataHandle ()
10531085
// else does that, it's quite unlikely this instance will become resurrected a second time.
10541086
var previous_data = data_handle;
10551087
if (previous_data is null) {
1056-
var msg = $"This object (of type {GetType ().Name}) does not have an existing data pointer, possibly because of a race condition. Please file a bug at https://github.com/dotnet/macios/issues.");
1088+
var msg = $"This object (of type {GetType ().Name}) does not have an existing data pointer, possibly because of a race condition. Please file a bug at https://github.com/dotnet/macios/issues.";
10571089
#if CONSISTENCY_CHECKS
10581090
throw new InvalidOperationException (msg);
10591091
#else
@@ -1067,7 +1099,7 @@ void RecreateDataHandle ()
10671099
}
10681100

10691101
if (previous_data.IsInvalid) {
1070-
var msg = $"This object (of type {GetType ().Name}) does not have valid data pointer, possibly because of a race condition. Please file a bug at https://github.com/dotnet/macios/issues.");
1102+
var msg = $"This object (of type {GetType ().Name}) does not have valid data pointer, possibly because of a race condition. Please file a bug at https://github.com/dotnet/macios/issues.";
10711103
#if CONSISTENCY_CHECKS
10721104
throw new InvalidOperationException (msg);
10731105
#else
@@ -1076,6 +1108,9 @@ void RecreateDataHandle ()
10761108
#endif
10771109
}
10781110

1111+
#if LOGGING
1112+
Runtime.NSLog ($"{GetType ().Name}: id={objectId} previous value invalidated and disposed. Current flags: {flags}");
1113+
#endif
10791114
previous_data.Invalidate ();
10801115
// Don't dispose previous_data, because another thread might be referencing it, and trying to access its pointer - which is still valid.
10811116
// The GC will dispose of previous_data when its not accessible anymore.

0 commit comments

Comments
 (0)