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

  • Home
  • SEARCH
  • 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 6602377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:52:11+00:00 2026-05-25T18:52:11+00:00

I’m trying to make a simple binary-adding app. Here’s the two main methods that

  • 0

I’m trying to make a simple binary-adding app. Here’s the two main methods that I use. You can assume that anything I don’t declare in the code has been declared in the header file:

-(IBAction)valuesChanged
{
    if ((![input1.text isEqualToString:@""]) && (![input2.text isEqualToString:@""]))
    {
        if (bitRange.selectedSegmentIndex == 0) {flowLimit = 8;}
        else if (bitRange.selectedSegmentIndex == 1) {flowLimit = 16;}
        else {flowLimit = 32;}

        NSMutableArray* bin1 = [[NSMutableArray alloc] initWithCapacity:32];
        NSMutableArray* bin2 = [[NSMutableArray alloc] initWithCapacity:32];
        NSMutableArray* resBin = [[NSMutableArray alloc] initWithCapacity:32];

        input1Decimal = [input1.text intValue];
        input2Decimal = [input2.text intValue];

        int decimalDummy = input1Decimal;
        while (decimalDummy > 0) 
        {
            if (decimalDummy == 1) 
            {
                [bin1 addObject:[NSNumber numberWithInt:1]];
                decimalDummy--;
            }
            else 
            {
                [bin1 addObject:[NSNumber numberWithInt:(decimalDummy % 2)]];
                decimalDummy = decimalDummy/2;
            }
        }

        decimalDummy = input2Decimal;
        while (decimalDummy > 0) 
        {
            if (decimalDummy == 1) 
            {
                [bin2 addObject:[NSNumber numberWithInt:1]];
                decimalDummy--;
            }
            else 
            {
                [bin2 addObject:[NSNumber numberWithInt:(decimalDummy % 2)]];
                decimalDummy = decimalDummy/2;
            }
        }

        while ([bin1 count] < flowLimit) {[bin1 addObject:[NSNumber numberWithInt:0]];}
        while ([bin2 count] < flowLimit) {[bin2 addObject:[NSNumber numberWithInt:0]];}

        resBin = [self addBinary:bin1 toBinary:bin2];

        NSString* string1 = @"";
        NSString* string2 = @"";
        NSString* string3 = @"";
        for (int i = 0; i < flowLimit; i++) 
        {
            string1 = [[NSString stringWithFormat:@"%@",[bin1 objectAtIndex:i]] stringByAppendingString:string1];
            string2 = [[NSString stringWithFormat:@"%@",[bin2 objectAtIndex:i]] stringByAppendingString:string2];
            string3 = [[NSString stringWithFormat:@"%@",[resBin objectAtIndex:i]] stringByAppendingString:string3];
        }
        [output1 setText:string1];
        [output2 setText:string2];
        [binResult setText:string3];

        [bin1 release];
        [bin2 release];
        [resBin release];
    }
}

-(NSMutableArray*)addBinary:(NSMutableArray*)binary1 toBinary:(NSMutableArray*)binary2
{
    BOOL carry = NO;
    NSMutableArray* result = [NSMutableArray arrayWithCapacity:32];
    for (int i = 0; i < flowLimit; i++) 
    {
        if (([[binary1 objectAtIndex:i] intValue] == 0) && ([[binary2 objectAtIndex:i] intValue] == 0)) 
        {
            if (carry) 
            {
                [result addObject:[NSNumber numberWithInt:1]];
            }
            else {[result addObject:[NSNumber numberWithInt:0]];}
        }
        else if (([[binary1 objectAtIndex:i] intValue] == 1) && ([[binary2 objectAtIndex:i] intValue] == 0)) 
        {
            if (carry) 
            {
                [result addObject:[NSNumber numberWithInt:0]];
                carry = YES;
            }
            else {[result addObject:[NSNumber numberWithInt:1]];}
        }
        else if (([[binary1 objectAtIndex:i] intValue] == 0) && ([[binary2 objectAtIndex:i] intValue] == 1)) 
        {
            if (carry) 
            {
                [result addObject:[NSNumber numberWithInt:0]];
                carry = YES;
            }
            else {[result addObject:[NSNumber numberWithInt:1]];}
        }
        else
        {
            if (carry) 
            {
                [result addObject:[NSNumber numberWithInt:1]];
                carry = YES;
            }
            else 
            {
                [result addObject:[NSNumber numberWithInt:0]];
                carry = YES;
            }

        }
        carry = NO;
    }
    return result;
}

The code runs fine in the debugger, but somewhere after these methods are run, I get an “EXC_BAD_ACCESS” error. Anyone know why this is happening?

  • 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-25T18:52:11+00:00Added an answer on May 25, 2026 at 6:52 pm

    Instead of removing all the [X release] messages, try changing this:

    NSMutableArray* bin1 = [[NSMutableArray alloc] initWithCapacity:32];
    NSMutableArray* bin2 = [[NSMutableArray alloc] initWithCapacity:32];
    NSMutableArray* resBin = [[NSMutableArray alloc] initWithCapacity:32];
    

    To this:

    NSMutableArray* bin1 = [NSMutableArray array];
    NSMutableArray* bin2 = [NSMutableArray array];
    NSMutableArray* resBin = [NSMutableArray array];
    

    Named initializers are autoreleased by default, but when you explicitely call alloc and initialize the return value of alloc, you must release it or it will leak.


    Another good debugging tip:

    Set NSZombieEnabled, malloc stack logging, and guard malloc in the debugger. Then, when your App crashes, type this in the gdb comsole:

    (gdb) info malloc-history 0x543216
    

    Replace 0x543216 with the address of the object that the stack trace says caused the crash,and it will give you a much more useful stack trace and it should highlight the exact line that is causing the problem.

    Check out this article for more info on NSZombieEnabled.

    This one for MallocStackLogging info

    More info on guard malloc here


    After rereading the code, the only problem I can see is here:

    resBin = [self addBinary:bin1 toBinary:bin2];
    [resBin release];
    

    Your releasing something without increasing the retain count first and since it has already been autoreleased by the second method, you get the EXC_BAD_ACCESS. Either remove the [resBin release]; or do this:

    NSMutableArray *resBin = [[NSMutableArray alloc] initWithArray:[self addBinary:bin1 toBinary:bin2]];
    

    Then you can safely call [resBin release];.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't

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.