baresip / baresip-ios

Baresip for iOS
46 stars 33 forks source link

Memory leaks and crash after relaunching the baresip stack in iOS #37

Closed HammerProgrammer closed 3 years ago

HammerProgrammer commented 3 years ago

If I register or make a call and then close baresip and relaunch it again it will crash after some time has passed or if I try to register or make a call again. My code is here: https://pastebin.com/t7a80DFz and the log below

31mconfig: no sip_cafile defined, tls connections maybe won't work
Local network address:  IPv4=en0|192.168.1.7 
dl: mod: ./stdio.so (dlopen(//stdio.so, 6): image not found)
module stdio.so: No such file or directory
aucodec: PCMU/8000/1
aucodec: PCMA/8000/1
auplay: audiounit
ausrc: audiounit
medianat: stun
medianat: turn
medianat: ice
uuid: fopen() per/CoreSimulator/Devices/3AA0D104-52FD-4790-8A14-F439277D1DA5/data/Containers/Bundle/Application/5BA743CA-0A66-4E19-B5F9-EF4A254C9495/baresip2nattempt.app/Frameworks/uuid (No such file or directory)
dl: mod: ./uuid.so (dlopen(//uuid.so, 6): image not found)
module uuid.so: No such file or directory
account: creating accounts template per/CoreSimulator/Devices/3AA0D104-52FD-4790-8A14-F439277D1DA5/data/Containers/Bundle/Application/5BA743CA-0A66-4E19-B5F9-EF4A254C9495/baresip2nattempt.app/Frameworks/accounts
dl: mod: ./account.so (dlopen(//account.so, 6): image not found)
module account.so: No such file or directory
contact: creating contacts template per/CoreSimulator/Devices/3AA0D104-52FD-4790-8A14-F439277D1DA5/data/Containers/Bundle/Application/5BA743CA-0A66-4E19-B5F9-EF4A254C9495/baresip2nattempt.app/Frameworks/contacts
dl: mod: ./contact.so (dlopen(//contact.so, 6): image not found)
module contact.so: No such file or directory
Populated 2 audio codecs
Populated 0 audio filters
Populated 0 video codecs
Populated 0 video filters
1001@novitae2.mycrestron.com: (prio 0) {0/UDP/v4} 200 OK (Grandstream UCM6202V1.7A 1.0.20.23) [1 binding]
"error value 0"
ua: stop all (forced=0)
"uag exit handler"
fd 6 in use: flags=1 fh=0x10a014810 arg=0x6000014c8dc0
fd 7 in use: flags=1 fh=0x10a014820 arg=0x6000014c9870
fd 8 in use: flags=1 fh=0x10a014810 arg=0x6000014c4dc0
fd 9 in use: flags=1 fh=0x10a016cf0 arg=0x6000002c36a0
Timers (2):
  0x7fb13c618578: th=0x109fe5480 expire=500ms
  0x7fb13c618540: th=0x109fe5440 expire=32000ms
mem: Memory leaks (60):
  0x7fb13d059e40: nrefs=1  size=1080    [10 00 00 00 02 00 00 00 10 27 00 00 30 75 00 00 ] [.........'..0u..]
  0x6000014c8dc0: nrefs=1  size=72      [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ] [................]
  0x6000014c9870: nrefs=1  size=72      [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ] [................]
  0x6000005c6840: nrefs=1  size=16      [90 99 72 3c b1 7f 00 00 10 00 00 00 00 00 00 00 ] [..r<............]
  0x7fb13c729990: nrefs=1  size=256     [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ] [................]
  0x6000005c7060: nrefs=1  size=16      [e0 e9 2d 00 00 60 00 00 02 00 00 00 00 00 00 00 ] [..-..`..........]
  0x6000002de9e0: nrefs=1  size=32      [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ] [................]
  0x600001ff8040: nrefs=1  size=152     [e0 75 1c 01 00 60 00 00 80 74 1c 01 00 60 00 00 ] [.u...`...t...`..]
  0x6000005ed670: nrefs=1  size=16      [30 5e 40 3c b1 7f 00 00 10 00 00 00 00 00 00 00 ] [0^@<............]
  0x7fb13c405e30: nrefs=1  size=256     [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ] [................]
  0x6000008d8140: nrefs=1  size=64      [00 00 00 00 00 00 00 00 c0 81 8d 00 00 60 00 00 ] [.............`..]
  0x6000005eda30: nrefs=1  size=16      [00 47 40 3c b1 7f 00 00 10 00 00 00 00 00 00 00 ] [.G@<............]
  0x7fb13c404700: nrefs=1  size=256     [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ] [................]
  0x6000008d81c0: nrefs=1  size=64      [40 81 8d 00 00 60 00 00 00 00 00 00 00 00 00 00 ] [@....`..........]
  0x6000005ef150: nrefs=1  size=16      [40 e5 40 3c b1 7f 00 00 10 00 00 00 00 00 00 00 ] [@.@<............]
  0x7fb13c40e540: nrefs=1  size=256     [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ] [................]
  0x6000005ef1a0: nrefs=1  size=16      [60 5e 41 3c b1 7f 00 00 10 00 00 00 00 00 00 00 ] [`^A<............]
  0x7fb13c415e60: nrefs=1  size=256     [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ] [................]
  0x6000005ecf40: nrefs=1  size=16      [30 c6 40 3c b1 7f 00 00 10 00 00 00 00 00 00 00 ] [0.@<............]
  0x7fb13c40c630: nrefs=1  size=256     [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ] [................]
  0x6000008d8240: nrefs=1  size=56      [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ] [................]
  0x6000005ef240: nrefs=1  size=4       [53 49 50 00                                     ] [SIP.            ]
  0x6000002c30a0: nrefs=1  size=24      [50 aa fe 09 01 00 00 00 40 80 ff 01 00 60 00 00 ] [P.......@....`..]
  0x6000011c75e0: nrefs=1  size=112     [00 00 00 00 00 00 00 00 30 75 1c 01 00 60 00 00 ] [........0u...`..]
  0x6000014c4dc0: nrefs=1  size=72      [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ] [................]
  0x6000011c7530: nrefs=1  size=112     [e0 75 1c 01 00 60 00 00 80 74 1c 01 00 60 00 00 ] [.u...`...t...`..]
  0x6000002c36a0: nrefs=1  size=24      [09 00 00 00 ff ff ff ff d0 b9 fe 09 01 00 00 00 ] [................]
  0x6000011c7480: nrefs=1  size=112     [30 75 1c 01 00 60 00 00 00 00 00 00 00 00 00 00 ] [0u...`..........]
  0x7fb13d80f440: nrefs=1  size=1176    [90 07 4c 01 00 60 00 00 00 00 00 00 00 00 00 00 ] [..L..`..........]
  0x6000014c0790: nrefs=1  size=65      [73 69 70 3a 31 30 30 31 3a 4e 4f 56 31 30 30 31 ] [sip:1001:NOV1001]
  0x600000dc2030: nrefs=1  size=39      [73 69 70 3a 31 30 30 31 40 6e 6f 76 69 74 61 65 ] [sip:1001@novitae]
  0x6000005c1350: nrefs=2  size=8       [4e 4f 56 31 30 30 31 00                         ] [NOV1001.        ]
  0x7fb13c406be0: nrefs=1  size=216     [00 00 00 00 00 00 00 00 10 02 de 3f c0 a8 01 07 ] [...........?....]
  0x600001ac4b40: nrefs=1  size=184     [10 76 dc 00 00 60 00 00 03 00 00 00 00 00 00 00 ] [.v...`..........]
  0x600000dc7610: nrefs=1  size=33      [73 69 70 3a 6e 6f 76 69 74 61 65 32 2e 6d 79 63 ] [sip:novitae2.myc]
  0x6000002c2a40: nrefs=1  size=17      [35 62 33 65 31 65 66 35 34 62 36 32 39 35 39 66 ] [5b3e1ef54b62959f]
  0x6000002c32e0: nrefs=1  size=17      [32 32 39 66 32 34 64 66 37 65 32 64 62 65 33 61 ] [229f24df7e2dbe3a]
  0x6000002c2e60: nrefs=1  size=32      [60 62 41 3c b1 7f 00 00 00 02 00 00 00 00 00 00 ] [`bA<............]
  0x7fb13c416260: nrefs=1  size=512     [54 6f 3a 20 3c 73 69 70 3a 31 30 30 31 40 6e 6f ] [To: <sip:1001@no]
  0x600000dc7450: nrefs=1  size=40      [20 6f 7c 01 00 60 00 00 20 6f 7c 01 00 60 00 00 ] [ o|..`.. o|..`..]
  0x6000002c2560: nrefs=1  size=20      [31 30 30 31 2d 30 78 37 66 62 31 33 63 35 30 61 ] [1001-0x7fb13c50a]
  0x6000008d8640: nrefs=1  size=64      [2b 73 69 70 2e 69 6e 73 74 61 6e 63 65 3d 22 3c ] [+sip.instance="<]
  0x6000002c2e00: nrefs=1  size=32      [60 b4 41 3c b1 7f 00 00 00 01 00 00 00 00 00 00 ] [`.A<............]
  0x7fb13c41b460: nrefs=1  size=256     [41 6c 6c 6f 77 3a 20 49 4e 56 49 54 45 2c 41 43 ] [Allow: INVITE,AC]
  0x6000017c6f20: nrefs=1  size=88      [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ] [................]
  0x6000005ed120: nrefs=1  size=12      [67 72 61 6e 64 73 74 72 65 61 6d 00             ] [grandstream.    ]
  0x6000005ef880: nrefs=1  size=5       [31 30 30 31 00                                  ] [1001.           ]
  0x600000dc7760: nrefs=1  size=44      [31 36 31 33 39 34 36 39 32 30 2f 38 66 37 39 37 ] [1613946920/8f797]
  0x6000005ee7f0: nrefs=1  size=5       [61 75 74 68 00                                  ] [auth.           ]
  0x6000002c3460: nrefs=1  size=17      [37 33 66 32 30 66 32 64 30 35 62 39 32 65 37 37 ] [73f20f2d05b92e77]
  0x6000002ccee0: nrefs=1  size=32      [40 4a 82 3d b1 7f 00 00 00 08 00 00 00 00 00 00 ] [@J.=............]
  0x7fb13d824a40: nrefs=1  size=2048    [4d 61 78 2d 46 6f 72 77 61 72 64 73 3a 20 37 30 ] [Max-Forwards: 70]
  0x7fb13c508cd0: nrefs=1  size=200     [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ] [................]
  0x6000005c1df0: nrefs=2  size=9       [52 45 47 49 53 54 45 52 00                      ] [REGISTER.       ]
  0x600000dc1cb0: nrefs=1  size=33      [73 69 70 3a 6e 6f 76 69 74 61 65 32 2e 6d 79 63 ] [sip:novitae2.myc]
  0x6000002cc940: nrefs=2  size=24      [6e 6f 76 69 74 61 65 32 2e 6d 79 63 72 65 73 74 ] [novitae2.mycrest]
  0x6000002c9b40: nrefs=1  size=24      [7a 39 68 47 34 62 4b 35 34 33 66 36 35 31 33 30 ] [z9hG4bK543f65130]
  0x6000002c9f00: nrefs=1  size=32      [40 c0 03 3e b1 7f 00 00 00 04 00 00 00 00 00 00 ] [@..>............]
  0x7fb13e03c040: nrefs=1  size=1024    [52 45 47 49 53 54 45 52 20 73 69 70 3a 6e 6f 76 ] [REGISTER sip:nov]
  0x7fb13c618500: nrefs=1  size=272     [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ] [................]
"deinit sipclient"
"sip state has been deinited"
config: no sip_cafile defined, tls connections maybe won't work
Local network address:  IPv4=en0|192.168.1.7 
dl: mod: ./stdio.so (dlopen(//stdio.so, 6): image not found)
module stdio.so: No such file or directory
aucodec: PCMU/8000/1
aucodec: PCMA/8000/1
auplay: audiounit
ausrc: audiounit
medianat: stun
medianat: turn
medianat: ice
uuid: fopen() per/CoreSimulator/Devices/3AA0D104-52FD-4790-8A14-F439277D1DA5/data/Containers/Bundle/Application/5BA743CA-0A66-4E19-B5F9-EF4A254C9495/baresip2nattempt.app/Frameworks/uuid (No such file or directory)
dl: mod: ./uuid.so (dlopen(//uuid.so, 6): image not found)
module uuid.so: No such file or directory
account: creating accounts template per/CoreSimulator/Devices/3AA0D104-52FD-4790-8A14-F439277D1DA5/data/Containers/Bundle/Application/5BA743CA-0A66-4E19-B5F9-EF4A254C9495/baresip2nattempt.app/Frameworks/accounts
dl: mod: ./account.so (dlopen(//account.so, 6): image not found)
module account.so: No such file or directory
contact: creating contacts template per/CoreSimulator/Devices/3AA0D104-52FD-4790-8A14-F439277D1DA5/data/Containers/Bundle/Application/5BA743CA-0A66-4E19-B5F9-EF4A254C9495/baresip2nattempt.app/Frameworks/contacts
dl: mod: ./contact.so (dlopen(//contact.so, 6): image not found)
module contact.so: No such file or directory
Populated 2 audio codecs
Populated 0 audio filters
Populated 0 video codecs
Populated 0 video filters
mem: mem_deref: magic check failed 0xb5b5b5b5 (0x7fb13c618500)
baresip2nattempt was compiled with optimization - stepping may behave oddly; variables may not be available.
(lldb) thread backtrace
* thread baresip/baresip#15, queue = 'com.apple.root.user-initiated-qos', stop reason = EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)
  * frame #0: 0x000000010a0205d3 baresip2nattempt`mem_deref(data=0x00007fb13c618500) at mem.c:314:6 [opt]
    frame baresip/baresip#1: 0x000000010a01e6d6 baresip2nattempt`tmr_poll(tmrl=0x000000010a04b200) at tmr.c:115:3 [opt]
    frame baresip/baresip#2: 0x000000010a01fa70 baresip2nattempt`re_main(signalh=<unavailable>) at main.c:1040:3 [opt]
    frame baresip/baresip#3: 0x0000000109fac919 baresip2nattempt`closure baresip/baresip#2 in SipClient.init(self=0x00006000033901c0) at ViewController.swift:103:30
    frame baresip/baresip#4: 0x0000000109facbe0 baresip2nattempt`thunk for @escaping @callee_guaranteed () -> () at <compiler-generated>:0
    frame baresip/baresip#5: 0x000000010a32b7ec libdispatch.dylib`_dispatch_call_block_and_release + 12
    frame baresip/baresip#6: 0x000000010a32c9c8 libdispatch.dylib`_dispatch_client_callout + 8
    frame baresip/baresip#7: 0x000000010a33e6e4 libdispatch.dylib`_dispatch_root_queue_drain + 827
    frame baresip/baresip#8: 0x000000010a33ee6d libdispatch.dylib`_dispatch_worker_thread2 + 135
    frame baresip/baresip#9: 0x00007fff60c8f47a libsystem_pthread.dylib`_pthread_wqthread + 244
    frame baresip/baresip#10: 0x00007fff60c8e493 libsystem_pthread.dylib`start_wqthread + 15
(lldb)
Koshub commented 3 years ago

Well, it is hard to say the timer is really the reason. According to the Swift code you are running re_main in concurrent queue so it is possible to have two re_main functions running simultaneously. Also, you are not unregistering the event handler registered here:

uag_event_register( { (agent, event, call, param, args) in
            let e = String(cString: uag_event_str(event))
            debugPrint("User Agent \(e)")
            if let pointer = args {
                let state = Unmanaged<SipState>.fromOpaque(pointer).takeUnretainedValue()
                state.update(to: e)
            }
        }, self.statePointer)
HammerProgrammer commented 3 years ago

I tried commenting out the event handler before but got the same result. I saw you opened a similar issue earlier did you solve it? How are you running the re_main loop if not inside a concurrent thread? Running on main blocks the UI for me

Koshub commented 3 years ago

@HammerProgrammer

I saw you opened a similar issue earlier did you solve it?

I just do not use menu module which was the reason in my case.

How are you running the re_main loop if not inside a concurrent thread?

What I do is provide a single thread (a serial dispatch queue) for every re_main loop. Also, I make sure the previous re_main deallocates its resources before load resources and run the new re_main.

You may consider do not rerun the baresip stack at all until you know why do you need this.

alfredh commented 3 years ago

please also check warnings. dont load modules that you dont need.

this issue is not a bug in baresip, but how it is used

HammerProgrammer commented 3 years ago

I've removed all the unnecessary modules, and I have moved the re_main loop to a serial queue, re_cancel is being called and I can see the serial queue stops running before deinit. And everytime I relaunch the stack (load the viewcontroller again) my memory usage goes up more and more.

* thread #19, queue = 'baresip', stop reason = EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)
  * frame #0: 0x0000000102c25de3 baresip2nattempt`mem_deref(data=0x00007f9858512280) at mem.c:314:6 [opt]
    frame #1: 0x0000000102c138ce baresip2nattempt`udp_timeout_handler [inlined] query_handler(q=0x00007f985852b270, err=0, hdr=0x0000000000000000, ansl=0x0000000000000000, authl=0x0000000000000000, addl=0x0000000000000000) at client.c:151:3 [opt]
    frame #2: 0x0000000102c13897 baresip2nattempt`udp_timeout_handler(arg=0x00007f985852b270) at client.c:629 [opt]
    frame #3: 0x0000000102c23ee6 baresip2nattempt`tmr_poll(tmrl=0x0000000102c51308) at tmr.c:115:3 [opt]
    frame #4: 0x0000000102c25280 baresip2nattempt`re_main(signalh=<unavailable>) at main.c:1040:3 [opt]
    frame #5: 0x0000000102bb5939 baresip2nattempt`closure #1 in SipClient.startLoop(self=0x0000600003b2e160) at SipClient.swift:156:30
    frame #6: 0x0000000102bacbc0 baresip2nattempt`thunk for @escaping @callee_guaranteed () -> () at <compiler-generated>:0
    frame #7: 0x0000000102f357ec libdispatch.dylib`_dispatch_call_block_and_release + 12
    frame #8: 0x0000000102f369c8 libdispatch.dylib`_dispatch_client_callout + 8
    frame #9: 0x0000000102f3d296 libdispatch.dylib`_dispatch_lane_serial_drain + 796
    frame #10: 0x0000000102f3df67 libdispatch.dylib`_dispatch_lane_invoke + 439
    frame #11: 0x0000000102f49de2 libdispatch.dylib`_dispatch_workloop_worker_thread + 882
    frame #12: 0x00007fff60c8f4c0 libsystem_pthread.dylib`_pthread_wqthread + 314
    frame #13: 0x00007fff60c8e493 libsystem_pthread.dylib`start_wqthread + 15

thread list
Process 1797 stopped
  thread #1: tid = 0x11e42, 0x00007fff60c5231a libsystem_kernel.dylib`mach_msg_trap + 10, queue = 'com.apple.main-thread'
  thread #7: tid = 0x11ea7, 0x00007fff60c5231a libsystem_kernel.dylib`mach_msg_trap + 10, name = 'com.apple.uikit.eventfetch-thread'
  thread #9: tid = 0x11eda, 0x00007fff60c52356 libsystem_kernel.dylib`semaphore_wait_trap + 10, name = 'AMCP Logging Spool'
  thread #15: tid = 0x122d8, 0x00007fff60c5231a libsystem_kernel.dylib`mach_msg_trap + 10, name = 'com.apple.audio.IOThread.client'
  thread #18: tid = 0x1228b, 0x00007fff60c52356 libsystem_kernel.dylib`semaphore_wait_trap + 10
* thread #19: tid = 0x1259c, 0x0000000102c25de3 baresip2nattempt`mem_deref(data=0x00007f9858512280) at mem.c:314:6, queue = 'baresip', stop reason = EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)
  thread #20: tid = 0x1278d, 0x00007fff60c539be libsystem_kernel.dylib`__workq_kernreturn + 10
HammerProgrammer commented 3 years ago

It no longer crashes. Also tmr_debug and mem_Debug no longer report any hanging timers or leaks. I had to call ua_stop_all(1) every time I wanted to close, even if re_main is not returning an error.