Last active
December 16, 2015 07:09
-
-
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))
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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