ReadLump data is read twice when lumps accumulate

Bugs that have been investigated and resolved somehow.

Moderator: GZDoom Developers

Forum rules
Please don't bump threads here if you have a problem - it will often be forgotten about if you do. Instead, make a new thread here.
Post Reply
User avatar
Nash
 
 
Posts: 17433
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

ReadLump data is read twice when lumps accumulate

Post by Nash »

Code: Select all

    void ReadTestLump(void)
    {
        testData.Clear();

        int lump = -1;
        while (-1 != (lump = Wads.FindLump('TESTLUMP', lump + 1)))
        {
            String data = Wads.ReadLump(lump);

            // split lines
            Array<String> lines;
            data.Split(lines, "\n", TOK_KEEPEMPTY);

            // strip comments
            for (int i = 0; i < lines.Size(); i++)
            {
                if (lines[i].IndexOf("//") == 0)
                {
                    continue;
                }
                else
                {
                    testData.Push(lines[i]);
                }
            }
        }

        //Console.Printf("size: %d", testData.Size());
        for (int i = 0; i < testData.Size(); i++)
        {
            // delete that weird character that gets added at the end
            testData[i].Truncate(testData[i].Length() - 1);

            if (testData[i])
            {
                Console.Printf("%s\n", testData[i]);
            }
        }
    }
 
Load ReadLump.pk3 and ReadLump Mod.pk3, in that order. If a second TestLump.txt is added into your load order, the data contents from the first lump be listed twice. Why does this happen?
Attachments
ReadLump Mod.pk3
(221 Bytes) Downloaded 118 times
ReadLump.pk3
(1.13 KiB) Downloaded 94 times
User avatar
phantombeta
Posts: 2084
Joined: Thu May 02, 2013 1:27 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Brazil

Re: ReadLump data is read twice when lumps accumulate

Post by phantombeta »

Somewhat user error, actually. Caused by an insane, non-sensical, ill-advised, terrible decision in the ZScript compiler.
Function-local variables are never initialized at all. The VM's registers are, but that only happens once, so any variables defined after anything that isn't a variable definition will contain garbage data. And this extends to dynamic arrays.

TL;DR User error caused by a terrible design decision in the ZScript compiler. Always clear your dynamic arrays.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49056
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: ReadLump data is read twice when lumps accumulate

Post by Graf Zahl »

This here is actually a real bug. Arrays get initialized at function init, but obviously require a Clear when being declared within a loop.
User avatar
Nash
 
 
Posts: 17433
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: ReadLump data is read twice when lumps accumulate

Post by Nash »

Should this thread be moved to Bugs, then?
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49056
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: ReadLump data is read twice when lumps accumulate

Post by Graf Zahl »

Sure. But you can also fix it on your side by clearing the array right after the declaration.
User avatar
Ozymandias81
Posts: 2062
Joined: Thu Jul 04, 2013 8:01 am
Graphics Processor: nVidia with Vulkan support
Location: Mount Olympus, Mars
Contact:

Re: ReadLump data is read twice when lumps accumulate

Post by Ozymandias81 »

Could be this issue related to this one? Since I load BoA as resource folder from GZDBuilder under GZDoom, there are gzdoom related pk3s loading before BoA...
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49056
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: ReadLump data is read twice when lumps accumulate

Post by Graf Zahl »

Are you using ZScript arrays?
User avatar
Ozymandias81
Posts: 2062
Joined: Thu Jul 04, 2013 8:01 am
Graphics Processor: nVidia with Vulkan support
Location: Mount Olympus, Mars
Contact:

Re: ReadLump data is read twice when lumps accumulate

Post by Ozymandias81 »

I am not the one who is coding with zscript since I am not familiar with it, but yes we are using it
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49056
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: ReadLump data is read twice when lumps accumulate

Post by Graf Zahl »

Then check if you define an array inside a for or while loop. That is the only condition which can trigger this.
User avatar
Gustavo6046
Posts: 136
Joined: Sat May 13, 2017 3:11 pm
Location: Brazil
Contact:

Re: ReadLump data is read twice when lumps accumulate

Post by Gustavo6046 »

Sorry for the bump, but I was wondering, doesn't declaring an array in a loop and clearing it delete "garbage data", that could actually happen to be useful memory elsewhere, and potentially cause side effects somewhere else in the game?

I mean, for there to be garbage data, the data must exist. I don't know if it's malloc-ing data that has been freed prior to allocating it again from ZScript.
_mental_
 
 
Posts: 3812
Joined: Sun Aug 07, 2011 4:32 am

Re: ReadLump data is read twice when lumps accumulate

Post by _mental_ »

I propose to fix it like this.
User avatar
Nash
 
 
Posts: 17433
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: ReadLump data is read twice when lumps accumulate

Post by Nash »

Heh, I have gotten into the habit of immediately Clear()'ing locally-declared dynamic arrays that I forgot I made this report. :lol: Still, a proper fix would be welcome, of course.
User avatar
Matt
Posts: 9696
Joined: Sun Jan 04, 2004 5:37 pm
Preferred Pronouns: They/Them
Operating System Version (Optional): Debian Bullseye
Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia
Contact:

Re: ReadLump data is read twice when lumps accumulate

Post by Matt »

Gustavo6046 wrote:Sorry for the bump, but I was wondering, doesn't declaring an array in a loop and clearing it delete "garbage data", that could actually happen to be useful memory elsewhere, and potentially cause side effects somewhere else in the game?

I mean, for there to be garbage data, the data must exist. I don't know if it's malloc-ing data that has been freed prior to allocating it again from ZScript.
This could be what's been causing a lot of crashes for me lately...
Post Reply

Return to “Closed Bugs [GZDoom]”