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

Re: gb.xml2: xmlwriter gives error on creating xml file


On Friday, November 13th, 2026 at 19:20, Brian G <brian@xxxxxxxxxxxxxxxx> wrote:

> On 11/13/26 09:22, gbWilly wrote:
> > Sorry, I presumed that Close was based on xmlTextWriterClose() and that EndDocument (in gb.libxml) is based on xmlTextWriterEndDocument() as that makes sense in my head, using names likewise to the libary it interfaces with. I wasn't aware that your close is based on xmlTextWriterEndDocument() as well.
> >
> > But the my question, why then doesn't it work, are you doing things differently, is it because it all became static, something must be causing it. I'm done with it for today, spend better part of an afternoon on this and it's time to prepare me some food.
> >
> > I hope is somehow will be solved.
> > Anyway thank for your time and effort.
> >
> >
> > gbWilly
> >
> >
> >
> Why it happens
> 
> 1. XmlWriter.Open creates the writer with xmlNewTextWriterFilename(path, 
> 0) (src/c_xmlwriter.c:63). In your libxml2 (2.9.14), that writes through 
> a C FILE* opened with fopen.
> 2. XmlWriter.Close (c_xmlwriter.c:181) only calls end_document(), which 
> runs xmlTextWriterEndDocument and then xmlTextWriterFlush. That flush 
> only moves libxml2's own buffer into the FILE* buffer. Nothing calls 
> fflush or fclose, so your small document sits in memory and never 
> reaches the disk.
> 3. The file only gets closed in xmlFreeTextWriter, which runs in 
> XmlWriter_free, the object's destructor. Your local hWriter keeps the 
> object alive until Main returns, so the file fills in only when the app 
> ends.
> 
> Here is the patch that fixes the issues for me
> 
> BEGIN_METHOD_VOID(XmlWriter_Close)
> 
>          if (!THIS->writer) { XML_error("Writer is closed"); return; }
> 
>          if (end_document(THIS))
>                  XML_error("Unable to close document");
> 
>          if (THIS->buffer)
>                  GB.ReturnNewString((char 
> *)xmlBufferContent(THIS->buffer), xmlBufferLength(THIS->buffer));
>          else
>                  GB.ReturnVoidString();
> 
>          xmlFreeTextWriter(THIS->writer);   // closes the FILE* -> data 
> hits disk
>          THIS->writer = NULL;
> 
> END_METHOD
> 
> BEGIN_METHOD_VOID(XmlWriter_free)
> 
>          if (THIS->writer)
>          {
>                  end_document(THIS);
>                  xmlFreeTextWriter(THIS->writer);
>          }
>          if (THIS->buffer) xmlBufferFree(THIS->buffer);
> 
> END_METHOD
> 
> 
> This also stops the destructor from running xmlTextWriterEndDocument a 
> second time after Close, which happens today.
> 
> The other methods (StartElement, AddText, etc.) should also check for 
> THIS->writer == NULL and raise an error, instead of passing a NULL 
> writer to libxml2. A check method in GB_DESC, or a macro at the top of 
> each method, would cover that.
> 
> -- 

Brian, I'm glad to read that it is fixable. It should be fixable as gb.limxml manages to make it work with the same library.
I hope Benoit accepts the fix so I can test it.

gbWilly


Follow-Ups:
Re: gb.xml2: xmlwriter gives error on creating xml fileBenoît Minisini <benoit.minisini@xxxxxxxxxxxxxxxx>
References:
gb.xml2: xmlwriter gives error on creating xml filegbWilly <gbWilly@xxxxxxxxxxxxxx>
Re: gb.xml2: xmlwriter gives error on creating xml filegbWilly <gbWilly@xxxxxxxxxxxxxx>
Re: gb.xml2: xmlwriter gives error on creating xml filegbWilly <gbWilly@xxxxxxxxxxxxxx>
Re: gb.xml2: xmlwriter gives error on creating xml filegbWilly <gbWilly@xxxxxxxxxxxxxx>
Re: gb.xml2: xmlwriter gives error on creating xml fileBenoît Minisini <benoit.minisini@xxxxxxxxxxxxxxxx>
Re: gb.xml2: xmlwriter gives error on creating xml filegbWilly <gbWilly@xxxxxxxxxxxxxx>
Re: gb.xml2: xmlwriter gives error on creating xml filegbWilly <gbWilly@xxxxxxxxxxxxxx>
Re: gb.xml2: xmlwriter gives error on creating xml fileBrian G <brian@xxxxxxxxxxxxxxxx>