Boost :: shared_ptr in Objective-C ++

This is a better understanding of the question I had earlier.

I have the following Objective-C ++ object

@interface OCPP
{
    MyCppobj * cppobj; 
}
@end

@implementation OCPP

-(OCPP *) init
 {
   cppobj = new MyCppobj;
 }
 @end

      

Then I create a completely different obj that should use cppobj in boost :: shared_ptr (I have no choice in this question, it is part of a huge library that I cannot change)

@interface NOBJ
-(void) use_cppobj_as_shared_ptr
{
    //get an OCPP obj called occ from somewhere..
    //troubling line here
}
@end

      

I tried the following and it failed: I tried synthesizing cppobj. Then I created a shared_ptr in the "alarming line" like this:

MyCppobj * cpp = [occ cppobj];
bsp = boost::shared_ptr<MyCppobj>(cpp);

      

It works great the first time. Then I destroy the NOBJ and recreate it. When I left for cppobj. Presumably shared_ptr decided that it was no longer needed and is done with it.

So I need help. How can I save cppobj live?

Is there a way to destroy the bsp (or cppobj reference) without destroying the cppobj?

+2


a source to share


2 answers


shared_ptr

supports custom deallocators. What you can do is do, do nothing.



void no_destroy(MyCppObj*)
{}

bsp = boost::shared_ptr<MyCppObj>(cpp, &no_destroy);

      

+3


a source


Why not use boost::shared_ptr<MyCppObj> cppobj;

in OCPP

instead MyCppobj * cppobj;

to store an instance of MyCppObj?



+1


a source







All Articles