• 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
How to Management Bytes?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: How to Management Bytes?
      • From: Scott Ribe <email@hidden>
    • Re: How to Management Bytes?
      • From: Fritz Anderson <email@hidden>
  • Prev by Date: Re: Non-rectangular image of NSView for dragging
  • Next by Date: Re: How to Management Bytes?
  • Previous by thread: Re: This code is leaking...
  • Next by thread: Re: How to Management Bytes?
  • Index(es):
    • Date
    • Thread