We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 428

Summary: SDL_malloc.c - bug in allocation
Product: SDL Reporter: Alex <alex.d>
Component: *don't know*Assignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P1    
Version: 1.2.11   
Hardware: x86   
OS: Linux   

Description Alex 2007-05-15 08:12:19 UTC
When memory routines from SDL_malloc.c are used, memory allocation may fail without apparent reasons (despite enough memory available). Subsequent allocations usually succeed.

In my case it was showing up only with SDL_mixer, but I believe it's generic and just triggered by some allocation/deallocation patterns specific to SDL_mixer.

After a bit of debugging, it seems to me that the problem originates from not considering the overhead due to alignment requirements in sys_alloc().

I.e., say

asize = granularity_align(nb + TOP_FOOT_SIZE + SIZE_T_ONE);

@line 3411 - sys_alloc()

Later, the actual usable size may become smaller, because of alignment:

  size_t offset = align_offset(chunk2mem(p));
  p = (mchunkptr)((char*)p + offset);
  psize -= offset;

@lines 3231-3233 in init_top()

In my case, the problem seemed to go away when adding MALLOC_ALIGNMENT in all such calculations in sys_alloc(), like

asize = granularity_align(nb + TOP_FOOT_SIZE + MALLOC_ALIGNMENT + SIZE_T_ONE);

To ensure that size is (at least) enough after applying alignment. I'm not sure it's the best solution though.
Comment 1 Ryan C. Gordon 2007-06-02 13:59:02 UTC
Bumping a bunch of bugs to Priority 1 for consideration for the 1.2.12 release.

--ryan.

Comment 2 Sam Lantinga 2007-06-14 23:09:23 UTC
Is there any chance you can give a simple test case to reproduce the problem?  Or lacking that, a step by step trace through the function showing all variable values?

Thanks!
Comment 3 Sam Lantinga 2007-07-15 15:16:22 UTC
Date: Sun, 15 Jul 2007 17:55:33 -0400
From: Doug Lea
To: Sam Lantinga <slouken@devolution.com>
Subject: Re: SDL_malloc.c bug details (fwd)

A first pass diagnosis is that when alignment is greater than reserved footer space, further padding is necessary. (A quick fix is just to add one alignment's worth to request size, but it is likely that this can be reduced with some thought.)

-Doug
Comment 4 Sam Lantinga 2007-07-15 17:11:23 UTC
The suggested fix was approved by Doug Lea and checked into subversion.  "overkill, but safe"  He's going to have a new release of his malloc implementation sometime next month and I'll use that when it's released.