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

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


On 11/13/26 09:22, gbWilly wrote:
On Friday, November 13th, 2026 at 18:07, Benoît Minisini <benoit.minisini@xxxxxxxxxxxxxxxx> wrote:

Le 13/11/2026 à 18:00, gbWilly a écrit :
Both xmlTextWriterEndDocument() and xmlTextWriterClose() exist in
libxml2 The latter one since libxml2 2.13 None of my systems are on
2.13 or higher.

xmlTextWriterEndDocument() clearly does something different that
xmlTextWriterClose() on a pre-2.13 libxml2 library version, being it
exist and it works. Is it problematic to implement both? gb.xml has
both as well and they both work on my system.

I'm just curious if using EndDocument, based on
xmlTextWriterEndDocument(), will work as it does in gb.libxml. It's
clearly possible with libxml2, but not with gb.xml2, so something is
different between using close based on xmlTextWriterClose() and
EndDocument based on xmlTextWriterEndDocument(). Why else would they
both exist inside the libary?

gbWilly
But why do you talk about 'xmlTextWriterClose()' ? I never call this
method. I have mostly copied the code from 'gb.libxml' in that regard...

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

Attachment: OpenPGP_0x78BFB26402F48419.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature


Follow-Ups:
Re: gb.xml2: xmlwriter gives error on creating xml filegbWilly <gbWilly@xxxxxxxxxxxxxx>
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>