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: Memory leak when reinitializing a Struct object
Type: resource usage Stage: needs patch
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 2.7, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: mark.dickinson Nosy List: Winston451, mark.dickinson
Priority: normal Keywords: patch

Created on 2010-07-29 20:33 by Winston451, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
leak.py Winston451, 2010-07-29 20:33 file reproducing the issue
leak.patch Winston451, 2010-07-29 21:00
Messages (9)
msg111995 - (view) Author: (Winston451) Date: 2010-07-29 20:33
The s_init function of the _struct.c file does not free the s_codes field before storing a new address into it. So when a Struct object is reinitialized the memory block pointed to by s_codes is lost. The file attached with the bug report shows a code which reproduce the problem.
msg112002 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-07-29 20:47
Thanks for the report!  I'll look into this.

Do you happen to have a patch available, or an interest in writing one?
msg112003 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-07-29 20:48
Add versions.
msg112005 - (view) Author: (Winston451) Date: 2010-07-29 21:00
Hi Mark,

I added a patch in the file list that should correct the bug.
msg112006 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-07-29 21:03
Great!  Thank you.

I wonder whether the weakreflist field needs to be cleared similarly.
msg112013 - (view) Author: (Winston451) Date: 2010-07-29 21:29
I don't think that the weakreflist should be cleared. After all the object is just reinitialized not destructed.
msg112015 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-07-29 21:48
Yes, you're right of course.  Fixed in r83239 (py3k), r83240 (release31-maint), r83241 (release27-maint) and r83242 (release26-maint).

Thanks!

BTW, just out of curiosity:  I don't think I've ever seen anyone re-initialize a struct.Struct object before.  What's the reason for doing this instead of creating a new one?
msg112016 - (view) Author: (Winston451) Date: 2010-07-29 22:05
I was just looking at the code of the struct when i saw this problem. Personnally I have never had to reinitialize a Struct object but Python allows it so...
msg112021 - (view) Author: (Winston451) Date: 2010-07-29 23:00
BTW, I am really amazed by your reactivity. I submitted a bug report at 22:33 and one hour later it's fixed.

It's really great! Keep up the good work! :)
History
Date User Action Args
2022-04-11 14:57:04adminsetgithub: 53668
2010-07-29 23:00:52Winston451setmessages: + msg112021
2010-07-29 22:05:51Winston451setmessages: + msg112016
2010-07-29 21:48:48mark.dickinsonsetstatus: open -> closed
resolution: fixed
messages: + msg112015
2010-07-29 21:29:32Winston451setmessages: + msg112013
2010-07-29 21:03:12mark.dickinsonsetmessages: + msg112006
2010-07-29 21:01:00Winston451setfiles: + leak.patch
keywords: + patch
messages: + msg112005
2010-07-29 20:48:43mark.dickinsonsetstage: needs patch
messages: + msg112003
versions: + Python 3.1, Python 2.7, Python 3.2
2010-07-29 20:47:18mark.dickinsonsetassignee: mark.dickinson
2010-07-29 20:47:06mark.dickinsonsetnosy: + mark.dickinson
messages: + msg112002
2010-07-29 20:33:32Winston451create