Search code examples
objective-cinteropobjective-c++

How to delete a c++ pointer in objective c with ARC


I'm wrapping the box2d library and I want to properly dealloc the b2World pointer when the wrapper deallocates. My question is, should I override the -dealloc method in objective c, or does ARC automatically deallocate the c++ *world pointer?

#import "World.h"
#import "Box2D.h"
@interface World()
@property b2World* world;

@end

@implementation World 
@synthesize world;

-(void) createWorld:(Vec2*) gravity{
    b2Vec2 g(gravity.x, gravity.y);
    world = new b2World(g);
}
-(void) dealloc{
    delete world;
    world = nil;
    NSString *temp = @"World DEALLOCATED!!";
    NSLog(@"%@", temp);
}

Did I properly implement -(void) dealloc or is it unnecessary to do so?


Solution

  • ARC does not automatically count references on c++ pointers. You did properly implement dealloc, though you may want to check to make sure world isn't 0 before calling delete on it.