Python Forum
Embedded Python Memory Leaks
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Embedded Python Memory Leaks
#1
Bug 
Hi dear Python team,

I am a building a c/c++ application for general purpose and I would like to have in the application the possibility of running “Python” scripting code. Python has proved to be a good programming language to deal with heavy Math as in AI.

Now when I embed Python in my application things go perfect, except for memory leaks!
As a sample, by just issuing the following code leaves memory leaks of around (2Mb):

#define PY_SSIZE_T_CLEAN
#define Py_LIMITED_API 0x03000000
#include <Python.h>
#pragma comment (lib,"python3.lib")

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
    Py_InitializeEx(0); //For Embeded Python
    Py_FinalizeEx();
    ..More code..
    return 0;
}
Is there something I must do to cause Python to release all resources allocated

thanks
Larz60+ write Sep-15-2022, 09:54 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.

Your indentation is off, and needs fixing
Reply
#2
Thanks for your replay Larz60, sorry I am new to this forum and I am learning how to do stuff:

The code I use is as simple as I just wrote "..mode code.." at this point is empty, since I am testing python engine before I add it to the project where I plan to use it. As you can see even the python dll gets unloaded when I finished with Python since I do late binding to python dlls due to the fact that the application might not use python for some applications while might use it for others and I don't want the user having to install python if it does not plan to use python scripting. I would like to know what does Py_InitializeEx(0); do that .Py_FinalizeEx(); cannot undo regarding computer resource utilization.

I am working with VS2019 generation a windows application 64 bits.

Thanks a log!!!


VS2019 log fragment:

Output:
'_COMCLASSES.exe' (Win32): Loaded 'C:\Python\Python311\python3.dll'. '_COMCLASSES.exe' (Win32): Loaded 'C:\Python\Python311\python311.dll'. '_COMCLASSES.exe' (Win32): Loaded 'C:\Windows\System32\bcrypt.dll'. '_COMCLASSES.exe' (Win32): Loaded 'C:\Windows\System32\version.dll'. '_COMCLASSES.exe' (Win32): Loaded 'C:\Windows\System32\vcruntime140.dll'. 0x1383630A630 -> Python Object created!... '_COMCLASSES.exe' (Win32): Unloaded 'C:\Windows\System32\version.dll' '_COMCLASSES.exe' (Win32): Unloaded 'C:\Windows\System32\vcruntime140.dll' '_COMCLASSES.exe' (Win32): Unloaded 'C:\Python\Python311\python311.dll' '_COMCLASSES.exe' (Win32): Unloaded 'C:\Python\Python311\python3.dll' 0x1383630A630 -> Python Object Destroyed!...
The object constructor runs this function and print the message "0x1383630A630 -> Python Object created!...":
# /////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT PythonObject_Init(LPPYTHONOBJECTDATA pObjectData,LPEXCEPINFO pei)
{
	HRESULT hr = S_OK;
		
	//first time Pythoon is called

	if (!hPythonDll)
	{
		if ((hr = PythonObject_BindFunctions(pObjectData, pei)) != S_OK) return hr;
	}
	if (hPythonDll && !pObjectData->bEngineInitialized)
	{
		callPython.Py_InitializeEx(0); //For Embeded Python 
		if (callPython.Py_IsInitialized())
		{
			pObjectData->bEngineInitialized = TRUE;
			callPython.PyGC_Enable();
			dwPyObjCounter++;
		}
		else hr = PythonObject_RiseException(pObjectData, pei);
	}
	
	return hr;
}

