• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
[NSLock dealloc] Exception
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[NSLock dealloc] Exception


  • Subject: [NSLock dealloc] Exception
  • From: Bing Li <email@hidden>
  • Date: Fri, 20 May 2011 23:11:05 +0800

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
_______________________________________________

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

  • Follow-Ups:
    • Re: [NSLock dealloc] Exception
      • From: Nick Zitzmann <email@hidden>
    • Re: [NSLock dealloc] Exception
      • From: Charles Srstka <email@hidden>
  • Prev by Date: Re: The simplest way to fade out a view
  • Next by Date: Re: [NSLock dealloc] Exception
  • Previous by thread: Re: Fire-and-forget controllers with blocks
  • Next by thread: Re: [NSLock dealloc] Exception
  • Index(es):
    • Date
    • Thread