[AI Task] [Tizen.Multimedia.Remoting] Remove triple enumeration in ScreenMirroring.SendGenericMouseEvent - #7738
[AI Task] [Tizen.Multimedia.Remoting] Remove triple enumeration in ScreenMirroring.SendGenericMouseEvent#7738JoonghyunCho wants to merge 2 commits into
Conversation
…seEvent (Fixes #7643) - Materialize the input IEnumerable<UibcMouseInfo> once (IReadOnlyList fast path, ToArray fallback) instead of enumerating it three times via Any() + Count() + foreach. - Throw ArgumentNullException for null input (previously NullReferenceException from Any()). - Throw ArgumentException for empty input instead of the semantically wrong ArgumentNullException. - Document the new ArgumentException in the XML doc comment. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
|
There was a problem hiding this comment.
Code Review
This pull request refactors the SendGenericMouseEvent method in ScreenMirroring.cs to prevent multiple enumerations of uibcMouseInfos, introduce proper null validation, and fix a potential indexing bug by replacing the foreach loop with a standard for loop. The reviewer provided valuable feedback pointing out a potential memory leak during unmanaged memory allocation and suggested a refactoring to ensure proper cleanup using a try-finally block, alongside simplifying pointer arithmetic.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| 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++) |
There was a problem hiding this comment.
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.
-
Memory Leak:
IfMarshal.AllocHGlobal(Marshal.SizeOf(uibcObject))throws an exception (such asOutOfMemoryException), or ifMarshal.StructureToPtrthrows an exception, the previously allocatedunmanagedUibcMousememory will be leaked because thetryblock has not been entered yet. -
Pointer Arithmetic:
Instead of checkingIntPtr.Size == 4and performing manual pointer arithmetic withToInt32()orToInt64(), you can useIntPtr.Addor 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);
}
}|
🤖 [AI Review] Reviewed — no findings. Scope checked:
No 🔴 critical issues, no 🟡 suggestions to flag. Automated review — final merge decision rests with human reviewers. |
Fix potential native memory leak in SendGenericMouseEvent by allocating unmanaged buffers inside try/finally, and replace 32/64-bit pointer arithmetic branching with IntPtr addition Applied-Human-Comments: 3524738875
|
🤖 [AI Review] |
|
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days |
Summary
ScreenMirroring.SendGenericMouseEventenumerated itsIEnumerable<UibcMouseInfo>input three times (Any()→Count()→foreach) on the UIBC high-frequency mouse-input path, and threw the semantically wrongArgumentNullExceptionfor an empty collection (while an actualnullinput crashed withNullReferenceException).This change materializes the input exactly once and uses the correct argument-validation exception types, with no public API signature change.
Changes
src/Tizen.Multimedia.Remoting/ScreenMirroring/ScreenMirroring.csSendGenericMouseEventnow validatesnullinput up front viaArgumentNullException.ThrowIfNull.as IReadOnlyList<UibcMouseInfo>fast path,ToArray()fallback), removing theAny()+Count()+foreachtriple enumeration — deferred LINQ sequences are now executed a single time.ArgumentException(wasArgumentNullException).<exception cref="ArgumentException">XML doc tag; marshalling and the native call are unchanged.Mode
Refactoring
Verification
dotnet buildon Tizen.Multimedia.Remoting — 0 errors)sdb deviceslists no targets)Fixes #7643
🤖 Generated with Claude Code