Skip to content

This is to demonstrates how shared_ptr can be smart enough to use as RAII in different scenarios other than just memory management. In this demo, we use it for closing file descriptor in normal and exception conditions.

Notifications You must be signed in to change notification settings

sheikhazad/Smart-RAII

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Smart-RAII

This is to demonstrates how shared_ptr can be smart enough to use as RAII in different scenarios other than just memory management.
In this demo, we use it for closing file descriptor in normal and exception conditions.

  1. RAII for file, file will be closed in normal flow and exception.
  2. RAII enables us to avoid exception handling just to close file.
  3. shared_ptr will never call it's own deleter but will be replaced by local lambda function provided.
  4. So, File object is never deleted by shared_ptr but closed only.
  5. File object will be deleted only when it goes out of scope but not by smart_ptr - shared_ptr.
  6. It also shows how try-catch can be used in constructor to inform client that object creation has failed

About

This is to demonstrates how shared_ptr can be smart enough to use as RAII in different scenarios other than just memory management. In this demo, we use it for closing file descriptor in normal and exception conditions.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages