15. C++
• Memory management - what C
developer thinks?
• If I know C I already know C++!
static bool check_memory()
{
! Something * a = new Something;
! if( !a )
! ! return false;
! a->do_something();
! if( a->some_condition() )
! {
! ! delete a;
! ! return true;
! }
! // Lots of code
! delete a;
! return false;
}
16. C++
• Memory management - what C
developer thinks?
• If I know C I already know C++!
static bool check_memory()
{
! Something * a = (Something *)malloc( sizeof(Something) );
! if( !a )
! ! return false;
! do_something(a);
! if( some_condition(a) )
! {
! ! free(a);
! ! return true;
! }
! // Lots of code
! free(a);
! return false;
}
17. C++
• Are there exceptions in C++?
• Are they good or evil? Good!
• If I do not use them?
• You are allowed not to throw
• But good C++ design always uses
exceptions, so you better be prepared
18. C++
• Memory management example
static bool check_memory()
{
! std::auto_ptr<Something> a( new Something );
! a->do_something();
! if( a->some_condition() )
! ! return true;
! // Lots of code
! return false;
}
19. C++
• Resource management example
static bool check_file(const std::string & name)
{
! liba::FileInputStream fs(name);
! int first = fs.read();
! if( first == 32 )
! ! return true;
! // Lots of code
! return false;
}
20. C++
• Resource management example
static bool check_file(const std::string & name)
{
! std::auto_ptr<liba::FileInputStream> fs( liba::create_fis(name) );
! int first = fs->read();
! if( first == 32 )
! ! return true;
! // Lots of code
! return false;
}
21. C++
• Delete is for implementing libraries!
{
std::auto_ptr<Something> a( new Something(...) );
if( ... )
return;
}
• No difference between memory and
other resource types
22. Objective-C
• Memory management - what C
developer thinks?
• If I know C I already know Objective-C!
-(bool)check_memory
{
! Something * a = [[Something alloc] init];
! if( !a )
! ! return false;
! [a do_something];
! if( [a some_condition] )
! {
! ! [a release];
! ! return true;
! }
! // Lots of code
! [a release];
! return false;
}
23. Objective-C
• Memory management - what C
developer thinks?
• If I know C I already know Objective-C!
static bool check_memory()
{
! Something * a = (Something *)malloc( sizeof(Something) );
! if( !a )
! ! return false;
! do_something(a);
! if( some_condition(a) )
! {
! ! free(a);
! ! return true;
! }
! // Lots of code
! free(a);
! return false;
}
24. Objective-C
• Are there exceptions in Objective-C?
• Are they good or evil? Absolutely good!
-(void)parse_object:(NSDictionary *)dic {
! // Lots of code
! int val = [[[dic objectForKey:@"users"] objectAtIndex:0] intValue];
! // Lots of code
}
-(void)parse_object:(NSDictionary *)dic {
! // Lots of code
! NSArray * arr = [dic objectForKey:@"users"];
! if( ![arr isKindOfClass:[NSArray class]] )
! ! return; // But we must tell about error...
! NSNumber * num = [arr objectAtIndex:0];
! if( ![num isKindOfClass:[NSNumber class]] )
! ! return; // But we must tell about error...
! int val = [num intValue];
! // Lots of code
}
42. Retain cycles
• Can compiler find them?
• Can runtime find them?
• Can tools find them?
• Is there a simple approach to avoid
them?
43. Retain cycles
• Can compiler find them?
Yes, soon (experimental languages).
• Can runtime find them?
• Can tools find them?
• Is there a simple approach to avoid
them?
44. Retain cycles
• Can compiler find them?
Yes, soon (experimental languages).
• Can runtime find them?
Yes, Garbage Collector.
• Can tools find them?
• Is there a simple approach to avoid
them?
45. Retain cycles
• Can compiler find them?
Yes, soon (experimental languages).
• Can runtime find them?
Yes, Garbage Collector.
• Can tools find them?
Yes, if included in test cases.
• Is there a simple approach to avoid
them?
46. Retain cycles
• Can compiler find them?
Yes, soon (experimental languages).
• Can runtime find them?
Yes, Garbage Collector.
• Can tools find them?
Yes, if included in test cases.
• Is there a simple approach to avoid
them?
Yes, analyze architecture.
54. Objective-C++
• Integration is almost flawless!
• Apple teams actually use C++ and STL
@interface Something : NSObject {
! std::vector< id<UIActionSheetDelegate> > a;
}
@end
-(void)do_something {
! std::for_each(a.begin(), a.end(), ^(id<UIActionSheetDelegate> x) {
! ! [x actionSheetCancel:nil];
! });
}
55. Success
• alloc-init-autorelease idiom
• NSPtr or @property(retain) to manage
retain/release
• Use text editor to eliminate all retain,
release
• Architecture with no cycles
• Typed containers (STL), so the compiler
can actually prove most of your
architecture
56. What we forgot?
• Active objects (timers, http requests)
• Parallel execution (NSOperation)
57. What we forgot?
• Active objects (timers, http requests)
• Parallel execution (NSOperation)
Do not worry!
This is also easy!