Skip to content

Instantly share code, notes, and snippets.

@stoyannk
Last active December 16, 2015 07:09
Show Gist options
  • Save stoyannk/5397115 to your computer and use it in GitHub Desktop.
Save stoyannk/5397115 to your computer and use it in GitHub Desktop.
Leaking ARC references in Objective-C++ (clang Apple clang version 4.1 / (tags/Apple/clang-421.11.66))
#include <iostream>
#import <Foundation/Foundation.h>
@interface TestObj : NSObject
@end
@implementation TestObj
-(id)init {
self = [super init];
NSLog(@"init!");
return self;
}
-(void)dealloc {
NSLog(@"dealloc!");
}
@end
class Parent
{
public:
/*virtual */ void aFunc() {} // Uncomment 'virtual' for reference leak!
};
class TestClass : public Parent
{
public:
TestClass() : m_Obj([[TestObj alloc] init])
{}
private:
TestObj* m_Obj;
};
int main(int argc, const char * argv[])
{
TestClass* cl = new TestClass;
cl->~TestClass();
operator delete(cl);
//delete cl; // Uncomment me and comment the manual mem. management to never leak!
std::cout << "End!" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment