home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / share / doc / python2.4 / README.valgrind < prev    next >
Encoding:
Text File  |  2004-07-06  |  3.3 KB  |  78 lines

  1. This document describes some caveats about the use of Valgrind with
  2. Python.  Valgrind is used periodically by Python developers to try
  3. to ensure there are no memory leaks or invalid memory reads/writes.
  4.  
  5. If you don't want to read about the details of using Valgrind, there
  6. are still two things you must do to suppress the warnings.  First,
  7. you must use a suppressions file.  One is supplied in
  8. Misc/valgrind-python.supp.  Second, you must do one of the following:
  9.  
  10.   * Uncomment Py_USING_MEMORY_DEBUGGER in Objects/obmalloc.c,
  11.     then rebuild Python
  12.   * Uncomment the lines in Misc/valgrind-python.supp that
  13.     suppress the warnings for PyObject_Free and PyObject_Realloc
  14.  
  15. Details:
  16. --------
  17. Python uses its own small-object allocation scheme on top of malloc,
  18. called PyMalloc.
  19.  
  20. Valgrind may show some unexpected results when PyMalloc is used.
  21. Starting with Python 2.3, PyMalloc is used by default.  You can disable
  22. PyMalloc when configuring python by adding the --without-pymalloc option.
  23. If you disable PyMalloc, most of the information in this document and
  24. the supplied suppressions file will not be useful.
  25.  
  26. If you use valgrind on a default build of Python,  you will see
  27. many errors like:
  28.  
  29.         ==6399== Use of uninitialised value of size 4
  30.         ==6399== at 0x4A9BDE7E: PyObject_Free (obmalloc.c:711)
  31.         ==6399== by 0x4A9B8198: dictresize (dictobject.c:477)
  32.  
  33. These are expected and not a problem.  Tim Peters explains
  34. the situation:
  35.  
  36.         PyMalloc needs to know whether an arbitrary address is one
  37.     that's managed by it, or is managed by the system malloc.
  38.     The current scheme allows this to be determined in constant
  39.     time, regardless of how many memory areas are under pymalloc's
  40.     control.
  41.  
  42.         The memory pymalloc manages itself is in one or more "arenas",
  43.     each a large contiguous memory area obtained from malloc.
  44.     The base address of each arena is saved by pymalloc
  45.     in a vector.  Each arena is carved into "pools", and a field at
  46.     the start of each pool contains the index of that pool's arena's
  47.     base address in that vector.
  48.  
  49.         Given an arbitrary address, pymalloc computes the pool base
  50.     address corresponding to it, then looks at "the index" stored
  51.     near there.  If the index read up is out of bounds for the
  52.     vector of arena base addresses pymalloc maintains, then
  53.     pymalloc knows for certain that this address is not under
  54.     pymalloc's control.  Otherwise the index is in bounds, and
  55.     pymalloc compares
  56.  
  57.             the arena base address stored at that index in the vector
  58.  
  59.         to
  60.  
  61.             the arbitrary address pymalloc is investigating
  62.  
  63.         pymalloc controls this arbitrary address if and only if it lies
  64.         in the arena the address's pool's index claims it lies in.
  65.  
  66.         It doesn't matter whether the memory pymalloc reads up ("the
  67.     index") is initialized.  If it's not initialized, then
  68.     whatever trash gets read up will lead pymalloc to conclude
  69.     (correctly) that the address isn't controlled by it, either
  70.     because the index is out of bounds, or the index is in bounds
  71.     but the arena it represents doesn't contain the address.
  72.  
  73.         This determination has to be made on every call to one of
  74.     pymalloc's free/realloc entry points, so its speed is critical
  75.     (Python allocates and frees dynamic memory at a ferocious rate
  76.     -- everything in Python, from integers to "stack frames",
  77.     lives in the heap).
  78.