Re: [NSLock dealloc] Exception
Re: [NSLock dealloc] Exception
- Subject: Re: [NSLock dealloc] Exception
- From: Charles Srstka <email@hidden>
- Date: Fri, 20 May 2011 10:23:45 -0500
On May 20, 2011, at 10:11 AM, Bing Li wrote:
> Dear all,
>
> I am implementing a TCP client to send messages with BSD sockets. A BOOL
> variable, isConnected, is used to detect if the TCP connection is built.
> Since the client must be accessed by multiple threads, a NSLock,
> isConnectedLock is used.
>
> When testing, after invoking the methods, Connect and SendMessage, I release
> the client. However, I got an exception as follows.
>
> **** -[NSLock dealloc]: lock (<NSLock: 0x100614cc0> '(null)') deallocated
> while still in use*
>
> **** Break on _NSLockError() to debug.*
>
> What is the potential problem?
>
> Thanks so much!
> Bing
>
> - (BOOL) Connect
> {
> [isConnectedLock lock];
> if (!isConnected)
> {
> destinationSocket = socket(AF_INET, SOCK_STREAM,
> IPPROTO_TCP);
> if (destinationSocket < 0)
> {
> return NO;
> }
>
> memset(&destinationAddress, 0, sizeof(destinationAddress));
> destinationAddress.sin_family = AF_INET;
> const char *ip = [DestinationIP UTF8String];
> int isAddressValidate = inet_pton(AF_INET, ip,
> &destinationAddress.sin_addr.s_addr);
> if (isAddressValidate == 0)
> {
> return NO;
> }
> else if (isAddressValidate < 0)
> {
> return NO;
> }
> destinationAddress.sin_port = htons(PortNumber);
> if (connect(destinationSocket, (struct sockaddr
> *)&destinationAddress, sizeof(destinationAddress)) < 0)
> {
> return NO;
> }
> isConnected = YES;
> }
> [isConnectedLock unlock];
> return YES;
> }
>
> - (BOOL) SendMessage:(char *)message
> {
> [isConnectedLock lock];
> if (isConnected)
> {
> size_t messageLength = strlen(message);
> ssize_t bytesCount = send(destinationSocket, message,
> messageLength, 0);
> if (bytesCount < 0)
> {
> return NO;
> }
> else if (bytesCount != messageLength)
> {
> return NO;
> }
> return YES;
> }
> else
> {
> return NO;
> }
> [isConnectedLock unlock];
> }
>
> - (BOOL) Disconnect
> {
> [isConnectedLock lock];
> if (isConnected)
> {
> close(destinationSocket);
> isConnected = NO;
> }
> [isConnectedLock unlock];
> return YES;
> }
>
> - (void) dealloc
> {
> [CommKey release];
> [DestinationIP release];
> [isConnectedLock release];
> [super dealloc];
> }
>
> @end
You see all those “return YES;” and “return NO;” statements in your Connect and Disconnect (these should actually start with a lower-case letter, btw, to conform to standard Cocoa convention) methods? Those are causing the code never to get to the [isConnectedLock unlock]; statement, which is causing your issue.
Charles
_______________________________________________
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