tags:

views:

464

answers:

1

I found some problems for std::list when I'm doing some iPhone development under Xcode. Here is the code:

///////////// interface //////////////

class CObj
{
public:
    int value;
};

typedef std::list<CObj*> ObjList;

@interface testAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    ObjList  m_objs;
}



//////////// implementation /////////

- (void)applicationDidFinishLaunching:(UIApplication *)application 
{    

    for (int i = 0; i< 10; i++ )
    {
     CObj* obj = new CObj;
     obj->value = i;
     m_objs.push_back(obj);
    }

    NSLog(@"%d objects",m_objs.size() );

    ObjList::iterator it = m_objs.begin();
    while (it != m_objs.end()) 
    {
     CObj* obj = *it;
     if ( obj->value == 3 )
      it = m_objs.erase(it);
     else
      it++;
    }

    NSLog(@"%d objects",m_objs.size() );
}

The application simply crashes at m_objs.push_back. But if I change the std::list to std::vector, everything's fine. Any idea? Thanks

A: 

By default Objective-C does not call the constructor for c++ types on initialization.

If you set this flag in the build settings "Call C++ Default Ctors/Dtors in Objective-C", it will change the default behavior. Be aware that the flag only shows up if you have the target set to Device.

CynicismRising