• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Using C++ AutoPtr within an Obj C file.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using C++ AutoPtr within an Obj C file.


  • Subject: Re: Using C++ AutoPtr within an Obj C file.
  • From: Tomasz Kukielka <email@hidden>
  • Date: Thu, 04 Aug 2005 23:59:09 -0700
  • Thread-topic: Using C++ AutoPtr within an Obj C file.

On 8/4/05 10:51 PM, "email@hidden"
<email@hidden> wrote:

>  My "controller"
> class will be making C++ calls,  mostly allocating pointers.  I already
> read the Apple
> document describing the limitations of integrating C++ calls from within
> an Obj C file (my controller).
>  [...]
> For instance,  the statement:
>
>   auto_ptr<ClientAuthManager> clientAuth(new ClientAuthManager);
>
> Creates a pointer to a ClientAuthManager object and places it into
> 'clientAuth' pointer.

Auto pointers or smart pointers in C++ are designed to provide
exception-safe mechanism for releasing memory allocations or other resources
or to restore a specific state. But the problem you are going to face is
that C++ exceptions cannot be caught in Objective C code. They can be caught
and handled in local function scope but they cannot be caught in parent
function. Consider the following:


- (void)testCPPExceptions
{
    try
    {
        throw -1L;
    }
    catch(...)
    {
        NSLog(@"C++ exception was caught in local function scope");
        throw;
    }
}

- (void)parentFunction
{
    try
    {
        [self testCPPExceptions];
    }
    catch(...)
    {
        NSLog(@"C++ exception was caught in parent");
    }
}

Suppose now that you call parentFuntion. In TestCPPExceptions we throw a C++
exception which will be caught. But the malicious programmer (read: me)
wrote a code to re-throw the exception out of the scope of the function. The
result is that your app is killed because your exception never arrives to
the catch(...) handler in parentFunction. The moral: every time you use a
C++ code in your ObjC method you need to provide a try-catch block wrapping
the C++ code. It is pretty much like you would implement some library in C++
internally and you would like to expose it as a plain C API - no exception
can propagate outside.
You may say that your C++ classes do not use exceptions. That may be right
but the default behavior of operator "new" is to throw an exception if it
cannot allocate a memory block - how would you deal with that?
Another thing is that you cannot put your C++ objects with custom
constructors, including auto_ptr, as a member of your ObjC class (a common
practice in C++ though). The compiler even displays a warning if you try the
following:

class TestCPPObject
{
public:
    TestCPPObject()
    {
        NSLog(@"C++ constructor invoked");
    }

};

@interface MyDocument : NSDocument
{
    TestCPPObject mCPPObject;
}

You will never see "C++ constructor invoked" printed in this case.
What you would have to do is to put a pointer to the object like:
TestCPPObject *mCPPObject;
And then, in "init" code allocate it via operator new:

-(id)init
{
    self = [super init];
    if(self != nil)
    {
        try
        {
            mCPPObject = new TestCPPObject;
            return self;
        }
        catch(...)
        {
             NSLog(@"Could not init self");
        }
    }
    return nil;
}


Hope it helps,
Tom


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Prev by Date: Path of Files.h
  • Next by Date: Re: Path of Files.h
  • Previous by thread: Re: Using C++ AutoPtr within an Obj C file.
  • Next by thread: Re: Using C++ AutoPtr within an Obj C file.
  • Index(es):
    • Date
    • Thread