On 22-Mar-05, at 12:34 PM, Jim Bublitz wrote:
>> It appears that when passing Python instances back into C++ you are
>> creating a second, unrelated smart pointer. Unless KSharedPtr is
>> maintaining a mapping of C++ pointers, the pointer that gets passed in
>> is getting an extra reference added to it that will never go away.
>> When the pointer is passed back out of Python, you area creating an
>> unassociated smart pointer that will deallocate the contained pointer
>> when it goes out of scope, potentially leaving a dangling pointer.
>> Yeah - I forgot about that part (or ignorant of it - not sure which).
> The
> problem is that incrementing the ref count of the original pointer
> (sipCpp)
> has the same result, and the underlying object doesn't have a copy
> constructor (I believe boost works that way too).
boost::shared_ptr relies on the copy ctor and assignment op to copy the
internal reference count.
I've just had a look at the KSharedPtr docs, and it appears that the
creation of unrelated KSharedPtrs is handled by the implementation of
KShared. KSharedPtr embeds the reference count *inside* the class it's
wrapping. The advantage of this is that you cannot shoot yourself in
the foot with something like KSharedPtr<T> foo( bar.data() ). (Where
bar is another KSharedPtr<T> instance.) The disadvantage is that you
must change the interface of the wrapped class. (Less than desirable
in some cases.)
The boost implementation doesn't require modification of the wrapped
class, but uses an embedded reference count object, allocated on the
heap. This means that if you do boost::shared_ptr<T> foo( bar.get() )
you *will* end up with a dangling pointer in bar when foo goes out of
scope (or vice versa.)
Anyway, I retract my previous statement, your original %MappedType code
will work nicely for KSharedPtr. Not helping me though! =P
> It seems like you should be able to subclass the shared ptr and bind
> the
> subclass instead, overload the C++ dtor to Py_DECREF the corresponding
> Python
> object (need to keep a ptr to it) and overload __del__ to decrement
> the ref
> count of the C++ object.
Well, shared_ptr is not designed to be subclassed. (nonvirtual
destructor) And I don't really want to make such an intrusive change.
I have worked on an implementation I alluded to in a previous message.
I think this might be along the right lines, it's included below. A
more refined implementation would probably use a single map with void
pointers and template function wrappers, mainly to avoid code bloat.
Hopefully someone finds this useful, and I'll post a final version when
it's done. (It would be a lot cleaner if there were somewhere better
to put the shared_ptr instance; like inside the PyObject.)
The problem I'm having with this now is that I am not releasing a
reference. BVApp_ISalesOrder::__del__ in defined in the wrapper to
call shared_ptr_mgr::release_ref() The only way I have found so far to
force __del__ to be called is by calling it explicitly in Python. I
have tried using 'del' on the builtin and then calling gc.collect() and
it will not go away. =( Perhaps this has something to do with the
'Abstract' annotation on the class?
James
%ModuleHeaderCode
#include <map>
#include <utility>
#include <boost/shared_ptr.hpp>
template< class T >
struct shared_ptr_mgr
{
typedef T cpp_type;
typedef boost::shared_ptr< cpp_type > smart_pointer_type;
typedef std::pair< smart_pointer_type, int > smart_pointer_count;
typedef std::map< cpp_type*, smart_pointer_count > pointer_map_type;
static pointer_map_type* _pointer_map;
static void init()
{
if( !_pointer_map )
_pointer_map = new pointer_map_type();
}
// Add a new reference or increment a previous reference
static int add_ref( smart_pointer_type& ref )
{
init();
pointer_map_type::iterator it = _pointer_map->find( ref.get() );
if( it != _pointer_map->end() )
return ++(*it).second.second;
_pointer_map->insert(std::make_pair( ref.get(), std::make_pair(ref,
1) ));
return 1;
}
// Release a reference
static int release_ref( cpp_type* ptr )
{
init();
pointer_map_type::iterator it = _pointer_map->find( ptr );
if( it == _pointer_map->end() )
return -1;
int cnt = --(*it).second.second;
if( 0 == cnt )
_pointer_map->erase( it );
return cnt;
}
// Create a new smart pointer instance (from copy in map if available)
static smart_pointer_type* create_pointer( cpp_type* ptr )
{
init();
std::cout << "create_pointer():" << std::hex << ptr << std::endl;
pointer_map_type::iterator it = _pointer_map->find( ptr );
if( it == _pointer_map->end() )
return new smart_pointer_type( ptr );
else
return new smart_pointer_type( (*it).second.first );
}
};
%End
%ModuleCode
// Need to add one of these for each pointer type (in this incarnation,
at least)
shared_ptr_mgr< BVApp::ISalesOrder >::pointer_map_type* shared_ptr_mgr<
BVApp::ISalesOrder >::_pointer_map( NULL );
%End
%MappedType BVApp_ISalesOrderPtr
{
%TypeHeaderCode
#include <BVData/SalesOrder.h>
#include "sipbvBVApp_ISalesOrder.h"
typedef BVApp::ISalesOrderPtr BVApp_ISalesOrderPtr;
%End
%ConvertFromTypeCode
if( !sipCpp )
return NULL;
// Add an extra reference...
shared_ptr_mgr< BVApp::ISalesOrder >::add_ref( *sipCpp );
PyObject* o = sipMapCppToSelf( sipCpp->get(),
sipClass_BVApp_ISalesOrder );
return o;
%End
%ConvertToTypeCode
if( sipIsErr == NULL )
return PyInstance_Check( sipPy );
int iserr = 0;
BVApp::ISalesOrder* ord = reinterpret_cast<BVApp::ISalesOrder*>(
sipForceConvertTo_BVApp_ISalesOrder( sipPy, &iserr ) );
if( iserr )
{
*sipIsErr = 1;
return 0;
}
*sipCppPtr = shared_ptr_mgr< BVApp::ISalesOrder >::create_pointer( ord
);
return 1;
%End
};
--
This is not my home; the cats just let me stay here.
More information about the PyQt
mailing list
‘She has never mentioned her father to me. Was he—well, the sort of man whom the County Club would not have blackballed?’ "We walked by the side of our teams or behind the wagons, we slept on the ground at night, we did our own cooking, we washed our knives by sticking them into the ground rapidly a few times, and we washed our plates with sand and wisps of grass. When we stopped, we arranged our wagons in a circle, and thus formed a 'corral,' or yard, where we drove our oxen to yoke them up. And the corral was often very useful as a fort, or camp, for defending ourselves against the Indians. Do you see that little hollow down there?" he asked, pointing to a depression in the ground a short distance to the right of the train. "Well, in that hollow our wagon-train was kept three days and nights by the Indians. Three days and nights they stayed around, and made several attacks. Two of our men were killed and three were wounded by their arrows, and others had narrow escapes. One arrow hit me on the throat, but I was saved by the knot of my neckerchief, and the point only tore the skin a little. Since that time I have always had a fondness for large neckties. I don't know how many of the Indians we killed, as they carried off their dead and wounded, to save them from being scalped. Next to getting the scalps of their enemies, the most important thing with the Indians is to save their own. We had several fights during our journey, but that one was the worst. Once a little party of us were surrounded in a small 'wallow,' and had a tough time to defend ourselves successfully. Luckily for us, the Indians had no fire-arms then, and their bows and arrows were no match for our rifles. Nowadays they are well armed, but there are[Pg 41] not so many of them, and they are not inclined to trouble the railway trains. They used to do a great deal of mischief in the old times, and many a poor fellow has been killed by them." As dusk came on nearly the whole population of Maastricht, with all their temporary guests, formed an endless procession and went to invoke God's mercy by the Virgin Mary's intercession. They went to Our Lady's Church, in which stands the miraculous statue of Sancta Maria Stella Maris. The procession filled all the principal streets and squares of the town. I took my stand at the corner of the Vrijthof, where all marched past me, men, women, and children, all praying aloud, with loud voices beseeching: "Our Lady, Star of the Sea, pray for us ... pray for us ... pray for us ...!" It had not occurred to her for some hours after Mrs. Campbell had told her of Landor's death that she was free now to give herself to Cairness. She had gasped, indeed, when she did remember it, and had put the thought away, angrily and self-reproachfully. But it returned now, and she felt that she might cling to it. She had been grateful, and she had been faithful, too.[Pg 286] She remembered only that Landor had been kind to her, and forgot that for the last two years she had borne with much harsh coldness, and with a sort of contempt which she felt in her unanalyzing mind to have been entirely unmerited. Gradually she raised herself until she sat quite erect by the side of the mound, the old exultation of her half-wild girlhood shining in her face as she planned the future, which only a few minutes before had seemed so hopeless. After he had gloated over Sergeant Ramsey, Shorty got his men into the road ready to start. Si placed himself in front of the squad and deliberately loaded his musket in their sight. Shorty took his place in the rear, and gave out: The groups about each gun thinned out, as the shrieking fragments of shell mowed down man after man, but the rapidity of the fire did not slacken in the least. One of the Lieutenants turned and motioned with his saber to the riders seated on their horses in the line of limbers under the cover of the slope. One rider sprang from each team and ran up to take the place of men who had fallen. "As long as there's men and women in the world, the men 'ull be top and the women bottom." Then, in the house, the little girls were useful. Mrs. Backfield was not so energetic as she used to be. She had never been a robust woman, and though her husband's care had kept her well and strong, her frame was not equal to Reuben's demands; after fourteen years' hard labour, she suffered from rheumatism, which though seldom acute, was inclined to make her stiff and slow. It was here that Caro and Tilly came in, and Reuben began to appreciate his girls. After all, girls were needed in a house—and as for young men and marriage, their father could easily see that such follies did not spoil their usefulness or take them from him. Caro and Tilly helped their grandmother in all sorts of ways—they dusted, they watched pots, they shelled peas and peeled potatoes, they darned house-linen, they could even make a bed between them. HoME一级毛片视频免费公开
ENTER NUMBET 0018astartup.com.cn s4x2rcbu.com.cn www.bjetc.net.cn wxhgztgsy.com.cn 58show.com.cn www.cpft864.com.cn dvk.net.cn kpee.com.cn zshggs.com.cn chkt283.com.cn