C API: Testing my reference counting

L

lord trousers

I'm currently replacing the Quake 3 game code (not the rendering,
sound, or collision detection pieces) with Python. I've now
successfully loaded Python modules and made callbacks to them, rendered
maps, written some fly-through code, and embedded a Python interactive
mode into the console (which is way, way cool).

It's my first C API project, and I want to make sure I've got my
reference counting right. I *could* go through the API docs and
determine which function returns what kind of PyObject pointer (shared,
new, etc.), and double- and triple-check all my code. In fact, I am,
but it's definitely prone to error, because I'm not as perfect as I'd
like to think. I want a more automatic way of doing it, or at least a
good way of checking correctness.

When the game is just running and spinning out frames, I *know* that no
new memory should be allocated that isn't immediately deallocated
(memory usage should be constant), and that Python's total reference
count shouldn't change. I can also set up tests that exercise various
parts of the Q3 engine / Python game thunking layer and support objects
that should leave memory in the same state it was at when they started.

Is there a way I can get hold of these kinds of statistics for
debugging?

Neil
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

lord said:
Is there a way I can get hold of these kinds of statistics for
debugging?

This is best done when Python is build in debug mode.
sys.gettotalrefcount then gives you the number of INCREF
calls for which no DECREF has been made; you said that
this shouldn't change.

If it does change, sys.get_counts() will give you the
number of objects per type.

Furthermore, sys.getobjects() will give you a list of
all objects allocated (excluding the result list).

HTH,
Martin
 
L

lord trousers

Martin said:
This is best done when Python is build in debug mode.
sys.gettotalrefcount then gives you the number of INCREF
calls for which no DECREF has been made; you said that
this shouldn't change.

If it does change, sys.get_counts() will give you the
number of objects per type.

Furthermore, sys.getobjects() will give you a list of
all objects allocated (excluding the result list).

HTH,
Martin

Wonderful! That's just what I was looking for.

Is this kind of thing documented somewhere public? (As attributes that
only show up in the debug build, they aren't documented in the regular
library docs.) There might be more nifty goodies like this, and I'd
like to check them out.

Thanks again!

Neil
 
T

Tim Peters

[lord trousers]
[Martin v. Löwis]
[lord trousers]
Wonderful! That's just what I was looking for.

Is this kind of thing documented somewhere public? (As attributes that
only show up in the debug build, they aren't documented in the regular
library docs.) There might be more nifty goodies like this, and I'd
like to check them out.

Actually, sys.getcounts() only exists in a COUNT_ALLOCS build (which
can be combined with a debug build, but is not implied by a debug
build).

All that (and other esoterica) is documented in
Misc/SpecialBuilds.txt, in any Python source distribution. The Python
Windows installer does not contain that file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top