How to use smart pointers as Objective-C fields

Matthew Jew mjew at icnc.com
Thu Aug 3 08:56:06 CDT 2006


On Aug 3, 2006, at 12:03 AM, Ruslan Zasukhin wrote:

> On 8/3/06 3:38 AM, "Matthew Jew" <mjew at icnc.com> wrote:
>
> Hi Matthew,
>
>> I made an Objective C class that acts as a sort of wrapper from the
>> old FBL_ArraySet.
>> It was defined like this:
>>
>> @interface PVArraySet : NSObject <NSCopying, NSCoding>
>> {
>> FBL_ArraySet *myArray;
>> }
>>
>>
>> Now, with the new Smart Pointers, I have modified it to be like this:
>>
>> @interface PVArraySet2 : NSObject <NSCopying, NSCoding>
>> {
>> ArraySet_Ptr myArray;
>> }
>>
>> However, when I do that, I get the following compilation warnings:
>>
>> /Volumes/Alpha2/Dev-NuPV/PVReplicator2/../Common-Classes/Classes/
>> PVArraySet2.h:18: warning: type `ArraySet_Ptr' has a user-defined
>> constructor
>> /Volumes/Alpha2/Dev-NuPV/PVReplicator2/../Common-Classes/Classes/
>> PVArraySet2.h:18: warning: type `ArraySet_Ptr' has a user-defined
>> destructor
>> /Volumes/Alpha2/Dev-NuPV/PVReplicator2/../Common-Classes/Classes/
>> PVArraySet2.h:18: warning: C++ constructors and destructors will not
>> be invoked for Objective-C fields
>>
>>
>> My question is this: will this Objective C class work properly, given
>> that the Smart Pointer's constructors and destructors will not be
>> invoked?
>
> no
>
>> If not, how am I supposed to address this?
>
> We have had similar problems in VNET.
>
> Solution is:
>
> * continue to use simple pointers
>      FBL_ArraySet *myArray;
>
> * use AddRef() and Release
>
> PVArraySet()
> {
>     myArray = pSet.get();
>     pSet->AddRef();
> }
>
>
> ~PVArraySet()
> {
>     myArray->Release();
> }
>
>
>

Okay, I can continue to use simple pointers and use AddRef and Release.

I have a need to pass ArraySet references to and from my PVArraySet  
objects.
If PVArraySet is using simple pointers and everywhere else is using  
Smart Pointers,
how do I convert between the two? The compiler doesn't like it if I  
just cast from one to the other.

- Matthew






More information about the Valentina mailing list