我已经使用CFStream
打开了套接字,并且它正确地发送和接收了数据包(数据)。那时,当我连续收到数据包时,我正在解码此数据包并将此数据存储到NSObject类中。
但是我的问题是,当它接收到连续的数据包时(我正在以毫秒为单位从服务器接收数据),并且当它处理数据方法时,应用程序由于原因Thread 1: EXC_BAD_ACCESS (code=1, address=0x4d79c40)
而崩溃。在我的NetworkController.m
文件中。
当我从服务器获取数据时,连续调用此方法会缺少什么?
请参阅this screenshot。我正在计算我的每个数据包,以查看我收到了多少个数据包。但是不幸的是,计数变量会存储错误的数据。
有时我也会收到此错误。
Hash table corrupted. This is probably a memory error somewhere. (table at 0x1061db488, buckets at 0x7f8ea8893000 (4096 bytes), 256 buckets, 123 entries, 29 tombstones, data 0x0 0x9c8a23 0xd81b23000000 0xffff230000d81c23)
下面是我的变量。
@interface NetworkController () {
uint8_t btPendingBuffer[1024];
short shPendingBufferSize;
BOOL blWrite_;
NSString *strCurrentUserId,*stUserID_;
NSString *stPassword_;
NSString *stNewPassword_;
NSMutableData *_data;
int bytesRead,byteIndex;
NSMutableData *OutputData;
int count;
}
我正在从下面的方法从服务器获取数据。
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)streamEvent {
//@synchronized(self) {
if (stream == inputStream) {
switch (streamEvent) {
case NSStreamEventNone:
NSLog(@"NSStreamEventNone");
break;
case NSStreamEventOpenCompleted:
isInputStreamOpen = YES;
[self finishOpeningConnection];
break;
case NSStreamEventHasBytesAvailable: {
[self readFromStreamToInputBuffer];
}
break;
case NSStreamEventHasSpaceAvailable:
break;
case NSStreamEventErrorOccurred:
break;
case NSStreamEventEndEncountered:
[self closeConnection];
break;
}
}
}
此后,此数据将存储在NSMutable数组中。
- (void)readFromStreamToInputBuffer {
// Temporary buffer to read data into.
uint8_t buffer[1024];
if (arrInputBuffer == nil) {
arrInputBuffer = [[NSMutableArray alloc] init];
}
NSMutableData *mData = [[NSMutableData alloc] init];
// Try reading while there is data.
while ([inputStream hasBytesAvailable]) {
NSInteger bytesRead = [inputStream read:buffer maxLength:sizeof(buffer)];
[mData appendBytes:buffer length:bytesRead];
}
[arrInputBuffer addObject:mData];
// Do protocol-specific processing of data.
if (!methodCalled) {
methodCalled = YES;
[self parseIncomingData];
}
}
- (void)parseIncomingData {
if (arrInputBuffer.count > 0) {
NSMutableData *mData = [arrInputBuffer objectAtIndex:0];
[arrInputBuffer removeObjectAtIndex:0];
[self process:(uint8_t *)[mData bytes] shLen:(int)[mData length]];
}else {
methodCalled = NO;
}
}
此方法对数据进行处理以查找标头和数据长度。
-(void)process:(uint8_t[])btBufferData shLen:(int)shLen {
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
int counter = 0;
for (int i = shPendingBufferSize; i<(shPendingBufferSize+shLen); i++) {
btPendingBuffer[i] = btBufferData[counter];
counter++;
}
shPendingBufferSize += shLen;
short shStart = 0;
while ((shPendingBufferSize - shStart) >= TAGS.shHeaderSize) {
int tagBufferSize = (shPendingBufferSize - shStart);
uint8_t btTagBuffer[tagBufferSize];
int index = shStart;
for (int i = 0; i<(shPendingBufferSize - shStart); i++) {
btTagBuffer[i] = btPendingBuffer[index];
index++;
}
TagDecode *mTagDeCode = [[TagDecode alloc] init];
NSMutableArray *marrBtTagBuffer = [[NSMutableArray alloc] init];
for (int i = 0; i<tagBufferSize; i++) {//sizeof(btTagBuffer)
NSInteger decimal = (uint8_t)btTagBuffer[i];
[marrBtTagBuffer addObject:[NSNumber numberWithInteger:decimal]];
}
[mTagDeCode TagDecode:marrBtTagBuffer];
short shTag;
short shDataType = 0;
short shDataSize = 0;
BOOL blDataType = false;
BOOL blDataSize = false;
while ([mTagDeCode CheckCapacity:TAGS.shShortSize]) {
shTag = [mTagDeCode NextTag];
if (shTag == TAGS.btDataType) {
if ([mTagDeCode CheckCapacity:TAGS.shShortSize]) {
shDataType = [mTagDeCode TagShort];
blDataType = true;
blDataSize = false;
}
}else if (shTag == TAGS.shDataSize) {
if ([mTagDeCode CheckCapacity:TAGS.shShortSize]) {
shDataSize = [mTagDeCode TagShort];
blDataSize = true;
}
}
if (blDataType && blDataSize)
break;
}
if (!(blDataType && blDataSize)) {
shStart += [mTagDeCode GetIndex];
continue;
}
if ((shPendingBufferSize - shStart - TAGS.shHeaderSize) < shDataSize) {
break;
}
shStart += [mTagDeCode GetIndex];
int index2 = shStart;
NSMutableArray *marrBrProcess = [[NSMutableArray alloc] init];
for (int i = 0; i<shDataSize; i++) {
NSInteger decimal = (uint8_t)btPendingBuffer[index2];
[marrBrProcess addObject:[NSNumber numberWithInteger:decimal]];
index2++;
}
if (shDataType == TAGS.shCompressCrypt) {
}
[self updateUI:marrBrProcess];// From here I am storing this all data in NSObject class and will update my UI.
marrBrProcess = nil;
marrBtTagBuffer = nil;
if (blWrite_) {
} else {
}
shStart += shDataSize;
}
if (shStart != 0) {
shPendingBufferSize -= shStart;
if (shPendingBufferSize != 0) {
int index = shStart;
for (int i = 0; i<shPendingBufferSize; i++) {
btPendingBuffer[i] = btPendingBuffer[index];
index++;
}
}
}
});
dispatch_group_notify(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
[self parseIncomingData]; // Getting next data from here.
});
dispatch_release(group);
}
process
方法在arrInputBuffer
计数不为0的情况下连续执行。在我当前的NetworkControlle
类之间释放。我不知道它从哪里发布。我添加了dispatch_group_async
来正确执行方法,但仍然出现EXC_BAD_ACCESS
错误。
谢谢。