1 registered members (AbrahamR),
717
guests, and 4
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: More than One "File_Open_Read" - Problem?
[Re: Dooley]
#466790
07/03/17 02:03
07/03/17 02:03
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
Dooley, I think you are chasing the wrong issue here. A memory leak doesn't lead to memory corruption. Leaking memory isn't good and should be fixed, but it's not the cause of your garbage data. That usually comes from writing into corrupt pointers and overriding existing data.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: More than One "File_Open_Read" - Problem?
[Re: Dooley]
#466934
07/09/17 22:03
07/09/17 22:03
|
Joined: May 2005
Posts: 868 Chicago, IL
Dooley
OP
User
|
OP
User
Joined: May 2005
Posts: 868
Chicago, IL
|
Okay I found a potential cause of corruption.
For some reason, when I wanted to check if a file existed, I was using
if(file_open_read(filename))
instead of
if(file_exists(filename))
In most cases this actually worked okay, since the function was looking for an existing file. However, in one case, it would only work when the file did not exist.
In this case, if the file did exist, I guess that file would actually remain open for reading, and it never got closed. I am about to test the game with this corrected ... wish me luck!
|
|
|
Re: More than One "File_Open_Read" - Problem?
[Re: Dooley]
#466937
07/09/17 22:57
07/09/17 22:57
|
Joined: Oct 2011
Posts: 1,082 Germany
Ch40zzC0d3r
Serious User
|
Serious User
Joined: Oct 2011
Posts: 1,082
Germany
|
Download process hacker or any other tool and check for open (file) handles. You will spot 99% of all not closed handles instantly
And yes, your above code was leaking a handle on ANY valid file because you didnt save the handle/return value...
Last edited by Ch40zzC0d3r; 07/09/17 22:58.
|
|
|
Re: More than One "File_Open_Read" - Problem?
[Re: Ch40zzC0d3r]
#466938
07/09/17 23:06
07/09/17 23:06
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
However, in theory, this IS the way to go to check if a file exists IF you want to read it as well!
The reason is that otherwise you might end up with a race condition: You check if the file exists and then open the file. Between the check and you opening the file, the file gets removed and you end up trying to open a file that doesn't exist (which returns an invalid handle).
The right way to do it is to open the file, which checks if it exists anyway, and then to check the returned file handle. And as Chaos pointed out, don't forget to close the files once you are done reading them, or you'll leak file handles.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: More than One "File_Open_Read" - Problem?
[Re: WretchedSid]
#466940
07/10/17 00:09
07/10/17 00:09
|
Joined: May 2005
Posts: 868 Chicago, IL
Dooley
OP
User
|
OP
User
Joined: May 2005
Posts: 868
Chicago, IL
|
Okay, thank you! So I'm imagining something like this: filehandle = file_open_read(filename));
if(filehandle != 0) { file_str_read(filehandle,file_string); file_close(filehandle); } else { file_close(filehandle); }
Last edited by Dooley; 07/10/17 01:05.
|
|
|
Re: More than One "File_Open_Read" - Problem?
[Re: Dooley]
#466941
07/10/17 01:05
07/10/17 01:05
|
Joined: May 2005
Posts: 868 Chicago, IL
Dooley
OP
User
|
OP
User
Joined: May 2005
Posts: 868
Chicago, IL
|
This is what I'm doing currently (after fixing it), do you think this will cause errors? if(file_exists(filename)) { filehandle = file_open_read(filename); file_str_read(filehandle,file_string); file_close (filehandle); }
|
|
|
Re: More than One "File_Open_Read" - Problem?
[Re: Dooley]
#466942
07/10/17 05:27
07/10/17 05:27
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
filehandle = file_open_read(filename));
if(filehandle)
{
file_str_read(filehandle, file_string);
file_close(filehandle);
}
This version is more terse and does exactly what you want.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
|