Problem with TFile("RECREATE") on WinXP

Dear ROOTers

Finally, I could port my program (package for “R”) to WinXP, compiled with VC++.

Now I have a strange problem:
When I want to create a temporary TFile, I use always the name "tmp_somename.root"
For example, in one of my methods I have the following code fragment:

      file = new TFile("tmp_exprtrees.root", "RECREATE");
      if (!file || file->IsZombie()) {
         cerr << "Error: Could not create temporary file <tmp_exprtrees.root>."
              << endl;
         SafeDelete(file);
         return errCreateFile;
      }//if

To test if an R-package works, I need to do: “R CMD check mypackage”, which checks
if e.g. the example code can be executed. Although this works w/o any warnings on
Mac and Linux, when I test this on WinXP I get the following error:

> Error in <TFile::TFile>: file tmp_exprtrees.root already exists
Error: Could not create temporary file <tmp_exprtrees.root>.

This means that the above code-fragment does not work on WinXP.

Is it possible that under certain circumstances TFile(“RECREATE”) does not work on WinXP?

Best regards
Christian

Hi,

On Windows you can not delete a file which is currently open by any processes. So it is possible you have not yet called the delete operator on an other TFile pointing to the same file. Also, the lock release is not immediate and thus some time must pass between the delete and the attempt to remove the file (sometimes several seconds).

Cheers,
Philippe.

Dear Philippe

Thank you for this explanation. However, this is bad news.

At least I could solve the problem for the temporary file mentioned above by doing:

   file = (TFile*)gROOT->FindObject("tmp_exprtrees.root");
   if (file) {file->Close(); delete file; file = 0;}

Best regards
Christian

Hi Christian,

If this does work, then you should be able to also solve the problem by deleting the temporary file as soon as you are done with it.

The one risk of your code is that it might that some other part of your code is still holding a pointer to this file (and assuming that it is open) which would lead to a seg fault the next time this other part of the code is activated.

Cheers,
Philippe

Dear Philippe

Thank you, I know of the risk.
The example works since this is an internal temporary file only, however, for some reason
it does not work with the main file, where the user will store the data trees.
Currently, I am trying to solve the problem by adding TDatime to the file name, however
this contradicts my intention to give the user the possibility to create a temporary file for
testing purposes only.

Best regards
Christian

Hi Christian,

As usual :slight_smile: if you provide a short runnable example, I’ll take a closer look.

Cheers,
Philippe.

Dear Philippe

Thank you for your kind offer, however this time I cannot supply a simple example:
The problem appears only when I run my library as R-package within the R environment. I assume that during the current R-session, R does not release the created file, thus I have to live with this limitation on WinXP.

Best regards
Christian