473,414 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

C API: Testing my reference counting

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

Mar 20 '06 #1
3 1811
lord trousers wrote:
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
Mar 20 '06 #2
Martin v. Löwis wrote:
lord trousers wrote:
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


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

Mar 23 '06 #3
[lord trousers]
Is there a way I can get hold of these kinds of statistics for
debugging?

[Martin v. Löwis] 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).

[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.
Mar 23 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Elbert Lev | last post by:
Please correct me if I'm wrong. Python (as I understand) uses reference counting to determine when to delete the object. As soon as the object goes out of the scope it is deleted. Python does...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
17
by: Divick | last post by:
Hi, I am designing an API and the problem that I have is more of a design issue. In my API say I have a class A and B, as shown below class A{ public: void doSomethingWithB( B * b) { //do...
10
by: Fabian Steiner | last post by:
I recently started learning C since I want to be able to write Python extension modules. In fact, there is no need for it, but I simply want to try something new ... I tried to implement the...
3
by: Huayang Xia | last post by:
I get a python object by running a class' constructor. Then I need to modify the instance's attribute just like obj.attr1.attr2 = 'a' if in python's term. PyObject* py_obj_attr1 =...
0
by: Chris Thomasson | last post by:
Here are the pre-alpha code downloads: http://appcore.home.comcast.net/vzoom/refcount/ (both C and C++ api here...) Here is some pre-alpha documentation: ...
1
by: oec.deepak | last post by:
Hi Cn any one telll me what is Reference counting in C++.
30
by: Alf P. Steinbach | last post by:
I once suggested in that SomeOne Else(TM) should propose a string value class that accepted literals and char pointers and so on, with possible custom deleter, and in case of literal strings just...
8
by: mathieu | last post by:
Hi there I have implemented a very simple smartpointer class (invasive design). And I was wondering what should be the natural API when using those in a Container. I choose to define the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.