Re: [pcre-dev] pcre_free crash

Top Page
Delete this message
Author: Philip Hazel
Date:  
To: Ryan Joseph
CC: pcre-dev
Subject: Re: [pcre-dev] pcre_free crash
On Sun, 20 Sep 2009, Ryan Joseph wrote:

> I just read in the man pages that pcre_free is a global variable and
> points to free by default. In Pascal I'm loading it as an external
> function, which the linker accepts but maybe it's not pointing to an
> actual function and thus crashing? I'll have to ask some people who
> are familiar with my Pascal compiler FPC but can you offer any advice
> as to how it should be used? Thank you.


I'm afraid I know nothing about Pascal. In C, having declared pcre_free
correctly, you can just use it just like a normal function (see, for
example, the pcredemo.c example program).

> Btw, I also read that the pointer to be freed is a single block of
> memory so I was able to use another function to call malloc's free
> which seems to work just fine. Maybe it's not necessary I resolve this
> the correct way?


That is OK. The main reason for having pcre_malloc and pcre_free is so
that any calls from *inside* pcre can be trapped if the calling programs
want to.

While checking up on this, I found the following fragment in pcre.h:

-------------------------------------------------------------------------------
/* Indirection for store get and free functions. These can be set to
alternative malloc/free functions if required. Special ones are used in the
non-recursive case for "frames". There is also an optional callout function
that is triggered by the (?) regex item. For Virtual Pascal, these definitions
have to take another form. */                                           


#ifndef VPCOMPAT                                                    
PCRE_EXP_DECL void *(*pcre_malloc)(size_t);                             
PCRE_EXP_DECL void  (*pcre_free)(void *);                              
PCRE_EXP_DECL void *(*pcre_stack_malloc)(size_t);                      
PCRE_EXP_DECL void  (*pcre_stack_free)(void *);                           
PCRE_EXP_DECL int   (*pcre_callout)(pcre_callout_block *);
#else   /* VPCOMPAT */                                                       
PCRE_EXP_DECL void *pcre_malloc(size_t);
PCRE_EXP_DECL void  pcre_free(void *);
PCRE_EXP_DECL void *pcre_stack_malloc(size_t);
PCRE_EXP_DECL void  pcre_stack_free(void *);
PCRE_EXP_DECL int   pcre_callout(pcre_callout_block *);
#endif  /* VPCOMPAT */              
-------------------------------------------------------------------------------


I don't know if you are using "Virtual Pascal", but this is a hint that
there is some issue with pcre_free().

I also remembered that PCRE contains this function:

-------------------------------------------------------------------------------
/*************************************************                        
*   Free store obtained by get_substring_list    *
*************************************************/                        


/* This function exists for the benefit of people calling PCRE from non-C
programs that can call its functions, but not free() or (pcre_free)() directly.

Argument:   the result of a previous pcre_get_substring_list()           
Returns:    nothing                                                       
*/                                                                      


PCRE_EXP_DEFN void PCRE_CALL_CONVENTION                             
pcre_free_substring_list(const char **pointer)                          
{                                                                      
(pcre_free)((void *)pointer);                                          
}                                                                         
-------------------------------------------------------------------------------


Note the comment at the start. Although it is aimed at freeing substring
lists, you'll see that it just calls pcre_free, and so you could use it
to free the compiled regex as well.

Philip

--
Philip Hazel