Cause for rlink32 Too many resources to handle

By | July 16, 2014

If you have many DFM files and RES files in your project you may run into the rlink32 error “Too many resources to handle”. Every DFM file is one resource and a RES file can contain multiple resources. All together must not exceed 3626 resources.

This limit comes from how rlink32 pre-allocates memory for all resources.  The following pseudo code shows the memory allocator.

function AllocateResourceArray(var AllocatedSize: Word): PResourceArray;
var
  Size: Integer;
begin
  Size := $FF00;
  repeat
    Result := GetMemory(Size);
    if Result <> nil then
    begin
      AllocatedSize := Size;
      Exit;
    end;
    Dec(Size, $400);
  until Size < $400;
  AllocatedSize := 0;
end;


There is an easy fix without much code change:
As you can see the max. number of bytes for the whole resource array is limited by $FF00 (65280) bytes. A resource array item’s size is 18 Bytes. That makes 3626 items. If the memory allocator can’t allocate enough memory it tries to allocate again with 1024 bytes less. So the max. number of resources can be lower than 3626. But wait, we have 2014 and not 1992 (rlink32.dll’s copyright message) and RAM is like sand on the beach, at least some developer think that. The 64KB age is long gone, but due to this function rlink32 will fail with “Too many resources to handle” if the 3627’s resource is processed.

Change the var-parameter from “Word” to “LongWord” and set the initial size to $11FFDC (1,179,612 bytes) which makes room for 65534 resource items which should be enough at the moment. Adjust the code that calls into this function (there is only one call). Recompile rlink32.dll.

Unfortunately I can’t recompile rlink32.dll due to the lack of source code, so I need to patch the machine code. And changing the data type from “Word” to “LongWord” isn’t that easy as you have to patch a lot more places that the compiler would just generate for you (fortunately the 32-bit opcodes are usually smaller than the 16-bit opcodes).

This bug fix for the rlink32 error will be in the next version 5.7 of IDE Fix Pack (for the IDE compiler) and fastdcc (for the command line compiler).