473,805 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with multiple Py_Initialize/Py_Finalize calls

Hi folks,

I've only been using Python for a few weeks, so please be gentle. :)
Lovely language by the way -- I really enjoying using it -- but
I've struck a very nasty problem when running Python scripts from
my app.

Summary:

Multiple Py_Initialize/Py_Finalize calls result in bad memory leaks,
and Py_Finalize gradually gets slower and slower.

The problem occurs on both Mac OS 10.3.9 and Windows 2000.
I'm using Python 2.3 on the Mac and 2.4.2 on Windows.

More details:

I'm co-writing an open source, cross-platform Life app called Golly
(more details at http://golly.sourceforge.net/ if you're interested).
The GUI is written in C++ using wxWidgets, in case that matters.

I've added scripting capability, ie. embedded Python, so users can
run .py scripts from within the app to do various fancy things.
This is all working very nicely, but then I noticed serious memory
problems after running many scripts (or the same script many times).
In fact, running a single complicated script that repeatedly builds
very large lists (representing large Life patterns) can cause a
leak of many megabytes!

After much hair-pulling and experimentation I decided to write a
very simple routine that doesn't even execute a script:

void TestPython()
{
for (int i=0; i<1000; i++) {
Py_Initialize() ;
Py_Finalize();
}
}

After running this routine on my Mac, the app's memory use increased
by about 12.9MB; ie. a 12.9K leak per Py_Initialize/Py_Finalize pair.
Similar result on Windows: the app's memory increased by 11MB.

Curiously, on both machines, the Py_Finalize() call takes longer and
longer to complete whatever it's doing. The above routine takes a
few *minutes* to complete on my 400MHz Mac.

Note that if I execute a script between Py_Initialize and Py_Finalize
by calling

PyRun_SimpleStr ing("execfile(' foo.py')");

then much larger memory leaks will occur. In fact it seems that
Py_Finalize is not actually deallocating any memory.

Any idea what is going wrong? Am I doing something stupid?
If Py_Initialize/Py_Finalize should not be called multiple times
then how can I tell Python to free all the memory used by an
executed script?

Yours in desperation, Andrew
an****@trevorro w.com
Mar 7 '06 #1
0 2403

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2463
by: Susanne | last post by:
Hello! In have embedded python into my MFC application. It works fine using the PyRun_SimpleString method, giving it a string with some simple commands. When i am using PyRun_SimpleFile instead, my application crashes. I have also created a console application. There i also call PyRun_SimpleFile. The console application executes the given script.
0
1392
by: Axel Diener | last post by:
I embed python interpreters in a C++ program. For each script to execute I create a new interpreter. In this environment I can use the win32 extensions only one time. Here is a little piece of code to illustrate the problem: int main(int argc, char **argv) { Py_Initialize(); Py_SetProgramName(argv);
0
1854
by: Atul Kshirsagar | last post by:
I am embedding python in my C++ application. I am using Python *2.3.2* with a C++ extention DLL in multi-threaded environment. I am using SWIG-1.3.19 to generate C++ to Python interface. Now to explain it in details, 1. Python initialization and finalization is done in the *main* thread. 2. For each new thread I create a separate sub-interpreter . 3. Using PyRun_String("import myModule"...) before execution of python
1
1989
by: youngdubliner | last post by:
I'm having a problem ........ I've stripped all my code to help isolate the problem. Its seems to be with importing numarray when python is embedded in C. I have a simple C program it Opens Python imports a script and then Closes Python like so .......
0
1200
by: Roman Suzi | last post by:
In pure curiosity I tried to compile loop.c from Demo/embed and started it with 'print 2+2'. It seems, that both 2.3 and 2.4 pythons have memory leaks in Py_Initialize/Py_Finalize calls. (That is, interpreter doesn't clean up well after itself). This is my setup: gcc -fpic loop.c -DHAVE_CONFIG_H -lm -lpython2.4 \ -lpthread -lutil -ldl \ -I/usr/local/include/python2.4 \
2
2775
by: Ugo Di Girolamo | last post by:
I have the following code, that seems to make sense to me. However, it crashes about 1/3 of the times. My platform is Python 2.4.1 on WXP (I tried the release version from the msi and the debug version built by me, both downloaded today to have the latest version).
1
1437
by: mmf | last post by:
Hallo! I tried to use Python from C like it is described in the Python Docmentation. So I wrote the following C source file: #include <Python.h> int main(int argc, char *argv) { Py_Initialize();
1
1298
by: Koichi | last post by:
Hi, I'm now making a plug-in for a CG software. I embed Python in a plugin and it works. The problem is that it conflicts with other plugins that also embeds Python because it runs in the same thread. I don't know when Py_Initialize() and Py_Finalize() are executed because I don't know when user loads/unloads a plugin that embeds Python. Is there any right way to embed Python in a plug-in under the following conditions? If not, what...
1
2579
by: Benke | last post by:
Hello, I'm quite new to Python and embedding python in c++. I'm trying to write a function that i can use to call a python function. It should take 3 arguments, the name of the python file, the python function name and a std:vector with arguments for the python function. This is my code: int main() { string arg1 = "runme"; //name of python file string arg2 = "sub"; // name of python function Py_Initialize();
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10104
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9182
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7645
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4317
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.