Xcode:memcpy EXC_BAD_ACCESS?

时间:2015-05-30 09:22:44

标签: c++ ios c xcode memcpy

我在构建并运行时遇到以下崩溃:

enter image description here

enter image description here

这是我在调试器中看到的消息(注意:我确实启用了Guard Malloc& Log Malloc Stack)。

GuardMalloc[MyApp-63254]: Allocations will be placed on 16 byte boundaries.
GuardMalloc[MyApp-63254]:  - Some buffer overruns may not be noticed.
GuardMalloc[MyApp-63254]:  - Applications using vector instructions (e.g., SSE) should work.
GuardMalloc[MyApp-63254]: version 107
MyApp(63254,0x1062a3300) malloc: stack logs being written into /tmp/stack-logs.63254.10e133000.MyApp.kmVU0O.index
MyApp(63254,0x1062a3300) malloc: recording malloc and VM allocation stacks to disk using standard recorder
MyApp(63254,0x1062a3300) malloc: process 63063 no longer exists, stack logs deleted from /tmp/stack-logs.63063.113aba000.MyApp.WTSUw0.index

这是我的整个方法:

- (MatrixObject *)matrixByRemovingColumnAtIndex:(int)index {

    // In this example this should be 2
    NSLog(@"int debug: %d", index);

    //Store new column size
    int newColumnCount = (columnCount - 1);

    //Make sure our index is not out of bounds
    if (index > newColumnCount) {

        @throw [NSException exceptionWithName:@"Index Out Of Bounds Exception"
                                       reason:[NSString stringWithFormat:@"Oops! %@'s index input is out of bounds. Maximum bounds allowed is: %d.", [self class], columnCount - 1]
                                     userInfo:nil];
    }

    //Create new double array
    double *newMatrix = malloc(rowCount * newColumnCount * sizeof(double));

    /*
     We need to copy in 3 steps.
     Step 1: Copy first row elements before our column at index.
     Step 2: Copy every row in every column except for our column at index rows
     Step 3: Copy last row elements after our column at index.
     */

    // Do Step 1
    memcpy(newMatrix, self->matrix, index * sizeof(double));

    //Total number of elements
    int allElements = (rowCount * columnCount);

    //Simplify our index
    int i = (index + 1);

    //Do Step 2 while looping through all our rows in our columns
    do {

        //Copy every element
        memcpy(newMatrix + index, self->matrix + i, newColumnCount * sizeof(double));

        //Increment our index by our newColumnCount
        index += newColumnCount;

        //Increment our i by columnCount
        i += columnCount;
    }

    //Loop through all our rows in our columns until we reach the end
    while ((i < allElements) && ((i + columnCount) < allElements));


    // Do Step 3 (Don't forget our rows after our column at index bc we want those)
    if (i < allElements) {

        //Copy our last elements (THIS IS WHERE THE EXCEPTION OCCURS)
        memcpy(newMatrix + index, self->matrix + i, (allElements - i) * sizeof(double));
    }

    return [[self class] matrixFromDoubleArray:newMatrix withRows:rowCount andColumns:newColumnCount];
}

有没有人使用memcpy遇到此类崩溃。注意:此方法是从MatrixObject子类调用的,但我怀疑这会对此产生影响。

0 个答案:

没有答案