SlideShare une entreprise Scribd logo
1  sur  18
Exception Safety and Garbage Collection and Some Other Stuff A Moderately Directed Rant
Why should I care about this? It’s very likely that you’ve been writing totally incorrect code without realizing it Once you do realize it, it’s usually not too hard to fix the problem, depending on the language This is information that isn’t all that widely known, for whatever reason You can use it to show off at interviews! You can use it to start arguments about which programming language is the best!
Boring Definitions We need to define a couple basic things at the beginning so everybody’s on the same page It’ll be quick, I promise Garbage collection is a method of managing dynamic (heap-allocated) memory It’s non-deterministic, and there is usually no guarantee that memory is cleaned up at all Most modern languages use garbage collection
More Boring Definitions A resource is anything you need to return to the system once you’re done using it File handles, dynamic memory, locks, etc. Exception safety means that you can throw an exception in the middle of your function without bad things happening There’s a complicated formal definition with degrees of exception safety but this is good enough for our purposes
Reasonable Code Example (C) Let’s look at some C code so we can figure out what this talk is even about voidexample() { lock(&g_mutex); int* my_int_pointer = (int*)malloc(sizeof(int)); do_something_with(my_int_pointer);free(my_int_pointer); unlock(&g_mutex); } This is fairly reasonable, safe C code. It executes deterministically and everyone is happy
Terrible Code Example (C++) Let’s see that exact same code, but now we’ll pretend that it was compiled as C++ voidexample() { lock(&g_mutex); int* my_int_pointer = (int*)malloc(sizeof(int)); do_something_with(my_int_pointer);free(my_int_pointer); unlock(&g_mutex); } Catastrophe! This is going to compile and run without warnings, but be completely and totally unsafe! Why? Exceptions!
That Sucks! Yes, it does suck! It’s such a problem that people were motivated to go try to solve it BjarneStroustrup (C++ language creator) came up with a solution which he named Resource Acquisition Is Initialization (RAII) Incidentally, in addition to providing exception safety, RAII made C++ way easier to use Let’s look at a correct C++ version of our code example, using RAII
Modern Code Example (C++) voidexample() { Lockmy_lock(&g_mutex); auto_ptr<int> my_int_pointer(new int()); do_something_with(my_int_pointer); } Thanks to RAII this example is exception safe, and we don’t have to worry about cleanup. auto_ptr<T> is part of the C++ standard library, but we’ve just made up Lock Let’s look at the code for our made-up Lock class so we can see how RAII actually works
How RAII Actually Works classLock { private: mutex* m_pMutex; public: Lock(mutex* pMutex) : m_pMutex(pMutex) {  lock(m_pMutex); 	} 	~Lock() { unlock(m_pMutex); 	} }; In C++ a stack-allocated object’s destructor is always called once it goes out of scope, whether due to a function returning, due to normal code execution, or due to stack unwinding caused by a thrown exception
Sure, but I don’t use C++ That’s understandable. We are (for better or worse) a Java school, so let’s see if we can’t make RAII work in Java Immediately we run into some problems Java doesn’t have destructors Java doesn’t have stack allocation for objects So RAII won’t work with Java, then. What else have we got? For dynamic memory we have garbage collection, but that’s a special case of the problem that doesn’t really need (or provide) determinism The best we can do is the Dispose pattern
The Dispose Pattern (Java) voidexample() { IntegermyInteger = newInteger(0); Locklock = newLock(g_mutex); try { doSomethingWith(myInteger); 	} finally { lock.dispose(); 	} } While rewriting this every time gives you exception safety, it’s really easy to forget it If you forget to do this, your program will still compile and run with no warnings, despite being wrong. Awesome! This is more verbose than even the C example, yet is the minimum amount of code required for Java
More Dispose Loveliness (Java) voidexample() { FilemyFile = newFile(filename); try { DBConnectiondbConn = newDBConnection(credentials); try { LockmyLock = newLock(g_mutex); try { doSomething(myFile, dbConn); 		    } finally { myLock.dispose(); 		    } 		} finally { dbConn.dispose(); 		} 	} finally { myFile.dispose(); 	} } This is again the minimum code required to be correct
Again, but with RAII (C++) voidexample() { FilemyFile(filename); DBConnectiondbConn(credentials); LockmyLock(&g_mutex); doSomething(myFile, dbConn); }
One More Time (D) voidexample() { scopemyFile = newFile(filename); scopedbConn= newDBConnection(credentials); scopemyLock = newLock(g_mutex); doSomething(myFile, dbConn); }
“You can take my deterministic resource management when my cold dead hand goes out of scope.” -- Anon
Why does Java suck so bad? By choice. You can have deterministic resource management alongside garbage collection, but the Java guys specifically chose not to The D programming language supports RAII and has a garbage collector, so it’s definitely possible Java, C#, Python, Ruby all screw this up to varying degrees The latter three have some syntactic sugar for resource management, but the onus is still on you to remember to use it Java 7 catches up with C# and adds the same syntactic sugar, but still doesn’t solve the problem Perl, PHP, C++ and D all get it right to varying degrees If PHP gets something right before your language does, you should reassess your life goals
So what should I do? My (unpopular) answer? Use C++ and Perl/PHP for everything until the D ecosystem matures a bit, then switch over to D entirely C++ has its own set of problems, but it’s my opinion that they’re exaggerated and the benefits far outweigh them If you’re stuck using a broken language like Java, I really don’t know what to tell you I guess you could cry a little bit, but I don’t think it would solve the problem Learn the Dispose pattern, always remember to use it
Okay, done ranting I wish I had more helpful advice for Java, C#, Python, Ruby users, but this is the unfortunate state we find ourselves in If you want more information about anything mentioned: The D Programming Language by Andrei Alexandrescu is an excellent D introduction If you want to learn how to code modern C++, you should read Effective C++ by Scott Meyers The Boost website has good information about exception safety and reference-counted smart pointers, which I didn’t really talk about (Scott Meyers does in Effective C++) Google knows all Questions?

