title: SIGABRT happens at the end of this function
body: <p>At the end of the following function (which is only called once), I receive a SIGABRT as a result of a __stack_chk_fail. I think it means the stack is corrupted? Why is it causing this abort?</p>
  
  <p>The code is supposed to send a file to my server, and to do so it breaks up the file into 256 byte chunks and sends each individually. The reason I did this is just writing <code>[writeSock write:[buf bytes]]</code> resulted in only like 30-40% of the data being sent.</p>
  
  <pre><code>- (BOOL) sendFile:(NSData*)buf
  {
      CFReadStreamRef readStream;
      CFWriteStreamRef writeStream;
  
      CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)(@"servername"), 999, &amp;readStream, &amp;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 *)&amp;length maxLength:4];
      // Write data to server
      Byte smallBuf[256];
      uint32_t amountTransfered = 0;
      while (amountTransfered &lt; 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;
  }
  </code></pre>