[C++-sig] Memory Leaks In VS.NET 2003 With BOOST_PYTHON_MODULE

mjkeyes at sbcglobal.net mjkeyes at sbcglobal.net
Fri Oct 14 07:04:11 CEST 2005


*********My apologies... The whitespace in my code was stripped (which
makes the Python difficult to read)  Here is the message again:

Okay, I solved the second problem.  Turns out it's something I wouldn't
expect.

Here's the situation - I have a C++ extended class to use in Python
called AccountMgr.  When I define a Python class, I do something like
this:

>>> from MyModule import BaseClass #another extended class from C++
>>> from MyModule import AccountMgr

>>> class MyClass(BaseClass):
>>> 	def UseAccountMgr(self):
>>> 		self.acctMgr.DoSomething()
>>> 	acctMgr = AccountMgr()

Whenever this module is imported from another, it constructs the acctMgr
object (even if I haven't instanced MyClass yet).  However, this
destructor is never called (I'm guessing because Py_Finalize isn't
implemented?).

Two followup questions:
1.  How should I handle the construction of the acctMgr (or any other
extended member variable in a Python class)?  I tried supplying an
__init__(self) function, but I get some interpreter errors about
converting the C++ class to Python.  Consequently, for now, I do
something like this:

>>> class MyClass(BaseClass):
>>> 	def UseAccountMgr(self):
>>> 		if self.acctMgr == None:
>>> 			self.acctMgr = AccountMgr()
>>>		self.acctMgr.DoSomething()
>>>	acctMgr = None

Can I declare the member variable as self.acctMgr = AccountMgr()?  I
haven't tried it.

2.  Please offer any comments on the Python code above.  I'm not green
when it comes to C++, but I've only been using Python for about a week,
so a little code review here would be helpful.

Thanks again!

<mjkeyes at sbcglobal.net> wrote in message
news:<000001c5d078$aee62b20$210110ac at CROW>...
> Okay, I solved the second problem.  Turns out it's something I 
> wouldn't expect.
> 
> Here's the situation - I have a C++ extended class to use in Python 
> called AccountMgr.  When I define a Python class, I do something like
> this:
> 
> from MyModule import BaseClass #another extended class from C++ from 
> MyModule import AccountMgr
> 
> class MyClass(BaseClass):
> 	def UseAccountMgr(self):
> 		self.acctMgr.DoSomething()
> 
> 	acctMgr = AccountMgr()
> 
> Whenever this module is imported from another, it constructs the 
> acctMgr object (even if I haven't instanced MyClass yet).  However, 
> this destructor is never called (I'm guessing because Py_Finalize 
> isn't implemented?).
> 
> Two followup questions:
> 1.  How should I handle the construction of the acctMgr (or any other 
> extended member variable in a Python class)?  I tried supplying an
> __init__(self) function, but I get some interpreter errors about 
> converting the C++ class to Python.  Consequently, for now, I do 
> something like this:
> 
> class MyClass(BaseClass):
> 	def UseAccountMgr(self):
> 		if self.acctMgr == None:
> 			self.acctMgr = AccountMgr()
> 		self.acctMgr.DoSomething()
> 
> 	acctMgr = None
> 
> Can I declare the member variable as self.acctMgr = AccountMgr()?  I 
> haven't tried it.
> 
> 2.  Please offer any comments on the Python code above.  I'm not green

> when it comes to C++, but I've only been using Python for about a 
> week, so a little code review here would be helpful.
> 
> Thanks again!
> 
> 
> "Matthew B. Keyes" <Keyes at simcrest.com> wrote in message 
> news:<6A26270516DF054BBB3B87AE01C831EC2CCBD0 at SERVER01.simcrest.int>...
> > Sorry guys, it was late when I posted this, so the typo:
> > 
> > return object(new CreateClassA());
> > 
> > Meant to be:
> > 
> > return object(new A());
> > 
> > I was under the impression that the object class functioned as a
> > reference counting auto_ptr of sorts... That solves that one for me.
> > 
> > I'll post more later on the string issue 'cause I'm out of time 
> > right
> > now.  Thanks for the help!
> > 
> > "Ralf W. Grosse-Kunstleve" <rwgk at yahoo.com> wrote in message
> > news:<20051013155914.43599.qmail at web31505.mail.mud.yahoo.com>...
> > > --- mjkeyes at sbcglobal.net wrote:
> > > 
> > > > Hey again, I'm back.
> > > 
> > > Hi Arnold! :)
> > > 
> > > > Here is some pseudo-code of what I'm doing:
> > > 
> > > Could you please turn this into a self-contained reproducer? 
> > > Please
> > explain
> > > exactly how you compile and link. Note that you have to be very
> > careful to not
> > > mix debug and release builds under Windows.
> > > 
> > > > class A
> > > > {
> > > > public:
> > > >   void SetString(const std::string &sVal)
> > > >   {
> > > >     m_sString = sVal; <--- Memory leak
> > > 
> > > I am doing things like this all the time and I am sure it doesn't
> > > leak
> > for me.
> > > 
> > > >   }
> > > > 
> > > >   void DoStuff()
> > > >   {
> > > >     SetString(msc_sString);
> > > >   }
> > > > protected:
> > > >   static const std::string msc_sString;
> > > > }
> > > 
> > > Probably a side issue: do you really want "static" here?
> > > 
> > > > 
> > > > class B
> > > > {
> > > > public:
> > > >   object CreateClassA()
> > > >   {
> > > >     return object(new CreateClassA()); <---- memory leak
> > > 
> > > What is this? Does this even compile?
> > > In any event, of course you get a memory leak if you use new like
> > that.
> > > Ideally, you don't have any "unencapsulated" new in your code.
> > > Always
> > use
> > > boost::shared_ptr<T>(new T) or std::auto_ptr<>(new T) or any other
> > smart
> > > pointer.
> > > 
> > > > Any suggestions?  I'm not getting any errors from the 
> > > > interpreter.
> > > 
> > > We need a complete reproducer.
> > > 
> > > Cheers,
> > >         Ralf
> > > 
> > > 
> > > 
> > > 		
> > > __________________________________
> > > Yahoo! Music Unlimited
> > > Access over 1 million songs. Try it free.
> > > http://music.yahoo.com/unlimited/




More information about the Cplusplus-sig mailing list