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
  How to run detectron2, as python embedded code in C++, on GPU? hassaniqbal931 3 1,040 Nov-02-2023, 04:45 PM
Last Post: blabling2
  Adding libraries to embedded Python as a ZIP The_Oman 0 1,194 May-05-2023, 04:05 PM
Last Post: The_Oman
  python list out top 10 Memory utilisation mg24 0 785 Nov-20-2022, 10:51 PM
Last Post: mg24
Bug Embedded Python Memory Leaks Alexei 1 1,003 Sep-16-2022, 01:15 PM
Last Post: Alexei
  Embedded Python in C++ Xeno 19 3,400 Aug-03-2022, 07:29 AM
Last Post: Gribouillis
  python memory rd_rakesh 1 1,627 Jul-14-2021, 11:15 AM
Last Post: jefsummers
  Where is the memory leak in this Flask Code? Morkus 1 5,472 Jun-18-2021, 02:48 PM
Last Post: Oliver
  Embedded python fails to compile on Raspberry Pi tryfon 2 3,404 Dec-22-2020, 02:06 PM
Last Post: tryfon
  Can Embedded Python run any shared library on Android ? sprotz 0 2,271 Nov-08-2020, 12:21 PM
Last Post: sprotz
  Windows Python Memory Scanner Awesometech 1 19,077 Oct-14-2020, 07:44 AM
Last Post: badengagen

Forum Jump:

User Panel Messages

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