Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. deterministic cleanup in C++/CLI

deterministic cleanup in C++/CLI

Scheduled Pinned Locked Moved Managed C++/CLI
csharpc++question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rockonedge
    wrote on last edited by
    #1

    I tried the following code StreamWriter^ f = gcnew StreamWriter(L"d:\\listfile.txt"); f->Write(L"begin"); expecting a file containing the string "begin" to be created. To my surprise, only an empty listfile.txt was created. "begin" was not written. after a few trials, I realized I must explicitly call f->Close() to get "begin" to be written. is it true that one of the advantages of C++/CLI over c# is that when an object goes out of scope, there is something called deterministic cleanup to close the object, or am I miseducated? For std::fstream, I surely don't have to call close() explicitly.

    G 1 Reply Last reply
    0
    • R rockonedge

      I tried the following code StreamWriter^ f = gcnew StreamWriter(L"d:\\listfile.txt"); f->Write(L"begin"); expecting a file containing the string "begin" to be created. To my surprise, only an empty listfile.txt was created. "begin" was not written. after a few trials, I realized I must explicitly call f->Close() to get "begin" to be written. is it true that one of the advantages of C++/CLI over c# is that when an object goes out of scope, there is something called deterministic cleanup to close the object, or am I miseducated? For std::fstream, I surely don't have to call close() explicitly.

      G Offline
      G Offline
      Geo Pa
      wrote on last edited by
      #2

      You can use implicitly dereferenced variables. These variables use the Resource Acquistion Is Initialization (RAII) principle to do their clean up. Also, these variables have the same limiitations as automatic variables:

      StreamWriter f(L"D:\\listfile.txt");
      f.Write(L"begin");
      ...
      // When "f" goes out of scope, it will be deterministically disposed of.
      

      In order to obtain a tracking handle to an implicitly dereferenced variable, you must prefix it with "%".

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups