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 8896215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:58:57+00:00 2026-06-14T23:58:57+00:00

I wrote a recursive flood fill method in objective c that I am currently

  • 0

I wrote a recursive flood fill method in objective c that I am currently using in an iPad app.

I get the color of the point the user touches in a UIImage via RGBA raw data for the image.

The problem is that it runs for a while then the app crashes with a EXC_BAD_ACCESS while accessing the raw data. My question is why, is this a “stack overflow”? and would anyone be able to suggest a way to fix this / improve upon this approach.

Here is my method (It could be cleaner, I apologize).

-(unsigned char *)floodFill:(unsigned char *)data withImageRef:(CGImageRef)imgRef withColor:(UIColor *)color whereColor:(UIColor *)pixelColor atX:(int)xx andY:(int)yy
    {
        //create points for top bottom left and right pixel
        CGPoint top = CGPointMake(xx, yy-1);
        CGPoint bottom = CGPointMake(xx, yy+1);
        CGPoint left = CGPointMake(xx-1, yy);
        CGPoint right = CGPointMake(xx+1, yy);

        //convert new color to rgba values
        const CGFloat *rgb = CGColorGetComponents(color.CGColor);

        float newRed = rgb[0];
        float newGreen = rgb[1];
        float newBlue = rgb[2];
        float newAlpha = CGColorGetAlpha(color.CGColor);

        //convert old color to rgba values
        const CGFloat *rgb2 = CGColorGetComponents(pixelColor.CGColor);

        float oldRed = rgb2[0];
        float oldGreen = rgb2[1];
        float oldBlue = rgb2[2];
        float oldAlpha = CGColorGetAlpha(pixelColor.CGColor);

        NSUInteger width = CGImageGetWidth(imgRef);
        NSUInteger bytesPerPixel = 4;
        NSUInteger bytesPerRow = bytesPerPixel * width;
        int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel;

        //color current pixel
        data[byteIndex] = (char)newRed*255;
        data[byteIndex+1] = (char)newGreen*255;
        data[byteIndex+2] = (char)newBlue*255;
        data[byteIndex+3] = (char)newAlpha*255;


        CGFloat red, green, blue, alpha;
        CGPoint currentPoint;

        //check top pixel
        currentPoint=top;
        if(currentPoint.x>=0 && currentPoint.y>=0)
        {
            byteIndex = (bytesPerRow * currentPoint.y) + currentPoint.x * bytesPerPixel;
            red   = (data[byteIndex]     * 1.0) / 255.0;
            green = (data[byteIndex + 1] * 1.0) / 255.0;
            blue  = (data[byteIndex + 2] * 1.0) / 255.0;
            alpha = (data[byteIndex + 3] * 1.0) / 255.0;
            if(red==oldRed&&green==oldGreen&&blue==oldBlue&&alpha==oldAlpha)
                data=[self floodFill:data withImageRef:imgRef withColor:color whereColor:pixelColor atX:currentPoint.x andY:currentPoint.y];
        }

        //check bottom pixel
        currentPoint=bottom;
        if(currentPoint.x>=0 && currentPoint.y>=0)
        {
            byteIndex = (bytesPerRow * currentPoint.y) + currentPoint.x * bytesPerPixel;
            red   = (data[byteIndex]     * 1.0) / 255.0;
            green = (data[byteIndex + 1] * 1.0) / 255.0;
            blue  = (data[byteIndex + 2] * 1.0) / 255.0;
            alpha = (data[byteIndex + 3] * 1.0) / 255.0;
            if(red==oldRed&&green==oldGreen&&blue==oldBlue&&alpha==oldAlpha)
                data=[self floodFill:data withImageRef:imgRef withColor:color whereColor:pixelColor atX:currentPoint.x andY:currentPoint.y];
        }

        //check left pixel
        currentPoint=left;
        if(currentPoint.x>=0 && currentPoint.y>=0)
        {
            byteIndex = (bytesPerRow * currentPoint.y) + currentPoint.x * bytesPerPixel;
            red   = (data[byteIndex]     * 1.0) / 255.0;
            green = (data[byteIndex + 1] * 1.0) / 255.0;
            blue  = (data[byteIndex + 2] * 1.0) / 255.0;
            alpha = (data[byteIndex + 3] * 1.0) / 255.0;
            if(red==oldRed&&green==oldGreen&&blue==oldBlue&&alpha==oldAlpha)
                data=[self floodFill:data withImageRef:imgRef withColor:color whereColor:pixelColor atX:currentPoint.x andY:currentPoint.y];
        }

        //check right pixel
        currentPoint=right;
        if(currentPoint.x>=0 && currentPoint.y>=0)
        {
            byteIndex = (bytesPerRow * currentPoint.y) + currentPoint.x * bytesPerPixel;
            red   = (data[byteIndex]     * 1.0) / 255.0;
            green = (data[byteIndex + 1] * 1.0) / 255.0;
            blue  = (data[byteIndex + 2] * 1.0) / 255.0;
            alpha = (data[byteIndex + 3] * 1.0) / 255.0;
            if(red==oldRed&&green==oldGreen&&blue==oldBlue&&alpha==oldAlpha)
                data=[self floodFill:data withImageRef:imgRef withColor:color whereColor:pixelColor atX:currentPoint.x andY:currentPoint.y];
        }



        return data;
    }

Thanks -TJ

  • 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-06-14T23:58:59+00:00Added an answer on June 14, 2026 at 11:58 pm

    Recursive methods are major cause of stack overflow exceptions because every recursive call is pushed onto stack until it reaches end condition, especially in flood fill algorithm this approach is not suitable at all, ipad has limited resources as compared to modern computers, consider using loop based implementation of flood fill algorithm or scan-line algorithm

    for more information visit this link Flood Fill algorithms

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

Sidebar

Related Questions

i'm making kinda ms paint application, that draws conture and fill inside.I wrote recursive
I wrote simple recursive version of Newton's method: #include <cmath> using namespace std; double
I'm trying to write a recursive function that would get some string, as well
For a SO question i quite recently wrote a generic extension method that should
I wrote recursive method to calculate folder size: private static long calcSize(File dir) {
I was creating a faster string splitter method. First, I wrote a non-tail recursive
I'm trying to create a parser using Treetop that is somewhat recursive. An expression
I am trying to write a recursive function in Java that prints the numbers
In clojure, I would like to write a tail-recursive function that memoizes its intermediate
I wrote a recursive function, which returns an array with the paths to all

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.