Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by EternallyCurious. 04/18/24 10:45
StartWeek not working as it should
by Zheka. 04/18/24 10:11
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (ozgur, TipmyPip), 722 guests, and 0 spiders.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, howardR, 11honza11, ccorrea, sakolin
19047 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 2 of 3 1 2 3
Re: More than One "File_Open_Read" - Problem? [Re: Dooley] #466760
06/30/17 23:21
06/30/17 23:21
Joined: May 2005
Posts: 868
Chicago, IL
Dooley Offline OP
User
Dooley  Offline OP
User

Joined: May 2005
Posts: 868
Chicago, IL
Yes, replacing my map entity with a model does not increase the nexus on each ent_create.

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 Offline
Expert
WretchedSid  Offline
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: WretchedSid] #466797
07/03/17 07:23
07/03/17 07:23
Joined: May 2005
Posts: 868
Chicago, IL
Dooley Offline OP
User
Dooley  Offline OP
User

Joined: May 2005
Posts: 868
Chicago, IL
Possibly. However, the memory leak is causing the game to crash too. If I can fix it, at least I can move on to the next possible cause...

Re: More than One "File_Open_Read" - Problem? [Re: WretchedSid] #466922
07/08/17 08:08
07/08/17 08:08
Joined: May 2005
Posts: 868
Chicago, IL
Dooley Offline OP
User
Dooley  Offline OP
User

Joined: May 2005
Posts: 868
Chicago, IL
Oh man, you were right. I did a great job of reducing the amount of memory the game uses. Memory usage seems to stabilize much lower than the previous version of the game.

However, the data corruption is still happening.

Okay, I am opening up a file, reading from it, and storing the information in variables. Then, it re-writes the file with updated information and saves it again. So I guess this may be what you described as "overriding existing data."

Can you tell me if there is a right or wrong way to do this? If it's as simple as just not over-writing a file, I may be able to come up with a better way to do it.

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 Offline OP
User
Dooley  Offline 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
C
Ch40zzC0d3r Offline
Serious User
Ch40zzC0d3r  Offline
Serious User
C

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 Offline
Expert
WretchedSid  Offline
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 Offline OP
User
Dooley  Offline OP
User

Joined: May 2005
Posts: 868
Chicago, IL
Okay, thank you!

So I'm imagining something like this:

Quote:
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 Offline OP
User
Dooley  Offline 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?

Quote:
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 Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
Code:
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
Page 2 of 3 1 2 3

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1