beeoproblem,
@beeoproblem@mastodon.gamedev.place avatar

A post about C++ got me thinking about how I've not really seen anything quite as clean (in theory at least) as RAII.

In short:
create an object to get a resource (e.g. open a file)
delete it to release (e.g. close the file)

Multiple languages have a "using" or "with" block that works similarly but they usually require some extra work to use and can be skipped. If you design your class right RAII requires no special code in the client.

Resources automatically release as they fall out of scope

beeoproblem,
@beeoproblem@mastodon.gamedev.place avatar

E.g.
{
FileReader theFile = FileReader("thing.txt"); // this opens the file

// do stuff
}

// The file is closed now

When theFile is initialized it opens thing.txt. When theFile falls out of scope, for any reason, its destructor is called which then closes the file.

beeoproblem,
@beeoproblem@mastodon.gamedev.place avatar

C#, on the client side, is also similarly clean.

using(ForgotTheName theFile = new ForgotTheName("thing.txt")) {
// Do stuff
}

However you don't get any protection against leaving the file open if you don't actually write "using" while RAII doesn't require such effort.

I get why C++ style RAII wouldn't work (you don't control object lifetimes in managed code) but I kinda miss it.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • ngwrru68w68
  • rosin
  • GTA5RPClips
  • osvaldo12
  • love
  • Youngstown
  • slotface
  • khanakhh
  • everett
  • kavyap
  • mdbf
  • DreamBathrooms
  • thenastyranch
  • magazineikmin
  • megavids
  • InstantRegret
  • normalnudes
  • tacticalgear
  • cubers
  • ethstaker
  • modclub
  • cisconetworking
  • Durango
  • anitta
  • Leos
  • tester
  • provamag3
  • JUstTest
  • All magazines