This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: In function define class inherit ctypes.structure, and using ctypes.pointer leak memory
Type: resource usage Stage: resolved
Components: ctypes Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Yang Big, amaury.forgeotdarc, belopolsky, berker.peksag, meador.inge, terry.reedy
Priority: normal Keywords:

Created on 2017-10-30 03:53 by Yang Big, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg305204 - (view) Author: Yang Big (Yang Big) Date: 2017-10-30 03:53
Here my code:
  2 import sys
  3 import ctypes
  4      
  5      
  6 def hi():
  7     class timespec(ctypes.Structure):
  8     ┆   """Time specification, as described in clock_gettime(3)."""
  9     ┆   _fields_ = (('tv_sec', ctypes.c_long),
 10     ┆   ┆   ┆   ┆   ('tv_nsec', ctypes.c_long))
 11      
 12      
 13     ts = timespec()
 14     po = ctypes.pointer(ts)
 15      
 16      
 17 def main():
 18     while True:
 19     ┆   test = hi()
 20      
 21 if __name__ == "__main__":
 22     sys  2 import sys
  3 import ctypes
  4      
  5      
  6 def hi():
  7     class timespec(ctypes.Structure):
  8     ┆   """Time specification, as described in clock_gettime(3)."""
  9     ┆   _fields_ = (('tv_sec', ctypes.c_long),
 10     ┆   ┆   ┆   ┆   ('tv_nsec', ctypes.c_long))
 11      
 12      
 13     ts = timespec()
 14     po = ctypes.pointer(ts)
 15      
 16      
 17 def main():
 18     while True:
 19     ┆   test = hi()
 20      
 21 if __name__ == "__main__":
 22     sys.exit(main())

run this code, i find that the program occupy more and more VmRss
my python is 2.7.11, in python3 i also find this problem. 
   Is this normal?I am a newbie with python, not very familiar with some 
python's internal principle. 
   Tanks.
msg305508 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2017-11-03 20:57
The tracker is for patching CPython.  Please consider closing this and asking your question on python-list.  When you post code, do so only once, and without line numbers, so it can be copied and run as is.  Do include data on the ref leaks.
msg305854 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2017-11-08 15:08
I agree with Terry. Your code calls hi() in an infinite loop (why timespec is defined in hi()?), we don't know how clock_gettime(3) is implemented etc. (it should cleanup its own memory if it calls malloc()) etc. Please use python-list or StackOverflow for usage questions.
msg305855 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2017-11-08 15:15
See also issue 12998.
History
Date User Action Args
2022-04-11 14:58:53adminsetgithub: 76077
2017-11-08 15:15:20berker.peksagsetmessages: + msg305855
2017-11-08 15:08:59berker.peksagsetstatus: open -> closed

nosy: + berker.peksag
messages: + msg305854

resolution: not a bug
stage: resolved
2017-11-03 20:57:01terry.reedysetnosy: + terry.reedy
messages: + msg305508
2017-10-30 03:53:39Yang Bigcreate