{"record":{"id":"0e10c6f2dc912d22","repo":"aeron-io/aeron","slug":"index-out-of-range","errorCode":null,"errorMessage":"index out of range","messagePattern":"index out of range","errorType":"exception","errorClass":"std::logic_error","httpStatus":null,"severity":"error","filePath":"aeron-client/src/main/cpp_wrapper/Subscription.h","lineNumber":542,"sourceCode":"\n        return createImage(image);\n    }\n\n    /**\n     * Return the {@link Image} associated with the given index.\n     *\n     * This method returns a share_ptr to the underlying Image and must be released before the Image may be fully\n     * reclaimed.\n     *\n     * @param index in the array\n     * @return image at given index or exception if out of range.\n     */\n    inline std::shared_ptr<Image> imageByIndex(std::size_t index) const\n    {\n        aeron_image_t *image = aeron_subscription_image_at_index(m_subscription, index);\n        if (nullptr == image)\n        {\n            throw std::logic_error(\"index out of range\");\n        }\n\n        return createImage(image);\n    }\n\n    /**\n     * Get a std::vector of active std::shared_ptr of {@link Image}s that match this subscription.\n     *\n     * This method will create a new std::vector<std::shared_ptr<Image>> populated with the underlying {@link Image}s.\n     *\n     * @return a std::vector of active std::shared_ptr of {@link Image}s that match this subscription\n     */\n    inline std::shared_ptr<std::vector<std::shared_ptr<Image>>> copyOfImageList() const\n    {\n        auto images = std::make_shared<std::vector<std::shared_ptr<Image>>>();\n        auto subscriptionAndImages = std::make_pair(m_subscription, images.get());\n\n        aeron_subscription_for_each_image(","sourceCodeStart":524,"sourceCodeEnd":560,"githubUrl":"https://github.com/aeron-io/aeron/blob/6d60124e15e35c11b49ba2e3c2c2858a09a18803/aeron-client/src/main/cpp_wrapper/Subscription.h#L524-L560","documentation":"Subscription::imageByIndex() delegates to the C API aeron_subscription_image_at_index(), and throws std::logic_error when no image exists at the requested index. It means the caller asked for an image position outside the current image list (0..imageCount()-1), or the image at that position has since gone away.","triggerScenarios":"Calling subscription->imageByIndex(i) with i >= subscription->imageCount(), or with a stale index after the number of images changed (image connected/disconnected between calls).","commonSituations":"Iterating with a cached count that is stale after a channel re-resolution or peer disconnect; off-by-one loops (<= imageCount()); assuming index equals session-id or a stable handle across image lifecycle changes.","solutions":["Read subscription->imageCount() immediately before the loop and iterate with i < imageCount().","Prefer imageBySessionId(sessionId) or the image list snapshot API instead of raw indices when possible.","Wrap the call in try/catch (std::logic_error) and skip/re-fetch if the image set changed concurrently.","Re-query the subscription after any connection change event before indexing."],"exampleFix":"// before\nfor (size_t i = 0; i <= sub->imageCount(); ++i)\n    auto img = sub->imageByIndex(i);\n// after\nconst size_t n = sub->imageCount();\nfor (size_t i = 0; i < n; ++i)\n    auto img = sub->imageByIndex(i);","handlingStrategy":"validation","validationCode":"bool validIndex(const std::shared_ptr<aeron::Subscription>& sub, size_t i) {\n    return i < sub->imageCount();\n}","typeGuard":null,"tryCatchPattern":"try {\n    auto image = sub->imageByIndex(i);\n} catch (const std::logic_error& e) {\n    // image set changed or index invalid: re-fetch imageCount() and retry/skip\n}","preventionTips":["Always iterate with i < sub->imageCount() fetched immediately before the loop.","Prefer imageBySessionId or for-each image APIs over raw indices.","Re-read image state after any connection/disconnection event."],"tags":["aeron","cpp","subscription","image"],"backgroundTag":"index-out-of-range","analyzedSha":"6d60124e15e35c11b49ba2e3c2c2858a09a18803","analyzedAt":"2026-09-12T11:17:07.683Z","contentChangedAt":"2026-09-12T11:17:07.683Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}