Re: [xml] xmlGcMemSetup & xmlMemSetup

With the help of William I adjust my source.
I attached the source and xml, xsl for the test.
Now the code seems to be ok but there is always a segmentation fault.
It seems that using xmlMemSetup if in the xsl there is a global variable with & xsltApplyStylesheet fails and it gives a segmentation fault. If you try to delete & from the variable urlbase there is no segmentation fault.
I tried to use valgrind and it seems that there is no error:

> valgrind -v xmem2
==31961== Memcheck, a memory error detector.
==31961== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==31961== Using LibVEX rev 1732, a library for dynamic binary translation.
==31961== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==31961== Using valgrind-3.2.3, a dynamic binary instrumentation framework.
==31961== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
--31961-- Command line
--31961--    xmem2
--31961-- Startup, with flags:
--31961--    -v
--31961-- Contents of /proc/version:
--31961-- Linux version 2.4.2-2 (root porky devel redhat com) (gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-79)) #1 Sun Apr 8 20:41:30 EDT 2001
--31961-- Arch and hwcaps: X86, x86-sse1
--31961-- Page sizes: currently 4096, max supported 4096
--31961-- Valgrind library directory: /usr/local/lib/valgrind
--31961-- Reading syms from /lib/ (0x4000000)
--31961-- Reading syms from /usr/local/home/siteadm/sviluppo/test/xmem2 (0x8048000) --31961-- Reading syms from /usr/local/lib/valgrind/x86-linux/memcheck (0x38000000)
--31961--    object doesn't have a dynamic symbol table
--31961-- Reading suppressions file: /usr/local/lib/valgrind/default.supp
You have invoked `', the helper program for shared library executables.
This program usually lives in the file `/lib/', and special directives
in executable files using ELF shared libraries tell the system's program
loader to load the helper program from this file.  This helper program loads
the shared libraries needed by the program executable, prepares the program
to run, and runs it.  You may invoke this helper program directly from the
command line to load and run an ELF executable file; this is like executing
that file itself, but always uses this helper program from the file you
specified, instead of the helper program file specified in the executable
file you run.  This is mostly of use for maintainers to test new versions
of this helper program; chances are you did not intend to run this program.

 --list                list all dependencies and how they are resolved
--verify verify that given object really is a dynamically linked
                       object we can handle
 --library-path PATH   use given PATH instead of content of the environment
                       variable LD_LIBRARY_PATH
 --inhibit-rpath LIST  ignore RUNPATH and RPATH information in object names
                       in LIST
==31961== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 3 from 1)
--31961-- supp:    3 _dl_start(Cond)
==31961== malloc/free: in use at exit: 0 bytes in 0 blocks.
==31961== malloc/free: 0 allocs, 0 frees, 0 bytes allocated.
==31961== All heap blocks were freed -- no leaks are possible.
--31961--  memcheck: sanity checks: 0 cheap, 1 expensive
--31961--  memcheck: auxmaps: 0 auxmap entries (0k, 0M) in use
--31961--  memcheck: auxmaps: 0 searches, 0 comparisons
--31961--  memcheck: SMs: n_issued      = 4 (64k, 0M)
--31961--  memcheck: SMs: n_deissued    = 0 (0k, 0M)
--31961--  memcheck: SMs: max_noaccess  = 65535 (1048560k, 1023M)
--31961--  memcheck: SMs: max_undefined = 0 (0k, 0M)
--31961--  memcheck: SMs: max_defined   = 1 (16k, 0M)
--31961--  memcheck: SMs: max_non_DSM   = 4 (64k, 0M)
--31961--  memcheck: max sec V bit nodes:    0 (0k, 0M)
--31961--  memcheck: set_sec_vbits8 calls: 0 (new: 0, updates: 0)
--31961--  memcheck: max shadow mem size:   368k, 0M
--31961-- translate:            fast SP updates identified: 131 ( 89.1%)
--31961-- translate:   generic_known SP updates identified: 9 (  6.1%)
--31961-- translate: generic_unknown SP updates identified: 7 (  4.7%)
--31961--     tt/tc: 288 tt lookups requiring 287 probes
--31961--     tt/tc: 288 fast-cache updates, 2 flushes
--31961--  transtab: new        144 (3,042 -> 52,671; ratio 173:10) [0 scs]
--31961--  transtab: dumped     0 (0 -> ??)
--31961--  transtab: discarded  0 (0 -> ??)
--31961-- scheduler: 4,556 jumps (bb entries).
--31961-- scheduler: 0/148 major/minor sched events.
--31961--    sanity: 1 cheap, 1 expensive checks.
--31961--    exectx: 30,011 lists, 3 contexts (avg 0 per list)
--31961--    exectx: 3 searches, 0 full compares (0 per 1000)
--31961--    exectx: 0 cmp2, 3 cmp4, 0 cmpAll

