merge the formfield patch from ooo-build
[ooovba.git] / pyuno / source / module / pyuno_gc.cxx
blob080a80cf7bf731faa474c446234e0ebea54336c7
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: pyuno_gc.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include <pyuno_impl.hxx>
31 #include <osl/thread.hxx>
32 namespace pyuno
35 bool g_destructorsOfStaticObjectsHaveBeenCalled;
36 class StaticDestructorGuard
38 public:
39 ~StaticDestructorGuard()
41 g_destructorsOfStaticObjectsHaveBeenCalled = true;
44 StaticDestructorGuard guard;
46 class GCThread : public ::osl::Thread
48 PyObject *mPyObject;
49 PyInterpreterState *mPyInterpreter;
50 GCThread( const GCThread & ); // not implemented
51 GCThread &operator =( const GCThread & ); // not implemented
53 public:
54 GCThread( PyInterpreterState *interpreter, PyObject * object );
55 virtual void SAL_CALL run();
56 virtual void SAL_CALL onTerminated();
60 GCThread::GCThread( PyInterpreterState *interpreter, PyObject * object ) :
61 mPyObject( object ), mPyInterpreter( interpreter )
64 void GCThread::run()
66 // otherwise we crash here, when main has been left already
67 if( g_destructorsOfStaticObjectsHaveBeenCalled )
68 return;
69 try
71 PyThreadAttach g( (PyInterpreterState*)mPyInterpreter );
73 Runtime runtime;
75 // remove the reference from the pythonobject2adapter map
76 PyRef2Adapter::iterator ii =
77 runtime.getImpl()->cargo->mappedObjects.find( mPyObject );
78 if( ii != runtime.getImpl()->cargo->mappedObjects.end() )
80 runtime.getImpl()->cargo->mappedObjects.erase( ii );
83 Py_XDECREF( mPyObject );
86 catch( com::sun::star::uno::RuntimeException & e )
88 rtl::OString msg;
89 msg = rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
90 fprintf( stderr, "Leaking python objects bridged to UNO for reason %s\n",msg.getStr());
95 void GCThread::onTerminated()
97 delete this;
100 void decreaseRefCount( PyInterpreterState *interpreter, PyObject *object )
102 // otherwise we crash in the last after main ...
103 if( g_destructorsOfStaticObjectsHaveBeenCalled )
104 return;
106 // delegate to a new thread, because there does not seem
107 // to be a method, which tells, whether the global
108 // interpreter lock is held or not
109 // TODO: Look for a more efficient solution
110 osl::Thread *t = new GCThread( interpreter, object );
111 t->create();