freelanceprogrammers.org Forum Index » Delphi
P4D finalizing problem
Joined: 12 Aug 2005
Posts: 7
P4D finalizing problem
I have a P4D container app loading an activeX also using P4D. Both the container app and
the activeX has its own appglobal TPythonEngine, initialized in a (common) unit initialization and
finalization sections, ie. at app start/temination.
Only the container reroutes the IO, the activeX uses the same routing.
The problem arises at shutdown, both the activeX and the container calls the Py_Finalize which generates
a python dll access violation.
When setting both AutoFinalize and AutoUnload to false the shutdown seems fine, windows releases the
python dll and no error messages. If I set AutoFinalize and AutoUnload true before freeing up the last
pythonEngine the Python dll crashes in the Py_Finalize call.
I also notices that the TPythonEngine.Initialize (calling Py_Initialize) is always called after loading the dll, ie.
both the container and the activeX calls Py_Initialize. Is multiple call harmless ?
Are there some kind of reference counting in python dll of Py_Initialize and Py_Finalize ?
Is it harmless not calling Py_Finalize ?
Roar Larsen--------------------------------------------------------------------------------------TORDIVEL ASLade Alle 65 C, N-7041 Trondheim, NorwayTel : +47 7350 9220 Fax : + 47 2315 8701E-mail : roar@... Web : http://www.tordivel.no--------------------------------------------------------------------------------------...... THE VISION SOFTWARE HOUSE--------------------------------------------------------------------------------------
Joined: 29 Apr 2005
Posts: 103
P4D finalizing problem
Hi Roar,
>I have a P4D container app loading an activeX also using P4D.
Both the >container app and
>the activeX has its own appglobal TPythonEngine,
initialized in a (common) >unit initialization and
>finalization sections, ie. at app start/temination.
Ok.
So, your ActiveX is in a separate Dll loaded by the container process?
>Only the container reroutes the IO, the activeX uses the same
routing.
Ok.
>The problem arises at shutdown, both the activeX and the container
calls >the Py_Finalize which generates
>a python dll access violation.
Ok.
>When setting both AutoFinalize and AutoUnload to false the
shutdown seems >fine, windows releases the
>python dll and no error messages. If I set AutoFinalize and
AutoUnload true >before freeing up the last
>pythonEngine the Python dll crashes in the Py_Finalize call.
I think that you should do it the other way: try to finalize when the
first engine is about to unload, and skip finalization for the last one.
If you have the exe and a dll (activex) and if the last engine that
will finalize is in the exe, if means that when it will try to finalize stuff
that was allocated by the activex dll, it will crash because the dll was already
unloaded! This is a common problem when building plugins with Dlls (and without
using packages). Each dll or process will have its own memory manager and you
can`t free an object anymore once its associated memory manager has been
released.
>I also notices that the TPythonEngine.Initialize (calling
Py_Initialize) is >always called after loading the dll, ie.
>both the container and the activeX calls Py_Initialize. Is multiple
call >harmless ?
Yes, it is harmless. See the documentation:
void Py_Initialize( )
Initialize the Python interpreter. In an application embedding Python,
this should be called before using any other Python/C API functions; with the
exception of Py_SetProgramName(), PyEval_InitThreads(), PyEval_ReleaseLock(),
and PyEval_AcquireLock(). This initializes the table of loaded modules
(sys.modules), and creates the fundamental modules __builtin__, __main__ and
sys. It also initializes the module search path (sys.path). It does not set
sys.argv; use PySys_SetArgv() for that. This is a no-op when called for a second time (without calling
Py_Finalize() first). There is no return value; it is a fatal
error if the initialization fails.
>Are there some kind of reference counting in python dll of
Py_Initialize and Py_Finalize ?
Not sure, I think there is only a global flag.
>Is it harmless not calling Py_Finalize ?
It is, only if your allocated objects don’t require any
finalization.
Bye,
Morgan
All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help







