discuss-gnustep
[Top][All Lists]
Advanced

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

Re: GSXML


From: Richard Frith-Macdonald
Subject: Re: GSXML
Date: Tue, 21 May 2002 09:33:43 +0100

On Monday, May 20, 2002, at 02:54 PM, Nicola Pero wrote:


I have to say that in that case I don't know what on earth you are
talking about.

My pleasure - try the following code -

#include <Foundation/Foundation.h>
#include <Foundation/GSXML.h>

/* Parse a NSString, returning the root node as parsed.  */
GSXMLNode *rootNode (NSString *string)
{
  NSData *data = [string dataUsingEncoding: NSUTF8StringEncoding];
  GSXMLParser *parser = [GSXMLParser parser];
  if ([parser parse: data] == NO)
    {
      NSLog (@"Couldn't parse");
      return nil;
    }
  return [[parser doc] root];

Incorrect code ... returning an object referring to non-retained libxml content.

}

int main (void)
{
  CREATE_AUTORELEASE_POOL (pool);

  /* The string we want to parse.  */
  NSString *string = @"<?xml version=\"1.0\"?><test></test>";

  /* Parse the document and get the root node for later printing.  */
  GSXMLNode *root = rootNode (string);

  /* Retain it because we need it later.  */
  RETAIN (root);

  /* The GUI automatically recreates autorelease pools regularly,
   * so let's simulate what happens in any running GUI program.  */
  DESTROY (pool);

Here we destroy the libxml data.

  pool = [NSAutoreleasePool new];

  /* Now print out the details of the parsed root node.  */
  NSLog (@"Content is :");
  NSLog ([root content]);

Now we try to print the data we destroyed.

  NSLog (@"Attributes are :");
  NSLog ([[root propertiesAsDictionary] description]);

  DESTROY (pool);

  return 0;
}

If you have been able to use non-SAX GSXML for anything serious - you
lucky.  I haven't.

But I suspect you are deliberately failing :-)

ie. From earlier discussions I'm pretty certain you know perfectly well how the memory management in libxml works, and therefore know what your problem is, but as you don't like wrapping of the libxml code and the OpenStep inconsistent way
it works, you choose not to accept that it does work.

A distaste for the memory management is a perfectly legitimate point of view - I would prefer a more consistent system myself - and I may even get round to making it so.

What I think is wrong is that you claim that it does not work and/or is
unreliable when the real case is that it does not work the way you want it to, and you simply have to remember that destroying the GSXMLParser destroys all the underlying data structure - so the parser must be kept in existence as long
as you are working with it.

Fixing this memory 'problem' *is* trivial (you just need all GSXML objects to retain the parser) without changing the parsing API as I said earlier. Perhaps I'll spend
an hour on the train doing that, just to prove the point.

I'd recommend using the GSXML parser, not because it's perfect (it certainly
isn't), but because it's as good as or better than the alternatives!




reply via email to

[Prev in Thread] Current Thread [Next in Thread]