简体   繁体   中英

c-extension Python memory leak,why?

recently i have a project write by C,and extension Python in , i call Python in a while loop,and the result is the memory grows never stop.

here is the code:

main
{
   while True {
      if( SUCCESS != PyInit())
      {
          return -1;
      }
      PyDone();
      usleep(1000);
      } 
}


int PyInit(void)
{
         Py_Initialize(); 
         if(!Py_IsInitialized())
         {
              printf("PyInit: Python Init fail!\n");
              return FAILURE;
         }
         else
         {
              printf( "Python Init succ!\n");
              return SUCCESS;

         }

}

void PyDone(void)
{
         Py_Finalize();
}

and at last my english is poor,hope i have described this question clearly.

for (;;) {
    PyInitialize();
    if (PyIsInitialized()) {
        PyFinalize();
        usleep(1000);
    }
}

If your code resembles this, and there appears to be memory leaking, then the python implementation is at fault because PyFinalize supposedly "frees all memory allocated by the Python interpreter". The python site states, in regards to PyFinalize, "Small amounts of memory allocated by the Python interpreter may not be freed (if you find a leak, please report it)." A search through the python bug tracker for leak indicates that this hasn't yet been reported, so I suggest reporting this bug to their bugtracker .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM