deleteFile strange behavior

I’m putting this in the Audio Plugins category, since it seems to be a plugin-related behavior.

Forgive me if I’m doing something stupid here, but it seems that if I create a file (in this example, for global settings), only the instance of the plugin that created the file can delete it. Example behaviors:

  1. On first instance, the plugin checks to see if a global settings file exists, and if it doesn’t, it creates one. In hosts that run a validation (like Bitwig), they effectively instance the plugin completely, so this settings file is created. So future instances can no longer delete or alter it, only append it.

  2. If I make it so the settings load from default if there’s no global settings file, and later write a settings file, any instance but the one that created it can not overwrite it.

    File tmp = globalSettingsDirectory.getChildFile(“quanta.settings”);
    if (tmp.existsAsFile())
    bool result = tmp.deleteFile();
    if (result) DBG(“Deleted”);

In that example, if existsAsAFile returns true, result returns false. I want the prefs to be per user, obviously, so I’m using the userApplicationDataDirectory special location, but I’ve tried it with pretty much everything including root, and it just won’t delete. While I’m not that interested in deleting the file, I do need to use replaceWithText(), which has a delete() in it.

Note this is Win10; I haven’t tried it on my macOS machine yet.

EDIT: Tried it on my Mac, and it works fine; replaceWithText() and delete() work as expected. So this is a Windows 10 issue, it would seem.

I can’t explain the behaviour, but a workaround without deleting the file might be:

FileOutputStream output (file);
output.setPosition (0);
output.truncate ();
output.writeText (text, false, false, nullptr);


EDIT: use writeText instead writeString according to docs…

1 Like

Ah, that did it. I had tried something along similar lines early on, but apparently didn’t try it hard enough.

Anyhow, the JUCEateers can consider this an official bug report. delete(), replaceWithText(), and related methods are apparently only accessible by the instance that created the file in Windows 10 (latest public version).

Thanks for the report, I am sure, the JUCE team will pick that up in the coming days!

Per your edit above, I was initially using replaceWithText() but with your sample code, JSON::writeToStream() worked more better, so I switched to that, since our globals are JSON anyhoo.

That makes sense. To be honest, I was unsure which one to choose, the docs say writeString behaves different from what you expect, that’s why I switched it to writeText.
But this way you don’t have to decide which one… much better :wink:

I’ll take a look.

I’ve created a basic audio plugin project in the Projucer and added the following lines to the AudioProcessor constructor:

auto testFile = File::getSpecialLocation (File::SpecialLocationType::userApplicationDataDirectory)
                     .getChildFile ("TestFile.settings");

if (! testFile.existsAsFile())
    jassert (testFile.deleteFile());

I’ve tried loading multiple instances of the VST in both the JUCE AudioPluginHost and Waveform 9 on Windows 10 and I can’t reproduce the deleteFile() failing. Can you put together a minimal code example that reproduces the problem, or try adding the lines above to a basic audio plugin template and stepping through in the debugger to see if it succeeds?