Avoid memory leak in Osip

Source: Internet
Author: User
I was debugging Memory Leak bugs recently. the bug was caused by incorrect usage of The oSIP library. it's not uncommon that we meet problems when we rely on a library or framework that we don't fully understand.

Symptom and debugging
The symptom is our application ran more and more slowly, and eventually crashed. this seemed very likely to be caused by resource leak. and after ran Performance Monitor against our application, it's further confirmed that memory was leaking. the virtual bytes, private bytes of the process was continuously increasing.
With the help of umdh.exe, we can find out exact lines of code that were leaking memory. it showed all stack traces of currently allocated memory blocks (including blocks that were either being used or leaked, so we must identify which blocks were in use and which were not) at the moment of dumping.

The causes of the memory leak is mainly caused by not understanding below items well.

    • Transaction isn' t destroyed automatically

Osip doesn' t take full responsibility of managing life time of transactions. though Osip invokes callbacks registered with osip_set_kill_transaction_callback when a transaction is to be terminated, the transaction isn' t freed automatically. this is supposed to be done by Osip users.
The first thought I had is to call osip_transaction_free inside the kill_transaction callback, but it was wrong. because the transaction is still accessed after the kill_transaction callback returned. so, a possible point to free transactions is to do it at the end of an iteration of the main loop, After all events have been handled.
I just don't get why this important point isn't mentioned in the official document.

    • Inconsistent Resource Management Model

In Osip, there are inconsistency between APIs about how memories are managed. for example, we call osip_message_set_body to set the body of a SIP message, this function internally duplicates the string we passed to it. so, we can (and need to) Free the string passed to it after the function finishes. but when we want to call osip_message_set_method, be cautious! This function doesn' t duplicate the string passed in, instead, it simply references the string we gave it. so, we can't free the string which is now owned by the SIP message.
Such inconsistency makes it extremely easy to get confused and write code that either crashes or leaks.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.