microsoft / omi-script-provider

Script Provider for the OMI Project
Other
8 stars 7 forks source link

Fix memory leaks Python >= 3 #80

Closed alinbalutoiu closed 6 years ago

alinbalutoiu commented 6 years ago

PyUnicode_AsEncodedString returns a new reference on which we need to call Py_DECREF after the call to PyBytes_AsString.

To fix this, the macro for PyString_AsString has been removed. All the usages of that function have been replaced with the correct one that also frees the object created.

alinbalutoiu commented 6 years ago

Confirmed by running valgrind on the test.py file.

Before this patch:

==42938==
==42938== HEAP SUMMARY:
==42938==     in use at exit: 1,329,431 bytes in 13,082 blocks
==42938==   total heap usage: 805,777 allocs, 792,695 frees, 57,123,708 bytes allocated
==42938==
==42938== LEAK SUMMARY:
==42938==    definitely lost: 15,699 bytes in 398 blocks
==42938==    indirectly lost: 0 bytes in 0 blocks
==42938==      possibly lost: 379,876 bytes in 2,532 blocks
==42938==    still reachable: 933,856 bytes in 10,152 blocks
==42938==         suppressed: 0 bytes in 0 blocks
==42938==

After this patch:

==42665==
==42665== HEAP SUMMARY:
==42665==     in use at exit: 1,313,732 bytes in 12,684 blocks
==42665==   total heap usage: 805,872 allocs, 793,188 frees, 57,125,425 bytes allocated
==42665==
==42665== LEAK SUMMARY:
==42665==    definitely lost: 0 bytes in 0 blocks
==42665==    indirectly lost: 0 bytes in 0 blocks
==42665==      possibly lost: 379,876 bytes in 2,532 blocks
==42665==    still reachable: 933,856 bytes in 10,152 blocks
==42665==         suppressed: 0 bytes in 0 blocks
==42665==