Do you have the error too?
Best regards


Daniel Veillard ha scritto:
On Wed, Aug 08, 2007 at 03:31:05PM +0200, Marco Spinetti wrote:
I tried to use gdb to debug the sorce I attached last email.
The instruction in my source which gives me the error is:
  hum, unclear why this would specifically fail there.

Could you help me to find the error?
Did you try to compile my source and executes it?

  I'm sorry, no, I don't have time to debug other people's code in
general. Sometimes I do but it's the exception, not the norm. Others
on the list may help too.


<?xml version="1.0" encoding="UTF-8"?>
#include <libxml/parser.h>
#include <libxml/xmlmemory.h>
#include <libxml/encoding.h>

#include <libxslt/xslt.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>
#include <libexslt/exslt.h>

#include <stdlib.h>
#include <string.h>

const unsigned int mgc = 0x88DEFEA7;

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

static void *xml2_realloc(void * emem, size_t sz)
    void *mem;
    const unsigned int *s;
    void *mem_re;
    if (emem == NULL)
        return xml2_alloc(sz);
    else if (sz == 0) {
        return NULL;
    mem = (char *)emem - 2 * sizeof(int);
    s = mem;
    mem_re = xml2_alloc(sz);
    memcpy(mem_re, emem, *s);
    return mem_re;

static char *xml2_strdup(const char * str)
    char *s = xml2_alloc(strlen(str) + 1);
    if (s == NULL)
        return NULL;
    strcpy(s, str);
    return s;

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;
    xsltStylesheetPtr cur = NULL;
    xmlDocPtr res;

    xmlChar *bufptr;
    int size = 0;
    char *xmlfile = "prova1.xml";
    char * xslfilename    = "xxx.xsl";
    xmlMemSetup(xml2_free, xml2_alloc, xml2_realloc, xml2_strdup);
    doc = parseDoc (xmlfile);
    if (doc == NULL) {
        fprintf(stderr,"[ERRORE] ParseDoc ha ritornato NULL\n");
        return -1;

    cur = xsltParseStylesheetFile((const xmlChar *)xslfilename);
    res = xsltApplyStylesheet(cur, doc, NULL);
    if (xsltSaveResultToString(&bufptr, &size, res, cur) == -1) {
        fprintf(stderr, "Errore nella funzione xsltSaveResultToString\n");



        return -1;
    fprintf(stderr, "Out = %s\n", bufptr);


    return 0;
<xsl:transform version="1.0"
xmlns:xsl=""; xmlns:test="#">

<xsl:variable name="id">14</xsl:variable>
<xsl:variable name="ip">X</xsl:variable>

<xsl:variable name="urlbase">/cgi-bin/test?id=<xsl:value-of select="$id" />&amp;ip=<xsl:value-of select="$ip" 

<xsl:template match="/">
<xsl:template match='@*|node()'>
    <xsl:apply-templates select='@*|node()'/>

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