I'm working on a Delphi 4 application (Don't ask me why, I already know it's so old but I had to, it's more complex to explain this, just boss demands).
This Delphi application creates a lot of *.MB files on execution (All good for now), it uses many TQueries, TUpdatesSQL, TDataSource so I guess it's normal. But when I close this app, those files should be deleted (but no), then when I open this app for a second time (third, fourth, etc.) it creates more and more *.MB files, finally ending with an error: "Too many open files. You may need to increase MAXFILEHANDLE limit in IDAPI configuration"
I just found on internet that you have to increase the MaxFileHandles (I have value of 48) at:
BDE Administrator:
Configuration->System->INIT->MAXFILHANDLES
I this the only solution? or there is another one? Can anyone give me a light, or a tip for deleting those files when the app is closed?
A correctly written and correctly used D4 program should not leave behind .MB files in the way you describe.
I strongly urge you to follow the procedure I describe below so that you can satisfy yourself that stray .MB files should not be left behind in normal operation; hopefully, once you have, it should be fairly straightforward to track down why they're getting left behind in your case.
Create a new project folder, call it D:\Test on your development drive and copy the files of an existing Paradox database into it. I used the Venues database which in D7 (I do not have an earlier version available) is in Delphi's Shared\Data folder. It consists of the data file Venues.DB, the index Venues.PX and the memos file Venues.MB. If D4 doesn't come with the Venues database, use another one. By the way, the point of copying the database files to the new project folder is that the database is quite likely to get damaged in the course of what follows.
Using the BDE Configuration program, create a new Standard, Paradox alias and point it at the project folder.
Start D4 and create a new project which includes a TDataBase, TQuery, TDataSource and a TDBGrid to display the TQuery's database. Set the TQuery's SQL property to
select * from venues
Important: when you save the project, make sure the TDataBase's Connected property is set to False and the TQuery's Active property is also False. Insert code in the FormCreate event to open Query1.
Open a CMD window on your project directory.
Compile and run the program.
While the program is open do a
dir *.mb
at the CMD prompt.
You should see
Venues.MB
and a file with a name like
_QSQ1.MB
which is a temporary .MB file the BDE has opened for Query1.
You should now only see
Venues.MB
listed because the BDE code has closed and deleted the temporary .MB file. That is what is supposed to happen.
Run the program again. and this time, after its form opens, press Ctrl-F2 to reset it (force it to close without executing its normal shut-down code.
Repeat step 7 and you should find there are two .MB files
Venues.MB
and the temporary file with a name like
_QSQ1.MB
because the BDE code didn't have a chance to remove the temporary .MB file
If you keep repeating steps 9 and 10, you'll find that an extra .MB file gets left behind (with an increasing number on the end of its name), which is why the BDE eventually complains that you've run out of file handles.
I'm afraid your task is to work out why this is happening:
It may be simply that you are in the habit of doing a Ctrl-F2 to terminate the program, in which case the solution is obvious, don't!
Otherwise something must be going wrong as the program closes. It could be any one of a number of things, but only you can see your code. What I would be looking for is something like an exception handler that has been set up in the FormClose event to suppress an unwanted exception during shutdown. This sort of "fast fix" which was intended to hide a problem without finding and fixing the problem causing it was fairly common in the D4 era.
Anyway, good luck! With a bit of systematic debugging, hopefully you won't need it/