2

I wrote a code snippet to implement LCS algorithm for my app. In this algorithm I implemented an array using malloc. But, I am not sure if I am doing anything wrong. I could not find any specific message showing the reason. I could not find the way out.

Note: I have arc on

my code is given below

- (IBAction)buttonAction:(id)sender
{
    [self getInlineDiffofFragment1:[NSArray arrayWithObjects:@"line1", @"line2", @"line3", @"line4", @"line5", nil] andFragment2:[NSArray arrayWithObjects:@"line1", @"line2", @"line4", @"line6", nil]];
}

- (void)getInlineDiffofFragment1:(NSArray*)fragment1 andFragment2:(NSArray*)fragment2
{
    int row = fragment1.count + 1;
    int col = fragment2.count + 1;
    int *lcs = (int*)malloc(sizeof(int) * row * col); //2d array
    NSString *str;
    for (int i = 0; i <= fragment1.count; i++) {
        lcs[i * row] = 0;
    }
    for (int j = 0; j <= fragment2.count; j++) {
        lcs[0 + j] = 0;
    }

    for (int i = 1; i < row; i++) {
        str = [NSString stringWithFormat:@""];
        for (int j = 1; j < col; j++) {
            int xy = i * row + j;
            if ([[fragment1 objectAtIndex:i - 1] isEqualToString:[fragment2 objectAtIndex:j - 1]]) {
                lcs[xy] = lcs[(i - 1) * row + (j - 1)] + 1;
            } else {
                lcs[xy] = MAX(lcs[(i - 1) * row + j], lcs[i * row + (j - 1)]);
            }
            str = [str stringByAppendingFormat:@"%d ", lcs[xy]];
        }
        NSLog(@"%@", str);
    }

    free(lcs);
    NSLog(@"freed");
}

The function perfectly print the result as expected and exit the block. The result is given below

2012-09-26 16:14:18.727 TestDiff[56178:11303] 1 1 1 1 
2012-09-26 16:14:18.733 TestDiff[56178:11303] 1 2 2 2 
2012-09-26 16:14:18.734 TestDiff[56178:11303] 1 2 2 2 
2012-09-26 16:14:18.735 TestDiff[56178:11303] 1 2 3 3 
2012-09-26 16:14:18.736 TestDiff[56178:11303] 1 2 3 3 
2012-09-26 16:14:18.736 TestDiff[56178:11303] freed

after printing the result and exiting the block it crashes. Crash report is following

enter image description here

The program was ok for the following test function

#define WIDTH 4
#define HEIGHT 5
#define INDEXOF(x,y) ((y*WIDTH) + x)

- (void)testArray
{
    NSString *str;
    int *myArray = (int *) malloc(sizeof(int) * 5 * 4);  // 4 * 5 array
    for(int x=0; x<WIDTH; x++){
        str = [NSString stringWithFormat:@""];
        for(int y=0; y<HEIGHT; y++){
            myArray[INDEXOF(x,y)] = y;
            str = [str stringByAppendingFormat:@"%d ", myArray[INDEXOF(x, y)]];
        }
        NSLog(@"%@", str);
    }
    free(myArray);
}

Thanks for your help

Avigit
  • 284
  • 2
  • 3
  • 13

2 Answers2

1

Go to your build settings and look for an item called 'Compile Source As'. Change it's value to 'Objective C++'.

Tommy Devoy
  • 13,441
  • 3
  • 48
  • 75
  • 'Compile Source As' is set to 'According to file type'. I changed it as you said, nothing changed so far. Furthermore, I will put this function to another project. so, i can't change that property permanently. – Avigit Sep 27 '12 at 01:40
1

Have you tried placing a break point on the line with free(myArray);? Does it make it to that point successfully and then crash when you step over it?

Also, if you're getting EXC_BAD_ACCESS, you should try turning zombies on and see if something has been released already.

Community
  • 1
  • 1
Matt Long
  • 24,438
  • 4
  • 73
  • 99
  • I tested putting break point on the line with `free(lcs);` I tried zombies after you as you said. After a few executions of the function it shows an error message `TestDiff(56896,0xac5482c0) malloc: *** error for object 0x8876224: incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug 1 1 1 1 1 2 2 2 1 2 2 2 1 2 3 3 1 2 3 3 freed 2012-09-26 19:48:33.484 TestDiff[56896:11303] *** -[__NSSetM release]: message sent to deallocated instance 0x8876220` but `lcs` is local and i freed it at the end of the block – Avigit Sep 27 '12 at 02:01
  • Yeah. This is one of the quirks with using ARC. I love ARC, but when you get these EXC_BAD_ACCESS errors in ARC they are a pain to figure out since you never explicitly call release. Do you have the All Exceptions breakpoint turned on? If not, try doing that first (see http://stackoverflow.com/questions/9882999/how-can-i-catch-all-exceptions-in-ios-5 ). This will often stop execution on the exact line where it's happening which can be helpful. Otherwise, see if there are any objects holding on to the NSSet that might be causing the issue. – Matt Long Sep 27 '12 at 03:23