Contenu connexe

Dernier

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 

Dernier (20)

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

En vedette

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 

En vedette (20)

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 

Exception Safety and Garbage Collection and Some Other Stuff

  • 1. Exception Safety and Garbage Collection and Some Other Stuff A Moderately Directed Rant
  • 2. Why should I care about this? It’s very likely that you’ve been writing totally incorrect code without realizing it Once you do realize it, it’s usually not too hard to fix the problem, depending on the language This is information that isn’t all that widely known, for whatever reason You can use it to show off at interviews! You can use it to start arguments about which programming language is the best!
  • 3. Boring Definitions We need to define a couple basic things at the beginning so everybody’s on the same page It’ll be quick, I promise Garbage collection is a method of managing dynamic (heap-allocated) memory It’s non-deterministic, and there is usually no guarantee that memory is cleaned up at all Most modern languages use garbage collection
  • 4. More Boring Definitions A resource is anything you need to return to the system once you’re done using it File handles, dynamic memory, locks, etc. Exception safety means that you can throw an exception in the middle of your function without bad things happening There’s a complicated formal definition with degrees of exception safety but this is good enough for our purposes
  • 5. Reasonable Code Example (C) Let’s look at some C code so we can figure out what this talk is even about voidexample() { lock(&g_mutex); int* my_int_pointer = (int*)malloc(sizeof(int)); do_something_with(my_int_pointer);free(my_int_pointer); unlock(&g_mutex); } This is fairly reasonable, safe C code. It executes deterministically and everyone is happy
  • 6. Terrible Code Example (C++) Let’s see that exact same code, but now we’ll pretend that it was compiled as C++ voidexample() { lock(&g_mutex); int* my_int_pointer = (int*)malloc(sizeof(int)); do_something_with(my_int_pointer);free(my_int_pointer); unlock(&g_mutex); } Catastrophe! This is going to compile and run without warnings, but be completely and totally unsafe! Why? Exceptions!
  • 7. That Sucks! Yes, it does suck! It’s such a problem that people were motivated to go try to solve it BjarneStroustrup (C++ language creator) came up with a solution which he named Resource Acquisition Is Initialization (RAII) Incidentally, in addition to providing exception safety, RAII made C++ way easier to use Let’s look at a correct C++ version of our code example, using RAII
  • 8. Modern Code Example (C++) voidexample() { Lockmy_lock(&g_mutex); auto_ptr<int> my_int_pointer(new int()); do_something_with(my_int_pointer); } Thanks to RAII this example is exception safe, and we don’t have to worry about cleanup. auto_ptr<T> is part of the C++ standard library, but we’ve just made up Lock Let’s look at the code for our made-up Lock class so we can see how RAII actually works
  • 9. How RAII Actually Works classLock { private: mutex* m_pMutex; public: Lock(mutex* pMutex) : m_pMutex(pMutex) { lock(m_pMutex); } ~Lock() { unlock(m_pMutex); } }; In C++ a stack-allocated object’s destructor is always called once it goes out of scope, whether due to a function returning, due to normal code execution, or due to stack unwinding caused by a thrown exception
  • 10. Sure, but I don’t use C++ That’s understandable. We are (for better or worse) a Java school, so let’s see if we can’t make RAII work in Java Immediately we run into some problems Java doesn’t have destructors Java doesn’t have stack allocation for objects So RAII won’t work with Java, then. What else have we got? For dynamic memory we have garbage collection, but that’s a special case of the problem that doesn’t really need (or provide) determinism The best we can do is the Dispose pattern
  • 11. The Dispose Pattern (Java) voidexample() { IntegermyInteger = newInteger(0); Locklock = newLock(g_mutex); try { doSomethingWith(myInteger); } finally { lock.dispose(); } } While rewriting this every time gives you exception safety, it’s really easy to forget it If you forget to do this, your program will still compile and run with no warnings, despite being wrong. Awesome! This is more verbose than even the C example, yet is the minimum amount of code required for Java
  • 12. More Dispose Loveliness (Java) voidexample() { FilemyFile = newFile(filename); try { DBConnectiondbConn = newDBConnection(credentials); try { LockmyLock = newLock(g_mutex); try { doSomething(myFile, dbConn); } finally { myLock.dispose(); } } finally { dbConn.dispose(); } } finally { myFile.dispose(); } } This is again the minimum code required to be correct
  • 13. Again, but with RAII (C++) voidexample() { FilemyFile(filename); DBConnectiondbConn(credentials); LockmyLock(&g_mutex); doSomething(myFile, dbConn); }
  • 14. One More Time (D) voidexample() { scopemyFile = newFile(filename); scopedbConn= newDBConnection(credentials); scopemyLock = newLock(g_mutex); doSomething(myFile, dbConn); }
  • 15. “You can take my deterministic resource management when my cold dead hand goes out of scope.” -- Anon
  • 16. Why does Java suck so bad? By choice. You can have deterministic resource management alongside garbage collection, but the Java guys specifically chose not to The D programming language supports RAII and has a garbage collector, so it’s definitely possible Java, C#, Python, Ruby all screw this up to varying degrees The latter three have some syntactic sugar for resource management, but the onus is still on you to remember to use it Java 7 catches up with C# and adds the same syntactic sugar, but still doesn’t solve the problem Perl, PHP, C++ and D all get it right to varying degrees If PHP gets something right before your language does, you should reassess your life goals
  • 17. So what should I do? My (unpopular) answer? Use C++ and Perl/PHP for everything until the D ecosystem matures a bit, then switch over to D entirely C++ has its own set of problems, but it’s my opinion that they’re exaggerated and the benefits far outweigh them If you’re stuck using a broken language like Java, I really don’t know what to tell you I guess you could cry a little bit, but I don’t think it would solve the problem Learn the Dispose pattern, always remember to use it
  • 18. Okay, done ranting I wish I had more helpful advice for Java, C#, Python, Ruby users, but this is the unfortunate state we find ourselves in If you want more information about anything mentioned: The D Programming Language by Andrei Alexandrescu is an excellent D introduction If you want to learn how to code modern C++, you should read Effective C++ by Scott Meyers The Boost website has good information about exception safety and reference-counted smart pointers, which I didn’t really talk about (Scott Meyers does in Effective C++) Google knows all Questions?