Python Forum
memory leak on embedded python in c++
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
memory leak on embedded python in c++
#1
why this line cause memory?
how do I solve it

void cyc()
{
Py_Initialize();


PyObject *pArgs = Py_BuildValue("idd",4,1.0,1.0);

Py_DECREF(pArgs);


Py_Finalize(); //memory leak why?
}

int main()
{
for(;;)
{cyc();}
}
Reply
#2
I don't know why there is a memory leak, but normal C programs call Py_Initialize() and Py_Finalize() only once. Why would anyone want to call them in a loop?
Reply
#3
(Jul-15-2020, 10:02 PM)Gribouillis Wrote: I don't know why there is a memory leak, but normal C programs call Py_Initialize() and Py_Finalize() only once. Why would anyone want to call them in a loop?

I've test on OS X 10.15 with python 3.8.3, there is a memory leak.
Do you mean that I should not using Py_Initialize and Py_Finalize in C function? so that I should not use them multi times?
Reply
#4
I'm not saying that you must never call them several times, but I don't see a good reason for calling them a very large number of times.

The documentation of Py_FinalizeEx() says
Quote:Bugs and caveats: The destruction of modules and objects in modules is done in random order; this may cause destructors (__del__() methods) to fail when they depend on other objects (even functions) or modules. Dynamically loaded extension modules loaded by Python are not unloaded. Small amounts of memory allocated by the Python interpreter may not be freed (if you find a leak, please report it). Memory tied up in circular references between objects is not freed. Some memory allocated by extension modules may not be freed. Some extensions may not work properly if their initialization routine is called more than once; this can happen if an application calls Py_Initialize() and Py_FinalizeEx() more than once.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python memory rd_rakesh 1 213 Jul-14-2021, 11:15 AM
Last Post: jefsummers
  Where is the memory leak in this Flask Code? Morkus 1 221 Jun-18-2021, 02:48 PM
Last Post: Oliver
  Embedded python fails to compile on Raspberry Pi tryfon 2 601 Dec-22-2020, 02:06 PM
Last Post: tryfon
  Can Embedded Python run any shared library on Android ? sprotz 0 502 Nov-08-2020, 12:21 PM
Last Post: sprotz
  Windows Python Memory Scanner Awesometech 1 4,576 Oct-14-2020, 07:44 AM
Last Post: badengagen
  Embedded Python PyObject_CallObject function JRHeisey 1 899 Nov-27-2019, 01:50 AM
Last Post: casevh
  Trying to implement Python into embedded OS thesurya7 2 1,013 Apr-02-2019, 06:38 PM
Last Post: ebolisa
  Multiple calls to Python interpreter embedded in C++ application yield segmentation f mmoelle1 0 1,245 Mar-21-2019, 08:54 PM
Last Post: mmoelle1
  How to get memory usage and execution time of each line in python SriRajesh 2 1,255 Mar-07-2019, 12:59 PM
Last Post: SriRajesh
  Queue get memory leak when used in multithreading wangcp 1 2,852 Nov-27-2018, 04:06 AM
Last Post: wangcp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020