CocoaAsyncSocket icon indicating copy to clipboard operation
CocoaAsyncSocket copied to clipboard

possible memory leak ios 9.0.2

Open eemerge opened this issue 10 years ago • 20 comments

Sending messages with a GCDAsyncUdpSocket in an iOS app (SpriteKit), makes the Memory indicator in the debugging sidebar in XCode go up and never comes down ( I reached 500+ MB at one time after a lot of usage, and the app only starts with 10 MB). I did not manage to find where the leak comes from with Instruments , but by trial and error, I managed to figure out it only happen when I send a message

My code [_clientSocket sendData:data toHost:host port:port withTimeout:-1 tag:1];

Which goes to GDCAsyncUdpSocket.m:

  • (void)sendData:(NSData *)data toHost:(NSString *)host port:(uint16_t)port withTimeout:(NSTimeInterval)timeout tag:(long)tag

Tested with an iphone 6 / ios 9.0.2. A thing to note, the leak does not seem to be happening on an iPhone4 / iOS 7.

eemerge avatar Dec 25 '15 00:12 eemerge

Any solution found ??

Prakashhadapad avatar Jan 01 '16 12:01 Prakashhadapad

Actually , it would be good if someone could reproduce this (or not). I connected the xcode debugger to the application launched directly on the iphone, and there the memory is stable. Is it possible that it's actually a bug coming from XCode ? That might explain why it's not appearing on the iphone 4 ?

eemerge avatar Jan 01 '16 20:01 eemerge

Hi , I am able to reproduce each and every every time , I am trying to send around 300MB video from one iPhone 5S (Sender )to IPhone 6S (Receiver), at Receiver side after reading each packet , memory getting acclimated and application memory increasing , which turning to crash due to memory shortage . I am guessing packet are not dequeuing after reading at receiver side .

How to ensure that packets are dequeued at receiver side after read.

Please let me know if you need any additional info .

Prakashhadapad avatar Jan 02 '16 02:01 Prakashhadapad

Hi , I am able to reproduce each and every time . seems like read packets are not dequeued after reading could you please help me to fix this issue . it is bit very urgent . On 01-Jan-2016 3:42 pm, "eemerge" [email protected] wrote:

Actually , it would be good if someone could reproduce this (or not). I connected the xcode debugger to the application launched directly on the iphone, and there the memory is stable. Is it possible that it's actually a bug coming from XCode ? That might explain why it's not appearing on the iphone 4 ?

— Reply to this email directly or view it on GitHub https://github.com/robbiehanson/CocoaAsyncSocket/issues/390#issuecomment-168336356 .

Prakashhadapad avatar Jan 02 '16 10:01 Prakashhadapad

You'll have much better results getting help on alleged memory bugs in FOSS projects if you can deliver a minimal, reproducible test case. That means a small project, easily read by someone else, that always reproduces the bug.

Since you have such a project, please post it somewhere, and perhaps someone will take a look.

halmueller avatar Jan 02 '16 10:01 halmueller

Hi Everyone , Thanks for your reply . I am able to found cause for it . Actually it is fix with Xcode memory usage feature , Application runs properly if i am not debugging through Xcode . i have tried with Xcode 6.4 , 7 and 7.2 version all Xcode version have same issue

Prakashhadapad avatar Jan 05 '16 01:01 Prakashhadapad

Simple test project to reproduce this problem: https://github.com/robbiehanson/CocoaAsyncSocket/files/101793/F53OSCLeakTest.zip

I get it regardless of what target I select in XCode. On a real device clicking the test button will leak 1MB.

Enigma644 avatar Jan 23 '16 18:01 Enigma644

It's appearing on my iPad with iPhone. Both ios9. Anyone Solved it?

xuanxt avatar Feb 19 '16 09:02 xuanxt

I managed to get a constant memory usage in Xcode debugger , by doing some ugly modifications to the GCDAsyncUdpSocket.m file:

  • first , I added a 2nd function by duplicating asyncResolveHost. I named it syncResolveHost and removed all the "dispatch_async" stuff from it, and including all the code of the functionin an @autorelease {} block. I imagine this removes all the 'async' magic from the function, but in my case it also removed the leaking.
  • secondly, in this function:
- (void)sendData:(NSData *)data
          toHost:(NSString *)host
            port:(uint16_t)port
     withTimeout:(NSTimeInterval)timeout
             tag:(long)tag

I replaced the call to asyncResolveHost with a call to the newly duplicated syncResolveHost

//[self asyncResolveHost:host port:port withCompletionBlock:^(NSArray *addresses, NSError *error) {
    [self syncResolveHost:host port:port withCompletionBlock:^(NSArray *addresses, NSError *error) {

and towards the end of the function i removed dispatch_async part:

//dispatch_async(socketQueue, ^{ @autoreleasepool {

        [sendQueue addObject:packet];
        [self maybeDequeueSend];

    //}});

I know its far from 'fixing' it properly, but this seems to stop the leaking (or at least Xcode debugger does not show any more memory leaking).

It would be great if someone with more knowledge and comprehension of the code of this library would do the proper fixes.

eemerge avatar Feb 24 '16 21:02 eemerge

I also found this on Apple developer forums:

https://forums.developer.apple.com/thread/19582

Seems people are saying it's a bug with xcode7 / ios9 concerning dispatch_async ...

eemerge avatar Feb 24 '16 21:02 eemerge

@eemerge I read this page.

The problem seems to be on device only and only when running from XCode in the debugger - not if the app is started without the debugger.

I test my testProject not running from XCode in the debugger,and i observed its memory changes. Exciting! It did not happen memory leak. So it just Xcode debugger bug in ios9,don't be worried.

xuanxt avatar Feb 25 '16 02:02 xuanxt

I filed a bug with Apple, will keep you updated.

eemerge avatar Feb 25 '16 20:02 eemerge

请问有人解决了么?

3812600 avatar Mar 22 '16 06:03 3812600

What ?

RegisStGelais avatar Mar 22 '16 11:03 RegisStGelais

[https://translate.google.com/] He's asking if someone has found a fix yet. I second that!

Enigma644 avatar Mar 22 '16 12:03 Enigma644

I write Chinese, I'm sorry! I want to ask the memory overflow problem solved? My IOS9.3 cannot be used normally, thank you for your reply

c3812600 avatar Mar 23 '16 14:03 c3812600

According to eemerge and puga1024 (see previous posts) the memory leak does not occurs when not debugging.

Quote "The problem seems to be on device only and only when running from XCode in the debugger - not if the app is started without the debugger."

RegisStGelais avatar Mar 23 '16 15:03 RegisStGelais

Testing procedures directly collapse

c3812600 avatar Mar 23 '16 15:03 c3812600

Xcode 7.3 seems to have fixed the issue. So , I'd say it was related to Xcode and not to CocoaAsyncSocket. I will wait for some confirmations from people here who had the issue with XCode < 7.3 before closing the issue.

eemerge avatar Apr 01 '16 22:04 eemerge

We are also facing the same problem with xcode 7.3.1 and ios9.2.1

dbammi-ari avatar Sep 09 '16 10:09 dbammi-ari