Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -499,32 +499,36 @@ public void SetUibcInformation(Size windowSize, ScreenMirroringCaptureMode mode)
/// An internal error occurs.
/// </exception>
/// <exception cref="ArgumentNullException"><paramref name="uibcMouseInfos"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="uibcMouseInfos"/> is empty.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="ScreenMirroring"/> has already been disposed.</exception>
[EditorBrowsable(EditorBrowsableState.Never)]
public void SendGenericMouseEvent(IEnumerable<UibcMouseInfo> uibcMouseInfos, ScreenMirroringMouseEventType type)
{
ArgumentNullException.ThrowIfNull(uibcMouseInfos);

ValidateState(ScreenMirroringState.Connected, ScreenMirroringState.Playing);

if (!uibcMouseInfos.Any())
var infos = uibcMouseInfos as IReadOnlyList<UibcMouseInfo> ?? uibcMouseInfos.ToArray();
if (infos.Count == 0)
{
throw new ArgumentNullException(nameof(uibcMouseInfos));
throw new ArgumentException("uibcMouseInfos cannot be empty.", nameof(uibcMouseInfos));
}

var uibcMouseInfosSize = uibcMouseInfos.Count();
var uibcMouseInfosSize = infos.Count;
var uibcMouse = new Native.UibcMouse[uibcMouseInfosSize];
int i = 0;
IntPtr unmanagedUibcMousePtr;

foreach (var uibcMouseInfo in uibcMouseInfos)
for (int i = 0; i < uibcMouseInfosSize; i++)
{
var uibcMouseInfo = infos[i];
uibcMouse[i].id = uibcMouseInfo.Id;
uibcMouse[i].x = uibcMouseInfo.X;
uibcMouse[i++].y = uibcMouseInfo.Y;
uibcMouse[i].y = uibcMouseInfo.Y;
}

var size = Marshal.SizeOf(typeof(Native.UibcMouse));
IntPtr unmanagedUibcMouse = Marshal.AllocHGlobal(size * uibcMouseInfosSize);
for (i = 0; i < uibcMouseInfosSize; i++)
for (int i = 0; i < uibcMouseInfosSize; i++)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Memory Leak and Pointer Arithmetic Improvement

There is a potential memory leak in the allocation of unmanaged memory, and the pointer arithmetic can be significantly simplified.

  1. Memory Leak:
    If Marshal.AllocHGlobal(Marshal.SizeOf(uibcObject)) throws an exception (such as OutOfMemoryException), or if Marshal.StructureToPtr throws an exception, the previously allocated unmanagedUibcMouse memory will be leaked because the try block has not been entered yet.

  2. Pointer Arithmetic:
    Instead of checking IntPtr.Size == 4 and performing manual pointer arithmetic with ToInt32() or ToInt64(), you can use IntPtr.Add or the + operator, which is cleaner, safer, and cross-platform.

Here is a suggested refactoring to address both issues:

            var size = Marshal.SizeOf(typeof(Native.UibcMouse));
            IntPtr unmanagedUibcMouse = IntPtr.Zero;
            IntPtr unmanagedUibcObject = IntPtr.Zero;

            try
            {
                unmanagedUibcMouse = Marshal.AllocHGlobal(size * uibcMouseInfosSize);
                for (int i = 0; i < uibcMouseInfosSize; i++)
                {
                    IntPtr unmanagedUibcMousePtr = unmanagedUibcMouse + (i * size);
                    Marshal.StructureToPtr(uibcMouse[i], unmanagedUibcMousePtr, false);
                }

                Native.UibcMouseEvent uibcObject;
                uibcObject.size = uibcMouseInfosSize;
                uibcObject.type = type;
                uibcObject.uibcMouse = unmanagedUibcMouse;

                unmanagedUibcObject = Marshal.AllocHGlobal(Marshal.SizeOf(uibcObject));
                Marshal.StructureToPtr(uibcObject, unmanagedUibcObject, false);

                Native.SendGenericMouseEvent(Handle, unmanagedUibcObject).ThrowIfError("Failed to send generic mouse event");
            }
            finally
            {
                if (unmanagedUibcMouse != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(unmanagedUibcMouse);
                }
                if (unmanagedUibcObject != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(unmanagedUibcObject);
                }
            }

{
if (IntPtr.Size == 4)
{
Expand Down
Loading