Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Possible fix for the #11

It seems that if you have a short lived XML document and perform an path query on the document then there's a chance (based on order of release in autorelease pool) that the xpath nodes will get freed before the document node is freed.

This causes a crash.

This fixes the issue by putting the nodes from the xpath query into the document's node pool and then explicitly freeing (via a new invalidate method) the nodes in the node pool before the document is dealloced.
  • Loading branch information...
commit 540e643b63adecfe89c5be6d1b7baad987433a6a 1 parent b19c60f
@schwa schwa authored
View
5 Source/CXMLDocument.m
@@ -187,6 +187,11 @@ - (void)dealloc
NSAutoreleasePool *thePool = [[NSAutoreleasePool alloc] init];
+ for (CXMLNode *theNode in nodePool)
+ {
+ [theNode invalidate];
+ }
+
[nodePool release];
nodePool = NULL;
View
13 Source/CXMLNode.m
@@ -48,18 +48,7 @@ @implementation CXMLNode
- (void)dealloc
{
-if (_node)
- {
- if (_node->_private == self)
- _node->_private = NULL;
-
- if (_freeNodeOnRelease)
- {
- xmlFreeNode(_node);
- }
-
- _node = NULL;
- }
+[self invalidate];
//
[super dealloc];
}
View
2  Source/CXMLNode_PrivateExtensions.h
@@ -39,4 +39,6 @@
+ (id)nodeWithLibXMLNode:(xmlNodePtr)inLibXMLNode freeOnDealloc:(BOOL)infreeOnDealloc;
+- (void)invalidate;
+
@end
View
16 Source/CXMLNode_PrivateExtensions.m
@@ -100,4 +100,20 @@ - (xmlNodePtr)node
return(_node);
}
+- (void)invalidate;
+ {
+ if (_node)
+ {
+ if (_node->_private == self)
+ _node->_private = NULL;
+
+ if (_freeNodeOnRelease)
+ {
+ xmlFreeNode(_node);
+ }
+
+ _node = NULL;
+ }
+ }
+
@end
View
9 Source/CXMLNode_XPathExtensions.m
@@ -32,6 +32,7 @@
#import "CXMLNode_XPathExtensions.h"
#import "CXMLDocument.h"
+#import "CXMLDocument_PrivateExtensions.h"
#import "CXMLNode_PrivateExtensions.h"
#include <libxml/xpath.h>
@@ -65,8 +66,12 @@ - (NSArray *)nodesForXPath:(NSString *)xpath namespaceMappings:(NSDictionary *)i
int N;
for (N = 0; N < theXPathObject->nodesetval->nodeNr; N++)
{
- xmlNodePtr theNode = theXPathObject->nodesetval->nodeTab[N];
- [theArray addObject:[CXMLNode nodeWithLibXMLNode:theNode freeOnDealloc:NO]];
+ xmlNodePtr theLibXMLNode = theXPathObject->nodesetval->nodeTab[N];
+
+ CXMLNode *theNode = [CXMLNode nodeWithLibXMLNode:theLibXMLNode freeOnDealloc:YES];
+ [self.rootDocument.nodePool addObject:theNode];
+
+ [theArray addObject:theNode];
}
theResult = theArray;
Please sign in to comment.
Something went wrong with that request. Please try again.