Skip to content

Commit

Permalink
fixup code after suggestions, guard free
Browse files Browse the repository at this point in the history
  • Loading branch information
Swatinem committed Jun 1, 2022
1 parent 7ddbba6 commit 13b8d24
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions src/Sentry.Unity/Il2CppEventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void Process(Exception incomingException, SentryEvent sentryEvent)
// whereas the native stack trace is sorted from callee to caller.
var frame = sentryStacktrace.Frames[i];
var nativeFrame = nativeStackTrace.Frames[nativeLen - 1 - i];
frame.InstructionAddress = $"0x{nativeFrame:X8}");
frame.InstructionAddress = $"0x{nativeFrame:X8}";
frame.AddressMode = addrMode;
}
}
Expand Down Expand Up @@ -103,32 +103,35 @@ private NativeStackTrace GetNativeStackTrace(Exception e)
var addresses = IntPtr.Zero;
try
{
var gchandle = GCHandle.ToIntPtr(gch).ToInt32();
var addr = il2cpp_gchandle_get_target(gchandle);

var numFrames = 0;
string? imageUUID = null;
string? imageName = null;
il2cpp_native_stack_trace(addr, out addresses, out numFrames, out imageUUID, out imageName);

// Convert the C-Array to a managed "C#" Array, and free the underlying memory.
var frames = new IntPtr[numFrames];
Marshal.Copy(addresses, frames, 0, numFrames);
}
finally
{
// We are done with the `GCHandle`.
gch.Free();

il2cpp_free(addresses);
}

return new NativeStackTrace
var gchandle = GCHandle.ToIntPtr(gch).ToInt32();
var addr = il2cpp_gchandle_get_target(gchandle);

var numFrames = 0;
string? imageUUID = null;
string? imageName = null;
il2cpp_native_stack_trace(addr, out addresses, out numFrames, out imageUUID, out imageName);

// Convert the C-Array to a managed "C#" Array, and free the underlying memory.
var frames = new IntPtr[numFrames];
Marshal.Copy(addresses, frames, 0, numFrames);

return new NativeStackTrace
{
Frames = frames,
ImageUuid = imageUUID,
ImageName = imageName,
};
}
finally
{
Frames = frames,
ImageUuid = imageUUID,
ImageName = imageName,
};
// We are done with the `GCHandle`.
gch.Free();

if (addresses != IntPtr.Zero)
{
il2cpp_free(addresses);
}
}
}

// NOTE: fn is available in Unity `2019.4.34f1` (and later)
Expand All @@ -150,7 +153,7 @@ private NativeStackTrace GetNativeStackTrace(Exception e)
internal class NativeStackTrace
{
public IntPtr[] Frames { get; set; } = Array.Empty<IntPtr>();
public string? ImageUuid { get; set; };
public string? ImageName { get; set; };
public string? ImageUuid { get; set; }
public string? ImageName { get; set; }
}
}

0 comments on commit 13b8d24

Please sign in to comment.