Subject: malloc/free issues.
Newsgroups: quics.experts.c
Organization: Technical Support Inc.
Summary:
Keywords:
We’ve been working on a problem where we run a program,
( made with Watcom C 10.6 Patch B on QNX 4.22 ),
restart the machine and it hangs inside of an
opendir() call. I stepped inside the opendir call and it gets
stuck inside of malloc. We fixed the problem (we think) by
calling _heapgrow() and _heapshrink(), in that the problem
goes away when _heapgrow() is called before malloc() and
_heapshrink() is called after free().
Is there a reasonable explanation for this behavior?
Bob Sullivan
sulli@techsi.com
P.S. We previously added header/trailer blocks that were quite large
(>128 bytes) to each allocated piece and they were checked on free()
and
had no damage to them…
P.P.S. code I used to wrap the heapgrow/heapshrink in:
/*
shared.c Source file for retrieving shared memory
Written By: Robert E. Sullivan, Technical Support Inc.
Date: March 1999
Tab Settings: 4
Version: 1.0
Compilers: Watcom C for QNX
ID: $id: shared.c,v 1.3 2000/10/11 16:04:58 sulli Exp
sulli $
Revision $revision: 1.3 $
System Rev 2.00
*/
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include “shared.h”
#include “printer.h” /* pprintf() */
extern char *nline;
#define SHMEM_DEBUG 0
#define SIGNATURE “FS-alloc”
#define SIG4HIGH 0x46532d61 // FS-a –
#define SIG4LOW 0x6c6c6f63 // lloc –
typedef unsigned long FOUR_BYTE;
struct malloc_header
{
union convert
{
char chars[ 8 ];
FOUR_BYTE longs[ 2 ];
} signature;
FOUR_BYTE length;
};
/*
fs_calloc
*/
void *fs_calloc( size_t n, size_t size, char *fname, int line_num )
{
void *p;
void *ret_val = NULL;
_heapgrow();
p = calloc( 1, ( n * size ) );
if ( p )
{
ret_val = p;
}
return(ret_val);
}
/*
fs_free
*/
int fs_free( void *ptr, char *fname, int line_num )
{
int rval = FALSE;
if ( ptr )
{
free( ptr );
_heapshrink(); /* Added to free up space. */
rval = TRUE;
}
else
{
rval = FALSE;
}
return(rval);
}