malloc(): invalid size (unsorted)

Linux specific questions.
N3trunn3r
Posts: 110
Joined: Feb 14, 2008 15:48

malloc(): invalid size (unsorted)

Post by N3trunn3r »

I am trying to compile an old game I wrote for Windows 7 several years ago.
It works fine on Windows but when I try to compile under Debian 10 x64 i get:
malloc(): invalid size (unsorted)
I could put in some print statement to find the culprit but I would like to take this opportunity to learn something new.
Is there a better way to debug this?

It's not a small game either.
I compile with: fbc -w all -exx
All my other smaller games compile successfully tough.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: malloc(): invalid size (unsorted)

Post by counting_pine »

Searching the error seems to reveal it's a common error in C programming. It looks like it means the program is going out of bounds in allocated memory - maybe a string or dynamic array, or something you've allocated yourself. (It's probably not an array though, if -exx doesn't catch it.)

A debugger such as gdb should be able to tell you more.
If you compile with -g, load into gdb and 'run' the program, then it should halt at that point, and might give you a line number, or allow you to backtrace ('bt') to see which function(s) the code was running.
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: malloc(): invalid size (unsorted)

Post by badidea »

A possible quick check: Compile and run with 32-bit fbc (instead of 64-bit). If the 32-bit version does work, it could be related to (u)integer (or mabye any ptr) use.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: malloc(): invalid size (unsorted)

Post by jj2007 »

N3trunn3r wrote:I could put in some print statement to find the culprit
That's the best solution. Put it before and after all instructions that access allocated memory.

One common error: The function xy needs a 100 char buffer, so you generously allocate 101 bytes, but xy doesn't agree with you.
marcov
Posts: 3455
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: malloc(): invalid size (unsorted)

Post by marcov »

(I would see if I can compile it for *nix and then try to use valgrind)
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: malloc(): invalid size (unsorted)

Post by counting_pine »

jj2007 wrote:
N3trunn3r wrote:I could put in some print statement to find the culprit
That's the best solution. Put it before and after all instructions that access allocated memory.
I think this would only be the best solution if you wanted to minimise the amount of learning you have to do.
Debuggers can be very useful and helpful tools.
I think one of the problems is simply that Microsoft implemented very accessible, user-friendly, integrated debuggers in their versions of BASIC, which can make it harder to transition to tools like gdb.
marcov
Posts: 3455
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: malloc(): invalid size (unsorted)

Post by marcov »

counting_pine wrote:
I think one of the problems is simply that Microsoft implemented very accessible, user-friendly, integrated debuggers in their versions of BASIC, which can make it harder to transition to tools like gdb.
The core problem is that GDB sucks of course ;-) Not the good integrated debuggers: -)
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: malloc(): invalid size (unsorted)

Post by jj2007 »

counting_pine wrote:Debuggers can be very useful and helpful tools.
marcov wrote:The core problem is that GDB sucks of course ;-) Not the good integrated debuggers: -)
In a 22kLines source, I have 550 debug lines. They are all switched off, i.e. no code is generated. Whenever I stumble over a problem, I go to the suspect section and activate a few of them.

I've worked on that source for a decade or so, and it's the best solution for me. From time to time, when I am really stuck (this is Assembly...), I resort to a "real" debugger (OllyDbg in this case), but digging so deep is too tiresome to be a general solution.

What about a debug macro in FB, that can be switched on and off?
Xusinboy Bekchanov
Posts: 783
Joined: Jul 26, 2018 18:28

Re: malloc(): invalid size (unsorted)

Post by Xusinboy Bekchanov »

You can also check with fbmld or FBMemCheck
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: malloc(): invalid size (unsorted)

Post by jj2007 »

Yes indeed, FbMemCheck is one way to deal with it. You delegate allocating and freeing memory to a routine that can, on request, do additional checks. Its use is limited because the problem is usually not the allocation but rather the writing beyond the allocated memory - and that happens elsewhere. Under Windows, you can catch it with tools like DebugHeapTest. If there is interest, I can provide a version for FB.
Imortis
Moderator
Posts: 1923
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: malloc(): invalid size (unsorted)

Post by Imortis »

jj2007 wrote:Yes indeed, FbMemCheck is one way to deal with it. You delegate allocating and freeing memory to a routine that can, on request, do additional checks. Its use is limited because the problem is usually not the allocation but rather the writing beyond the allocated memory - and that happens elsewhere. Under Windows, you can catch it with tools like DebugHeapTest. If there is interest, I can provide a version for FB.
I would very much like to see a version of this in FB. It is always good to have something else in debugging toolbox.
marcov
Posts: 3455
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: malloc(): invalid size (unsorted)

Post by marcov »

Afaik most of those tools simply have options to check memory integrity on each existing allocations into the heap manager (an alloc/realloc/dealloc), configurable check the memory guards on the current, last <n> or all allocations (the latter of course slows down tremendously).

Quite often they also don't reuse memory used for objects (or not for a while) to detect double frees. (if you free an allocation, you put a marker in the memory, and on each free you check that)

The next step is of course engaging the compiler to embed custom calls to check (e.g. every procedure).

All this is to bomb out at the earliest moment, closest to where the corruption happens, so you can work your way through cleaning up mistakes and hopefully rectify the signalled problem in the process.

In the past I have ported (portions of) windows only programs to Linux just to use valgrind.....
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: malloc(): invalid size (unsorted)

Post by jj2007 »

Imortis wrote:
jj2007 wrote:Yes indeed, FbMemCheck is one way to deal with it. You delegate allocating and freeing memory to a routine that can, on request, do additional checks. Its use is limited because the problem is usually not the allocation but rather the writing beyond the allocated memory - and that happens elsewhere. Under Windows, you can catch it with tools like DebugHeapTest. If there is interest, I can provide a version for FB.
I would very much like to see a version of this in FB. It is always good to have something else in debugging toolbox.
Download it here. Drag any FB exe that misbehaves over DebugHeap.exe. Disclaimer: Use at your own risk bla bla.
adeyblue
Posts: 299
Joined: Nov 07, 2019 20:08

Re: malloc(): invalid size (unsorted)

Post by adeyblue »

Imortis wrote: I would very much like to see a version of this in FB. It is always good to have something else in debugging toolbox.
These things are built in to Windows, they're turned on with registry settings or in the exe header amongst other ways. gflags in the Debugging Tools For Windows package is the official tool to manage them. Alternatively if you start your program with a debugger attached, Windows will turn some of heap ones on for you automatically.

You need to manually turn on things like Page Heap mode which is the equivalent of the Electric Fence *nix library, and memory leak checking etc.
Imortis
Moderator
Posts: 1923
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: malloc(): invalid size (unsorted)

Post by Imortis »

jj2007 wrote:
Imortis wrote:
jj2007 wrote:Yes indeed, FbMemCheck is one way to deal with it. You delegate allocating and freeing memory to a routine that can, on request, do additional checks. Its use is limited because the problem is usually not the allocation but rather the writing beyond the allocated memory - and that happens elsewhere. Under Windows, you can catch it with tools like DebugHeapTest. If there is interest, I can provide a version for FB.
I would very much like to see a version of this in FB. It is always good to have something else in debugging toolbox.
Download it here. Drag any FB exe that misbehaves over DebugHeap.exe. Disclaimer: Use at your own risk bla bla.
This is just more MASM code. I thought you said you were going to provide a version for FreeBASIC. Your post was reported on the basis of being basically spam for the MASM forum, yet again. I did not follow through on the report because of the offer of a version for FB. This is what you meant? In that case, I tend to agree with the report.
Post Reply