# /////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT PythonObject_BindFunctions(LPPYTHONOBJECTDATA pObjectData, LPEXCEPINFO pei)
{
	HRESULT hr = S_OK;
	
	if (pwcPyLibPathName)
	{
		hPythonDll = LoadLibrary(pwcPyLibPathName);
	
		if (hPythonDll)
		{
			char* pchar = "Py_InitializeEx";
			//Bind Functions I use
			callPython.Py_InitializeEx			= (f_Py_InitializeEx) GetProcAddress(hPythonDll, pchar);
			if (!callPython.Py_InitializeEx)				goto PythonBindError;

			pchar = "Py_IsInitialized";
			callPython.Py_IsInitialized			= (f_Py_IsInitialized)GetProcAddress(hPythonDll, pchar);
			if (!callPython.Py_IsInitialized)				goto PythonBindError;

			pchar = "Py_FinalizeEx";
			callPython.Py_FinalizeEx				= (f_Py_FinalizeEx)   GetProcAddress(hPythonDll, pchar);
			if (!callPython.Py_FinalizeEx)				goto PythonBindError;

			
			goto PyBindOk;
		PythonBindError:
			{
				wchar_t *pwcFunction = WStrFromChar(pchar);
				wchar_t* pwcDummy = NULL;
				pwcDummy = WStrConcatenate(pwcFunction, ALEXPYTHONOBJECT_ERROR_UNSSUPORTEDVERSION_STR);
				if (pwcDummy)
				{
					CreateErrorInfo2(pei, SysAllocString(pwcDummy), NULL, NULL, 0, (ALEXPYTHONOBJECT_ERROR_UNSSUPORTEDVERSION), HRESULT_CODE(ALEXPYTHONOBJECT_ERROR_UNSSUPORTEDVERSION));
				}else CreateErrorInfo2(pei, SysAllocString(ALEXPYTHONOBJECT_ERROR_UNSSUPORTEDVERSION_STR), NULL, NULL, 0, (ALEXPYTHONOBJECT_ERROR_UNSSUPORTEDVERSION), HRESULT_CODE(ALEXPYTHONOBJECT_ERROR_UNSSUPORTEDVERSION));
				return DISP_E_EXCEPTION;
			}
PyBindOk:
			hr = hr;
		}
		else
		{
			hr = GetLastError();
		}
	}
	else hr = E_INVALIDARG;
	
	return hr;
}
while the object destructor calls the following function and print the message "0x1383630A630 -> Python Object Destroyed!...":
VOID PythonObject_CleanUp(LPPYTHONOBJECTDATA pObjectData)
{
	HRESULT hr = S_OK;
	# //==============TESTING this code!!!================
	if (pObjectData->bEngineInitialized)
	{
	
		if (pObjectData->pPyMainModule)
		{
			callPython.Py_DecRef(pObjectData->pPyMainModule);
			pObjectData->pPyMainModule = NULL;
		}
		
		callPython.PyGC_Collect();
		dwPyObjCounter--;

		if (dwPyObjCounter == 0)
		{
			
			callPython.Py_FinalizeEx();
			pObjectData->bEngineInitialized = FALSE;
			if (FreeLibrary(hPythonDll))
			{
				if (pwcPyLibPathName) CoTaskMemFree(pwcPyLibPathName);
				pwcPyLibPathName = NULL;
				hPythonDll = NULL;
			}
		
		}
# //==============TESTING this code!!!================		
	}
}
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,114 Nov-02-2023, 04:45 PM
Last Post: blabling2
  Adding libraries to embedded Python as a ZIP The_Oman 0 1,246 May-05-2023, 04:05 PM
Last Post: The_Oman
  python list out top 10 Memory utilisation mg24 0 811 Nov-20-2022, 10:51 PM
Last Post: mg24
  Embedded Python in C++ Xeno 19 3,582 Aug-03-2022, 07:29 AM
Last Post: Gribouillis
  python memory rd_rakesh 1 1,655 Jul-14-2021, 11:15 AM
Last Post: jefsummers
  Embedded python fails to compile on Raspberry Pi tryfon 2 3,493 Dec-22-2020, 02:06 PM
Last Post: tryfon
  Can Embedded Python run any shared library on Android ? sprotz 0 2,333 Nov-08-2020, 12:21 PM
Last Post: sprotz
  Windows Python Memory Scanner Awesometech 1 22,034 Oct-14-2020, 07:44 AM
Last Post: badengagen
  memory leak on embedded python in c++ asdf3721 3 3,408 Jul-16-2020, 06:33 AM
Last Post: Gribouillis
  Embedded Python PyObject_CallObject function JRHeisey 1 2,400 Nov-27-2019, 01:50 AM
Last Post: casevh

Forum Jump:

User Panel Messages

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