{"record":{"id":"619c26b9dd81aa8d","repo":"dotnet/maui","slug":"loop-manager-expects-a-uicollectionviewflowlayout","errorCode":null,"errorMessage":"Loop Manager expects a UICollectionViewFlowLayout","messagePattern":"Loop Manager expects a UICollectionViewFlowLayout","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/iOS/CollectionView/CarouselViewController.cs","lineNumber":499,"sourceCode":"\t\t\telse\n\t\t\t{\n\t\t\t\tCollectionView.Hidden = true;\n\t\t\t}\n\t\t}\n\t}\n\n\tclass CarouselViewLoopManager : IDisposable\n\t{\n\t\tint _indexOffset = 0;\n\t\tUICollectionViewFlowLayout _layout;\n\t\tconst int LoopCount = 3;\n\t\tILoopItemsViewSource _itemsSource;\n\t\tbool _disposed;\n\n\t\tpublic CarouselViewLoopManager(UICollectionViewFlowLayout layout)\n\t\t{\n\t\t\tif (layout == null)\n\t\t\t\tthrow new ArgumentNullException(nameof(layout), \"LoopManager expects a UICollectionViewFlowLayout\");\n\n\t\t\t_layout = layout;\n\t\t}\n\n\t\tpublic void CenterIfNeeded(UICollectionView collectionView, bool isHorizontal)\n\t\t{\n\t\t\tif (isHorizontal)\n\t\t\t\tCenterHorizontalIfNeeded(collectionView);\n\t\t\telse\n\t\t\t\tCenterVerticallyIfNeeded(collectionView);\n\t\t}\n\n\t\tprotected virtual void Dispose(bool disposing)\n\t\t{\n\t\t\tif (!_disposed)\n\t\t\t{\n\t\t\t\tif (disposing)\n\t\t\t\t{","sourceCodeStart":481,"sourceCodeEnd":517,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/iOS/CollectionView/CarouselViewController.cs#L481-L517","documentation":"CarouselViewLoopManager constructor throws ArgumentNullException when the UICollectionViewFlowLayout layout parameter is null. The loop manager implements infinite carousel scrolling and requires a valid flow layout to manage section insets, item spacing, and scroll positioning for the loop mechanism.","triggerScenarios":"Passing null to the CarouselViewLoopManager constructor; CarouselViewController creating the loop manager before the layout is initialized; layout disposed or nulled before loop manager construction during carousel teardown/recreation.","commonSituations":"CarouselView reconfiguration (e.g. changing layout direction) disposes the old layout before the loop manager is recreated; custom CarouselView setup that doesn't provide a flow layout; initialization race in the CarouselViewController lifecycle.","solutions":["Ensure a non-null UICollectionViewFlowLayout is created before constructing CarouselViewLoopManager","Guard the constructor call: if (layout != null) _loopManager = new CarouselViewLoopManager(layout);","Review CarouselViewController lifecycle to ensure layout exists before loop manager creation","Dispose the loop manager before disposing the layout to prevent race conditions"],"exampleFix":"// before\n_loopManager = new CarouselViewLoopManager(_layout); // throws if _layout was disposed\n\n// after\nif (_layout != null)\n    _loopManager = new CarouselViewLoopManager(_layout);\nelse\n    _loopManager = new CarouselViewLoopManager(new UICollectionViewFlowLayout());","handlingStrategy":"validation","validationCode":"// Before constructing CarouselViewLoopManager, null-check the layout\nif (layout == null)\n    throw new ArgumentNullException(nameof(layout), \"UICollectionViewFlowLayout required\");\nvar loopManager = new CarouselViewLoopManager(layout);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure UICollectionViewFlowLayout is created before the loop manager","Dispose the loop manager before disposing the layout to prevent stale references","Guard layout recreation in CarouselViewController lifecycle methods"],"tags":["carousel","collectionview","ios","null-argument","layout"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}