pcp
[Top] [All Lists]

Patch for Python's pmExtractValue Function

To: pcp@xxxxxxxxxxx
Subject: Patch for Python's pmExtractValue Function
From: Tom Yearke <tyearke@xxxxxxxxxxx>
Date: Thu, 27 Mar 2014 09:14:49 -0400
Delivered-to: pcp@xxxxxxxxxxx
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Thunderbird/24.4.0
Hi all,

In developing some scripts using the Python bindings for PCP, I discovered the scripts were leaking memory whenever they extracted string values from pmResult objects. As the man page for pmExtractValue states, the user is responsible for freeing strings returned by the function, but freeing memory was not something I expected to do in Python beyond pmResult objects, which have an explicit function for doing so. This was a simple enough fix to make on the script side, but I'd like to suggest that the issue be taken care of on the API side to prevent other developers from making the same mistake.

Part of my confusion stemmed from the fact that many Python functions return either Python objects or C objects allocated in Python code, both of which are subject to Python's memory management scheme. In the case of pmExtractValue, while the returned pmAtomValue objects are allocated in the Python function, any strings returned inside those objects are allocated in the C function. This means that the former are deallocated automatically when there are no more Python references to them, while the latter will stay in memory until manually freed.

I've attached a patch based on 3.9.1 for one possible approach to the problem, which involves performing an additional step in the Python pmExtractValue function after the C function returns. A string returned by the C function will be copied into Python memory and freed from C memory before the Python function returns. This will allow both the returned pmAtomValue object and its string to be automatically freed when there are no more Python references.

Thanks for your consideration! Hope this helps!

Tom Yearke

Attachment: patch.diff
Description: Text document

<Prev in Thread] Current Thread [Next in Thread>
  • Patch for Python's pmExtractValue Function, Tom Yearke <=