How to Management Bytes?
How to Management Bytes?
- Subject: How to Management Bytes?
- From: Bing Li <email@hidden>
- Date: Sun, 19 Jun 2011 23:28:12 +0800
Dear all,
First, I appreciate so much for your replies in the past days. Those replies
help me a lot when implementing my system.
I got another problem in the following method. A byte array is used to save
data transmitted from a remote node and the size is different for each
message. I think I need to release the byte array after it is used. However,
the system got crashed if the line, free(receivedBytes), was added. So I
made a copy for the receivedMessage. But it still crashed.
One more issue is that there was no memory leaking even if I didn't add the
line, free(receivedBytes). Why?
Thanks so much for your help!
Best regards,
Bing
…...
NSInteger messageSize = [sizeString intValue];
char receivedBytes[messageSize];
while (currentByteIndex < messageSize)
{
NSAutoreleasePool *subLoopPool =
[[NSAutoreleasePool alloc] init];
numberBytesReceived = recv(clientSocket,
buffer, messageSize - currentByteIndex, 0);
if (numberBytesReceived > 0)
{
for (int i = 0; i <
numberBytesReceived; i ++)
{
receivedBytes[currentByteIndex + i] = buffer[i];
}
currentByteIndex +=
numberBytesReceived;
}
else
{
[subLoopPool drain];
return;
}
[subLoopPool drain];
}
// NSString *receivedMessage = [[[[NSString alloc]
initWithBytes:receivedBytes length:currentByteIndex
encoding:NSUTF8StringEncoding] copy] autorelease];
NSString *receivedMessage = [[[NSString alloc]
initWithBytes:receivedBytes length:currentByteIndex
encoding:NSUTF8StringEncoding] autorelease];
[self NotifyMessageReceived:receivedMessage];
free(receivedBytes);
…...
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden