![]() |
Memory/heap corruption during string manipulation - Printable Version +- Forum (https://www.boriel.com/forum) +-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12) +--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11) +---- Forum: Bug Reports (https://www.boriel.com/forum/forumdisplay.php?fid=15) +---- Thread: Memory/heap corruption during string manipulation (/showthread.php?tid=810) |
Memory/heap corruption during string manipulation - LTee - 06-26-2017 This one's a bit long, sorry in advance! As mentioned in the support forum, I've been having problems with my program growing unstable and crashing after a while. With your help (thanks!) I've narrowed the issue down to a single routine (and possibly a single line, but we'll get to that in a minute) which seems to be causing something bad to happen to the heap and/or memory. I've extracted the offending routine out into the following program: Code: #include <alloc.bas> I'm compiling with: -t -B -a --array-base=1 --string-base=1 --strict-bool --explicit Apologies for the untidy appearance of the code, part of it's ripped straight from my game and the rest I wrote to support this test. The subroutine at line 8500 is supposed to generate six random alien names by taking a base array of 20 partial strings, then randomly choosing 12 of them and joining them together in pairs (perhaps also including a hyphen or apostrophe). I've removed the random elements because they don't seem to affect the result, so in this test the same names will be generated each time. I've also introduced a lot of calls to a routine that prints the available memory and waits for you to press a key - the number displayed on the bottom line of the display shows you where you are in the code at any moment. If you run the program and keep pressing a key, you will see that the numbers change as the names are generated but you're never really short of heap space. However, it's unlikely you'll make it through to the end of all six names without either the heap display going crazy and displaying huge numbers (usually between steps 6 and 7) or screen corruption appearing and the program crashing (usually but not always between steps 3 and 4). I've tried altering the heap size and it doesn't seem to make any difference - even using a 10k+ heap it crashes in the same way. What *does* make a difference (and I have no idea why) is removing line 8545. This is supposed to capitalise the first letter of the name (done in the Sinclair BASIC way). With that line in, the program fails almost every time. REM that line out and it works perfectly, but I don't know why. I've tried taking that single line and writing another test program around that alone, but I can't make it fail outside of this specific context. Knowing that, I could probably work around the issue but I'm a bit worried I'd be leaving a potential bug unfixed if I did. It's also possible I've done something crazy and illegal here, in which case I'd love to know what it is so that I don't do it again! Let me know if anything doesn't make sense or you need anything else! And thanks, as always. ![]() Re: Memory/heap corruption during string manipulation - LTee - 06-26-2017 Okay, having said all that I think I might be able to show something weird happening with that capitalisation line of code in a simpler program. This code repeatedly sets a string to 'test' and then tries to capitalise the first letter to give 'Test': Code: #include <alloc.bas> The weird thing here is the longer you run the routine, the higher the value of memavail - it just gets bigger and bigger. Would I be correct in assuming it's not supposed to behave like that? |