C API: Testing my reference counting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lord trousers

    #1

    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

  • Martin v. Löwis

    #2
    Re: C API: Testing my reference counting

    lord trousers wrote:[color=blue]
    > Is there a way I can get hold of these kinds of statistics for
    > debugging?[/color]

    This is best done when Python is build in debug mode.
    sys.gettotalref count 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

    Comment

    • lord trousers

      #3
      Re: C API: Testing my reference counting

      Martin v. Löwis wrote:[color=blue]
      > lord trousers wrote:[color=green]
      > > Is there a way I can get hold of these kinds of statistics for
      > > debugging?[/color]
      >
      > This is best done when Python is build in debug mode.
      > sys.gettotalref count 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[/color]

      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

      Comment

      • Tim Peters

        #4
        Re: C API: Testing my reference counting

        [lord trousers][color=blue][color=green][color=darkred]
        >>> Is there a way I can get hold of these kinds of statistics for
        >>> debugging?[/color][/color][/color]

        [Martin v. Löwis][color=blue][color=green]
        >> This is best done when Python is build in debug mode.
        >> sys.gettotalref count 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).[/color][/color]

        [lord trousers][color=blue]
        > 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.[/color]

        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.t xt, in any Python source distribution. The Python
        Windows installer does not contain that file.

        Comment

        Working...