Re: [xml] xmlGcMemSetup & xmlMemSetup

I tried to use xmlMemSetup and libxml2 gives me an error.
Probably I'm missing something.
This is a simple main I wrote:

#include <libxml/parser.h>
#include <libxml/xmlmemory.h>
#include <libxml/encoding.h>
#include <stdlib.h>

const unsigned int mgc = 0x88DEFEA7;

static void *xml2_alloc(size_t sz)
  void *mem = malloc(sz + sizeof(int));
  const unsigned int *mark = mem;
  *mark = mgc;
  return (char *) mem + sizeof(int);
 static void xml2_free(void *emem)
  void *mem = (char *)emem - sizeof(int);
  const unsigned int *mark = mem;
  if (*mark == mgc) {

static void *xml2_realloc(void * emem, size_t sz)
  void *mem = (char *)emem - sizeof(int);
  void *mem_re = xmlMemRealloc(mem, sz);
  if (mem_re != NULL)
      return (char *) mem_re + sizeof(int);
      return NULL;

parseDoc(char *xmlfile)
  xmlDocPtr doc;
  xmlNodePtr root;

  doc = xmlParseFile(xmlfile);

  if (doc == NULL ) {
      fprintf(stderr,"Document not parsed successfully. \n");
      return (NULL);

  root = xmlDocGetRootElement(doc);

  if (root == NULL) {
      fprintf(stderr,"empty document\n");
      return (NULL);

  return doc;


main(int argc, char **argv) {
  xmlDocPtr doc;
  xmlChar *bufptr;
  int size = 0;
  char *xmlfile = "/usr/local/apache/conf/conf_prova.xml";
    xmlMemSetup(xml2_free, xml2_alloc, xml2_realloc, xmlMemoryStrdup);
    doc = parseDoc (xmlfile);
    if (doc == NULL) {
      fprintf(stderr,"[ERRORE] ParseDoc ha ritornato NULL\n");
      return -1;
    fprintf(stderr,"[OK] ParseDoc Ok\n");

  return 0;

The xml document is very simple:

<?xml version="1.0" encoding="ISO-8859-1"?>

this is the error I get:

[site sms-lt01 test]$ ./main
xmlMallocBreakpoint reached on block 0
Memory tag error occurs :0x805c140
Memory tag error occurs :0x805c158
/usr/local/apache/conf/conf_prova.xml:1: parser error : Memory allocation failed
<?xml version="1.0" encoding="ISO-8859-1"?>
Document not parsed successfully.
[ERRORE] ParseDoc ha ritornato NULL

What am I missing?


Daniel Veillard ha scritto:
On Tue, Aug 07, 2007 at 12:09:05PM +0200, Marco Spinetti wrote:
Reading the docs, I don't understand the difference between xmlMemSetup and xmlGcMemSetup. I need to overwrite libxml2 memory allocation and I don't understand if I have to use xmlGcMemSetup or xmlMemSetup.
Any hints?
the later has an extra function allowing a special function for
allocation of atomic area (i.e. areas which will never be grown).
In doubt use xmlMemSetup()

Other question: is xmlInitMemory(9 should be called before use xmlGcMemSetup or xmlMemSetup?

  no, xmlMemSetup must be the very first call to libxml2 then call
xmlInitParser(). Don't call xmlInitMemory by yourself, xmlInitParser
will do it.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]