2003-03-23 11:12:59 +00:00
|
|
|
/*************************************************************************
|
|
|
|
*
|
2005-09-08 15:52:36 +00:00
|
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
2003-03-23 11:12:59 +00:00
|
|
|
*
|
2005-09-08 15:52:36 +00:00
|
|
|
* $RCSfile: pyuno_gc.cxx,v $
|
2003-03-23 11:12:59 +00:00
|
|
|
*
|
2005-09-08 15:52:36 +00:00
|
|
|
* $Revision: 1.4 $
|
2003-03-23 11:12:59 +00:00
|
|
|
*
|
2005-09-08 15:52:36 +00:00
|
|
|
* last change: $Author: rt $ $Date: 2005-09-08 16:52:36 $
|
2003-03-23 11:12:59 +00:00
|
|
|
*
|
2005-09-08 15:52:36 +00:00
|
|
|
* The Contents of this file are made available subject to
|
|
|
|
* the terms of GNU Lesser General Public License Version 2.1.
|
2003-03-23 11:12:59 +00:00
|
|
|
*
|
|
|
|
*
|
2005-09-08 15:52:36 +00:00
|
|
|
* GNU Lesser General Public License Version 2.1
|
|
|
|
* =============================================
|
|
|
|
* Copyright 2005 by Sun Microsystems, Inc.
|
|
|
|
* 901 San Antonio Road, Palo Alto, CA 94303, USA
|
2003-03-23 11:12:59 +00:00
|
|
|
*
|
2005-09-08 15:52:36 +00:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License version 2.1, as published by the Free Software Foundation.
|
2003-03-23 11:12:59 +00:00
|
|
|
*
|
2005-09-08 15:52:36 +00:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2003-03-23 11:12:59 +00:00
|
|
|
*
|
2005-09-08 15:52:36 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
* MA 02111-1307 USA
|
2003-03-23 11:12:59 +00:00
|
|
|
*
|
|
|
|
************************************************************************/
|
|
|
|
#include <osl/thread.hxx>
|
|
|
|
#include <pyuno_impl.hxx>
|
|
|
|
namespace pyuno
|
|
|
|
{
|
|
|
|
|
2005-02-11 15:40:27 +00:00
|
|
|
bool g_destructorsOfStaticObjectsHaveBeenCalled;
|
|
|
|
class StaticDestructorGuard
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
~StaticDestructorGuard()
|
|
|
|
{
|
|
|
|
g_destructorsOfStaticObjectsHaveBeenCalled = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
StaticDestructorGuard guard;
|
2003-03-23 11:12:59 +00:00
|
|
|
|
|
|
|
class GCThread : public ::osl::Thread
|
|
|
|
{
|
|
|
|
PyObject *mPyObject;
|
|
|
|
PyInterpreterState *mPyInterpreter;
|
|
|
|
GCThread( const GCThread & ); // not implemented
|
|
|
|
GCThread &operator =( const GCThread & ); // not implemented
|
|
|
|
|
|
|
|
public:
|
|
|
|
GCThread( PyInterpreterState *interpreter, PyObject * object );
|
|
|
|
virtual void SAL_CALL run();
|
|
|
|
virtual void SAL_CALL onTerminated();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
GCThread::GCThread( PyInterpreterState *interpreter, PyObject * object ) :
|
|
|
|
mPyInterpreter( interpreter ), mPyObject( object )
|
|
|
|
{}
|
|
|
|
|
|
|
|
void GCThread::run()
|
|
|
|
{
|
2005-02-11 15:40:27 +00:00
|
|
|
// otherwise we crash here, when main has been left already
|
|
|
|
if( g_destructorsOfStaticObjectsHaveBeenCalled )
|
|
|
|
return;
|
2003-03-23 11:12:59 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
PyThreadAttach guard( (PyInterpreterState*)mPyInterpreter );
|
|
|
|
{
|
2004-02-02 18:30:09 +00:00
|
|
|
Runtime runtime;
|
|
|
|
|
|
|
|
// remove the reference from the pythonobject2adapter map
|
|
|
|
PyRef2Adapter::iterator ii =
|
|
|
|
runtime.getImpl()->cargo->mappedObjects.find( mPyObject );
|
|
|
|
if( ii != runtime.getImpl()->cargo->mappedObjects.end() )
|
|
|
|
{
|
|
|
|
runtime.getImpl()->cargo->mappedObjects.erase( ii );
|
|
|
|
}
|
|
|
|
|
2003-03-23 11:12:59 +00:00
|
|
|
Py_XDECREF( mPyObject );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( com::sun::star::uno::RuntimeException & e )
|
|
|
|
{
|
|
|
|
rtl::OString msg;
|
|
|
|
msg = rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
|
|
|
|
fprintf( stderr, "Leaking python objects bridged to UNO for reason %s\n",msg.getStr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GCThread::onTerminated()
|
|
|
|
{
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void decreaseRefCount( PyInterpreterState *interpreter, PyObject *object )
|
|
|
|
{
|
2005-02-11 15:40:27 +00:00
|
|
|
// otherwise we crash in the last after main ...
|
|
|
|
if( g_destructorsOfStaticObjectsHaveBeenCalled )
|
|
|
|
return;
|
|
|
|
|
2003-03-23 11:12:59 +00:00
|
|
|
// delegate to a new thread, because there does not seem
|
|
|
|
// to be a method, which tells, whether the global
|
|
|
|
// interpreter lock is held or not
|
|
|
|
// TODO: Look for a more efficient solution
|
|
|
|
osl::Thread *t = new GCThread( interpreter, object );
|
|
|
|
t->create();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|