Autoreleased Data In Cocoa
Autoreleased Data In Cocoa
- Subject: Autoreleased Data In Cocoa
- From: Bing Li <email@hidden>
- Date: Tue, 31 May 2011 10:15:10 +0800
Dear all,
I have a question about autoreleased data in Cocoa. According to the
documentation from Apple.com, developers need to design autorelease pools
when programming command-line, loop and threading. If in Cocoa without
threading, no autorelease pools are programmed explicitly since an
autorelease pool is created by Cocoa. Am i right? If so, autoreleased data
has a life cycle as long as the process? What is the difference between
implicit autoreleasing and memory leaking?
I got a problem related to autorelease pools. My program works in Cocoa
without NSThread developed by me. The method received data remotely and then
returned data outside. I autoreleased the data before they were returned.
However, it got exceptions sometimes. If I did NOT autorelease them,
everything ran well. Could you give me a hand on this? The code is as
follows.
I appreciate so much for your help!
Best regards,
Bing
- (NSString *) receiveMessage
{
NSMutableString *receivedString;
NSString *message;
NSString *newReceivedString;
[isConnectedLock lock];
@try
{
if (isConnected)
{
char buffer[Constants.WWW.BUFFER_SIZE];
// Receive remote data
ssize_t numBytesReceived = recv(destinationSocket,
buffer, Constants.WWW.BUFFER_SIZE, 0);
if (numBytesReceived <= 0)
{
return Constants.WWW.NO_RECEIVED_MESSAGE;
}
buffer[numBytesReceived] = '\0';
// Convert data to NSString locally
receivedString = [[NSMutableString alloc]
initWithBytes:buffer length:numBytesReceived encoding:NSUTF8StringEncoding];
message = [[NSString alloc]
initWithString:Constants.WWW.EMPTY_STRING];
// Since the data has specific formats, some simple
parsing jobs by delimiter are done as follows.
NSRange range = [receivedString
rangeOfString:Constants.WWW.DELIMITER];
if (range.location > 0 && range.location <
[receivedString length])
{
message = [receivedString
substringToIndex:range.location];
// Return data after parsing
return message;
}
else
{
// Receive remote data continually if the
receivedString is longer than expected
while (numBytesReceived > 0)
{
NSAutoreleasePool *loopPool =
[[NSAutoreleasePool alloc] init];
// Receive data
numBytesReceived =
recv(destinationSocket, buffer, Constants.WWW.BUFFER_SIZE, 0);
if (numBytesReceived <= 0)
{
return
Constants.WWW.NO_RECEIVED_MESSAGE;
}
buffer[numBytesReceived] = '\0';
newReceivedString = [[[NSString
alloc] initWithBytes:buffer length:numBytesReceived
encoding:NSUTF8StringEncoding] autorelease];
range = [receivedString
rangeOfString:Constants.WWW.DELIMITER];
if (range.location > 0 &&
range.location < [receivedString length])
{
message = [receivedString
substringToIndex:range.location];
// Return data after parsing
return message;
}
[loopPool drain];
}
}
return Constants.WWW.NO_RECEIVED_MESSAGE;
}
else
{
return Constants.WWW.NO_RECEIVED_MESSAGE;
}
}
@catch (NSException *e)
{
return Constants.WWW.NO_RECEIVED_MESSAGE;
}
@finally
{
[isConnectedLock unlock];
// Before returning received data, autorelease them here.
[receivedString autorelease];
[message autorelease];
}
}
_______________________________________________
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