File closed unexpected during read loop when #PB_Any is used

Just starting out? Need help? Post your questions and find answers here.
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: File closed unexpected during read loop when #PB_Any is used

Post by DarkDragon »

What other commands or APIs are you using? Maybe try hooking the windows CloseHandle command in your own application somehow to check when it gets closed. With the C backend you might be able to attach with lldb or gdb (maybe even without C backend, but then you don't have debug symbols).
bye,
Daniel
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: File closed unexpected during read loop when #PB_Any is used

Post by infratec »

API is only used for detecting network changes.
And this is only in a listening way.

The program is crossplatform and I avoided API when ever it is possible.

All of this can not explain why it is working without problems when I use fixed numbers.

With #PB_Any it happens latest after 20 program starts.
With fixed number I was not able to reproduce it.
User avatar
skywalk
Addict
Addict
Posts: 4218
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: File closed unexpected during read loop when #PB_Any is used

Post by skywalk »

I am curious if you set the constant to the same value returned by PB_ANY?
Does this happen regardless of file sizes?
If it happens after 20 tries of the program, did the original programs actually close out completely?

Rename your app(app1.exe, app2.exe) each time you start.

Is this a memory leak?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Demivec
Addict
Addict
Posts: 4267
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: File closed unexpected during read loop when #PB_Any is used

Post by Demivec »

Can you provide a program skeleton for the program that causes the issue. I know you provided a written outline of some of the details but this is hard to test.

The important parts of the skeleton would be all file operations for open and closing and reading (since you said there are no write operations), thread creation and operations for the same. Not needed would be any data read from a file unless it causes a file action such as closing or opening a file. Anything GUI related would also not be needed.

I haven't experienced the issue but would like to see if I can produce it and then find solutions.
User avatar
idle
Always Here
Always Here
Posts: 5897
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: File closed unexpected during read loop when #PB_Any is used

Post by idle »

I assume your using readfile with #PB_File_SharedRead and that your file handle is an integer rather than a long
in case it's getting clobbered on the stack. You could also try it without buffering.
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: File closed unexpected during read loop when #PB_Any is used

Post by BarryG »

@infratec: You never said which version of PureBasic you're using. We know that 6.10 brought some crashing issues due to long-vs-integer data types, so I'd at least try a pre-6.10 version and see if the problem still occurs with that.
Last edited by BarryG on Sat Jun 22, 2024 4:41 am, edited 1 time in total.
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

Re: File closed unexpected during read loop when #PB_Any is used

Post by AZJIO »

BarryG wrote: Sat Jun 22, 2024 2:03 am @infratec: You never said which version of PureBasic you're using.
https://www.purebasic.fr/english/viewto ... 28#p623028
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: File closed unexpected during read loop when #PB_Any is used

Post by BarryG »

Dang, I only paid attention to his first post.
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: File closed unexpected during read loop when #PB_Any is used

Post by DarkDragon »

infratec wrote: Fri Jun 21, 2024 8:49 pm API is only used for detecting network changes.
And this is only in a listening way.

The program is crossplatform and I avoided API when ever it is possible.
Yes, but when it's being closed the API will likely be called. And then you could check where the call happened. You can also do it on Linux with fclose/close or so.

Ollydbg might be helpful as well.
bye,
Daniel
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: File closed unexpected during read loop when #PB_Any is used

Post by infratec »

The program is always completely closed before I start it again.

As you can see in the screenshot I'm running it from the IDE with Purifier and a granularity of 1, 1, 1, 1.

For all file operations I used #PB_Any, so it is impossible that the same value is used.
As I can say, at this point no other file operations are active.
File operations are only for reading/writing the INI file and for this CSV file.
Since it is at the program start, the ini file was already read, and the user (I) was not able to change something which
requires a write of the INI file.

There is no action of defender active, and I don't use an other virus scanner.

As I told you, I was not able to write a multithreaded small program which can reproduce this behaviour.
I can not give away the sourcecode. It is a phone program for a customer with SIP and a WEB GUI communicating via WebSocket.
At this early moment I fetch the contacts from the PBX via HTTPRequests and write them in the file. Then I reopen it and read it line by line.
And sometimes it happens, but only with #PB_Any. I use the standard ReadFile() without optional parameters.

It is really difficult.

On monday I will try to find out from where the file close is comming.
PBJim
Enthusiast
Enthusiast
Posts: 296
Joined: Fri Jan 19, 2024 11:56 pm

Re: File closed unexpected during read loop when #PB_Any is used

Post by PBJim »

I had a particular interest in this, as I also maintain a thread-dependent application that manages random access data files inside those threads. I recently asked on the forum about the maximum concurrent open files. In the end, I carried out the testing myself and I found that I could open 30,000 files with #PB_Any and keep them open, furthermore that I could write to them all and close them. In my testing, I didn't lose any of the files.

The only thing I can foresee with your case, is that it's caused by a timing problem that hasn't been considered, for instance a thread still running when you don't realise it. I think you say that you close the file in the threads only, but it wasn't clear to me from your code screenshot, if that section is the thread.
I don't really follow this though :
infratec wrote: Fri Jun 21, 2024 7:10 am I one thread I use CreateFile with #PB_Any, write something, close it and open it again with ReadFile and #PB_Any.
I return the File as ProcedureReturn.
You're creating the file in the thread, but I don't follow whether you're passing the file no. back from the same thread in which you create it. You say you return the file as a ProcedureReturn, but threads can't pass back a ProcedureReturn.

I have just converted my recent test code to use threads, to see if any #PB_Any-opened file gets closed, but it works fine. It probably isn't close to what you're doing, but just thought I'd try to reproduce. Sometimes in the commercial world, we can't afford to spend time and cost trying to resolve a weird problem like this, so we have to use the workaround.
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: File closed unexpected during read loop when #PB_Any is used

Post by BarryG »

infratec said it works if #PB_Any isn't used, so the crux of the issue is using constants vs using #PB_Any.
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: File closed unexpected during read loop when #PB_Any is used

Post by infratec »

The whole thing is inside one thread.

It calls Mein_gntel_GetContactCDV() this returns the file handle.
And then it is read line by line and afterwards it should be closed by the code.
Post Reply