Linux app crashing when trying to access file in same directory as app

I have a cross platform application that looks for a Config.txt file in the same directory as the application in order to read settings and import them.

My code works great on Windows and macOS, but on Linux I’m “Class IO Exception not handled”: “Exception Error number 2.” This is true even if I move the location of where the app is looking for the Config.txt file, like the desktop. I have some code to check if the file is there or not, and that works correctly on Linux. It’s just when the file is actually there I get the error.

Dim f As FolderItem = GetFolderItem("")
if f <> nil then
  f = f.child("Config.txt")
end

if f.Exists=false then
  Messagebox ("No Config.txt file found!")
  OKQUIT = true
  quit
end


Dim t As TextInputStream
t = TextInputStream.Open(f)
dim s as String = t.ReadAll
Dim rg as New RegEx
rg.SearchPattern="####MAXTIME####(.*)####MAXTIME####"
rg.Options.Greedy = False
rg.Options.DotMatchAll = True
Dim myMatch as RegExMatch = rg.search(s)
if myMatch <> nil then
  dim configpremax as string
  configpremax = trim(myMatch.SubExpressionString( 1 ))
  MaxTime = val(configpremax)
  t.Close
else
  t.Close
end

I’ve also tried the newer folder constructer:

Dim f As new FolderItem("")
if f <> nil then
  f = f.child("Config.txt")
end

if f.Exists=false then
  Messagebox ("No Config.txt file found!")
  OKQUIT = true
  quit
end

as well as this:

Var f As New FolderItem("")
f= App.ExecutableFile.Parent.child("Config.txt")


if f.Exists=false then
  Messagebox ("No Config.txt file found!")
  OKQUIT = true
  quit
end

No luck, exact same thing happens on Linux only.

Any ideas? A bug perhaps?

Thanks!

You should not try and store files next to the executable, certainly on macOS you will not be able to modify that file. Look at

SpecialFolder.Preferences

Which should give you a location for each platform where setting files should be stored.

Var f As New FolderItem("")
f= App.ExecutableFile.Parent.child("Config.txt")

This code creates a FolderItem using the first line and then replaces it with another one on the second line. You would be better off with:

Var f As FolderItem
f= App.ExecutableFile.Parent.child("Config.txt")
1 Like

Actually, @Amy_Barnes just recently had an issue accessing a database in the same directory as her Linux web app. @Ricardo_Cruz was on the ticket as well. It’s not currently a “known issue” but two curious cases seems suspicious.

#72608 - Web app silent crash - possible Prepared Statement problems?

2 Likes

This is not an end-user application, but something that will pushed out with a script. Sort of a fancy pop-up alert screen. The file location is fine and doesn’t need to be modified after the fact. It’s all temporary.

You are correct about the syntax, but alas, did not change the problem.

Thanks. It does sound similar. I’ve added a link to this post on that thread.

1 Like

Can you please also include a sample project that we can use to quickly reproduce the issue? :pray:

1 Like

I put together a small example to submit, and realized that it actually worked on Linux!

That lead me to double-checking my project. I forget there were a couple of more spots where I was using that old, depreciated FolderItem constructor.

Dim f As FolderItem = GetFolderItem("")
if f <> nil then
  f = f.child("Config.txt")
end

Once I got those changed over to the new method, everything worked!

Important safety tip: If you’re using Linux, make sure to use the newer FolderItem constructor.

Dim f As new FolderItem
if f <> nil then
  f = f.child("Config.txt")
end
1 Like

The takeaway is, don’t use deprecated stuff.

What is the new way versus the depreciated way?

The new way is to use the constructor.

dim f as FolderItem = GetFolderItem("")

is replaced by

var f as FolderItem = New FolderItem("")
1 Like

Read the documentation.

Or as they says in the old times RTFM !

https://documentation.xojo.com/api/files/folderitem.html#folderitem
search Constructor…

Huh, okay, the “new” way is what I’ve been using for a while now.
So when I checked the documentation and saw the code I am already using, I was confused.

I already use the “new” way, and have been doing so. So someone’s comment regarding depreciated code posted without inclusion of what the original code used… was confusing to me, as I do not use the previous method of constructing a FolderItem.

I already use the proper code, and simply asked for context in the form of what code has been depreciated versus the newer way.