Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6761851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:14:27+00:00 2026-05-26T14:14:27+00:00

Consider the following image: My app first finds all the blue pixels and also

  • 0

Consider the following image:

enter image description here

My app first finds all the blue pixels and also records the x,y coordinates of all their siblings (the siblings for a given pixel are those that border it: up, down, left, right, upper-left, upper-right, etc). It then loops through all of these blue pixels in order to figure out how many blue siblings they have. The goal is ultimately to determine which group of blue pixels is the biggest.

This is a large loop, though, and it ends up resulting in the error “warning: Unable to restore previously selected frame.” I believe this occurs because I’m overflowing the stack. If that is the case, how would you recommend I change my code in order to fix this issue?

Here’s the code:

The method that starts the looping:

for (NSString *key in pixelItemDict) {
    Pixel *px = [pixelItemDict objectForKey:key];
    if (!px.hasBeenCounted) {

        //If it hasn't been counted, we know that we're onto a new distinct area, because otherwise counterForSiblingsOfPixel would have already counted it. So we now add a copy of the pixelsCurrentlyBeingCounted array, empty that array, and reset the counter
        [pixelCounterDict setObject:[pixelsCurrentlyBeingCounted copy] forKey:[NSString stringWithFormat:@"%i",currentPxCounter]];

        [pixelsCurrentlyBeingCounted removeAllObjects];

        [self counterForSiblingsOfPixel:px];

    }
}

The method that continuously calls itself, resulting in the stack overflow:

- (void)counterForSiblingsOfPixel:(Pixel *)px
{
    [pixelsCurrentlyBeingCounted addObject:px];
    currentPxCounter++;
    px.hasBeenCounted = true;

    for (Pixel *sibling in px.siblings) {
        if (!sibling.hasBeenCounted) [self counterForSiblingsOfPixel:px];
    }
}

Essentially, I have a pixelItemDict that contains all the blue pixels. I enumerate over this dictionary, calling the method counterForSiblingsOfPixel: on each one. That method in turn calls itself on each one of the siblings of the pixel passed to it. The forin loop only regains control once every pixel in an area of blue has been counted. Therefore, large areas of blue result in counterForSiblingsOfPixel: calling itself a huge number of times, leading to the overflow.

Is there a standard design pattern that should be used in cases such as this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:14:27+00:00Added an answer on May 26, 2026 at 2:14 pm

    First, you’re not modifying px, so each call to -counterForSiblingsOfPixel: will do exactly the same thing. When you use recursion, you want to make sure that each level of recursion simplifies the problem to some degree. If the recursion doesn’t eventually hit some base case, it’ll go on forever and, yes, overflow the stack. So, start by making sure that your call to that method is for some simpler case.

    Second, you probably don’t want to simplify by just one pixel… You’d be fine if this method divides the pixels into two halves and calls itself recursively for each half, because you’d only end up with log2(pixels) levels of recursions — about 20 for a millions pixels. But if you process one pixel and then call yourself recursively for the remaining pixels, you’d end up with 1000000 levels of recursion, which will also overflow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following mock-up image I've created: I am new with working with InfoPath
Consider the following xhtml: <img src=myImage.jpg alt=My image /> <br /> <hr /> I
Let's consider the following case: There is a 2.5MB image in an <img> tag
Consider the following html: <div class=image> <img src=sample.png/> <div class=text> Text of variable length
I want to create an android app. please consider the following scenario: Iam standing
Please consider the following code (also in this fiddle ): var viewModel = {
Consider the following image: How would I get the average (unknown what this is
Please consider following example: The source image consists of 6 areas that need to
Consider the following piece of Xaml <Grid Background=Blue> <Border Width=100 Height=60 BorderBrush=Black BorderThickness=2> <Border
Consider the following image: This is supposed to look like a patch of grass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.