Looking at the comments, I had to review your code again. I didn't see at first where in the code you are retaining the bitmaps in memory. Being able to load only 4 or 5 is indicative of retaining the bitmaps in memory. It's also indicative of allocating larger and larger bitmaps with intervening objects that are not deallocated. The first step is to stop retaining both the bitmaps and the regions. Although I can't say exactly what the memory requirements of a region is, I have tested bitmaps. Here's the post where I relate my experience on
memory limits and bitmaps.
If you're allocating large and increasing chunks, I could easily see it taking up memory from fragmentation, see the diagram below.
Note that in this diagram, bitmap4 is bigger than bitmap3, which is bigger than bitmap2, which is bigger than bitmap1. Any allocation that is not freed (represented by the |x|) causes fragmentation.
|<---bitmap1--->|x|<-----free ------------------------------------------------------>| AFTER ALLOCATING bitmap1
|<---free ----->|x|<---bitmap2------->|x|<-----free -------------------------------->| AFTER DEALLOCATING bitmap1 and ALLOCATING bitmap2
|<---free ----->|x|<---free --------->|x|<---bitmap3--------->|x|<-----free -------->| AFTER DEALLOCATING bitmap2 and ALLOCATING bitmap3
|<---free ----->|x|<---free --------->|x|<---free ----------->|x|<---bitmap4----------->| FAILS
Perhaps the allocation of |x| is not obvious. The two examples of fragmenting allocations that are in the code segment you provided are:
msgport = CreateObject("roMessagePort")
screen.SetPort(msgport)
and
codes = bslUniversalControlEventCodes()
Move both of these code segments to before the NewFile label. And remove array of regions.
Then let us know how that works!