Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Possible fix for the #11 (ARC version)

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 9072ff2c6d35e52143fad5ae487375f4ec65ba12 1 parent 75f365a
@schwa schwa authored
View
9 Source/CXMLDocument.m
@@ -181,13 +181,12 @@ - (id)initWithContentsOfURL:(NSURL *)inURL encoding:(NSStringEncoding)encoding o
- (void)dealloc
{
- // Fix for #35 http://code.google.com/p/touchcode/issues/detail?id=35 -- clear up the node objects first (inside a pool so I _know_ they're cleared) and then freeing the document
-
- @autoreleasepool {
+ for (CXMLNode *theNode in nodePool)
+ {
+ [theNode invalidate];
+ }
nodePool = NULL;
-
- }
//
xmlFreeDoc((xmlDocPtr)_node);
_node = NULL;
View
14 Source/CXMLNode.m
@@ -48,19 +48,7 @@ @implementation CXMLNode
- (void)dealloc
{
-if (_node)
- {
- if (_node->_private == (__bridge void *)self)
- _node->_private = NULL;
-
- if (_freeNodeOnRelease)
- {
- xmlFreeNode(_node);
- }
-
- _node = NULL;
- }
-//
+[self invalidate];
}
- (id)copyWithZone:(NSZone *)zone;
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 == (__bridge void *)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;

1 comment on commit 9072ff2

@runmad

I am still having an issue with #11.

Crashes at CXMLNode invalidate if (_node->_private == (__bridge void *)self)

Please sign in to comment.
Something went wrong with that request. Please try again.