SIGABRT发生在此函数的末尾

时间:2014-08-09 23:42:12

标签: objective-c

在以下函数结束时(只调用一次),我收到了__stack_chk_fail的SIGABRT。我认为这意味着堆栈已损坏?为什么导致这次中止?

代码应该将文件发送到我的服务器,为此,它将文件分成256个字节的块并单独发送。我这样做的原因只是写[writeSock write:[buf bytes]]只导致30-40%的数据被发送。

- (BOOL) sendFile:(NSData*)buf
{
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;

    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)(@"servername"), 999, &readStream, &writeStream);

    NSInputStream * readSock = (__bridge NSInputStream*)(readStream);
    [readSock setDelegate:self];
    NSOutputStream * writeSock = (__bridge NSOutputStream * )(writeStream);
    [writeSock setDelegate:self];


    [readSock scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [writeSock scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    NSLog(@"Open socket");
    [readSock open];
    [writeSock open];

    [writeSock write:(uint8_t*)("submit\0\0\0\0") maxLength:10];

    NSLog(@"Data length: %d", [buf length]);

    // Write length to server
    uint32_t length = (uint32_t)([buf length]);
    [writeSock write:(uint8_t *)&length maxLength:4];
    // Write data to server
    Byte smallBuf[256];
    uint32_t amountTransfered = 0;
    while (amountTransfered < length) {
        NSRange r;
        r.location = amountTransfered;
        r.length = MIN(amountTransfered + 256, length - amountTransfered);
        [buf getBytes:smallBuf range:r];
        [writeSock write:smallBuf maxLength:r.length];
        amountTransfered += r.length;
    }

    NSLog(@"Done writing");
    /*uint8_t inBuf[128];
    [readSock read:inBuf maxLength:128];

    if (strcmp((char*)inBuf, "success") != 0) {
        NSLog(@"Couldn't submit file: %s", (char*)inBuf);
    }*/

    NSLog(@"Close socket");
    return YES;
}

1 个答案:

答案 0 :(得分:1)

您写道:

r.length = MIN(amountTransfered + 256, length - amountTransfered);

你的意思是:

r.length = MIN(256, length - amountTransfered);