Merge lp:~zorba-coders/zorba/xqxq-memory-smash into lp:zorba/xqxq-module

Proposed by Chris Hillery
Status: Merged
Approved by: Juan Zacarias
Approved revision: 42
Merged at revision: 41
Proposed branch: lp:~zorba-coders/zorba/xqxq-memory-smash
Merge into: lp:zorba/xqxq-module
Diff against target: 313 lines (+130/-43)
7 files modified
src/xqxq.xq.src/xqxq.cpp (+52/-24)
src/xqxq.xq.src/xqxq.h (+23/-19)
test/ExpQueryResults/xqxq/url-schema-resolver3.xml.res (+2/-0)
test/Queries/xqxq/error-in-query.spec (+2/-0)
test/Queries/xqxq/error-in-query.xq (+21/-0)
test/Queries/xqxq/test.xml (+1/-0)
test/Queries/xqxq/url-schema-resolver3.xq (+29/-0)
To merge this branch: bzr merge lp:~zorba-coders/zorba/xqxq-memory-smash
Reviewer Review Type Date Requested Status
Juan Zacarias Approve
Chris Hillery Approve
Review via email: mp+130657@code.launchpad.net

Commit message

Fix several memory errors having to do with freeing URIMappers / URLResolvers.

To post a comment you must log in.
Revision history for this message
Chris Hillery (ceejatec) :
review: Approve
Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :
Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :

Validation queue job xqxq-memory-smash-2012-10-20T02-21-41.631Z is finished. The final status was:

All tests succeeded!

Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :

Voting does not meet specified criteria. Required: Approve > 1, Disapprove < 1, Needs Fixing < 1, Pending < 1. Got: 1 Approve, 1 Pending.

Revision history for this message
Juan Zacarias (juan457) :
review: Approve
Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :
Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :

Validation queue job xqxq-memory-smash-2012-10-22T16-48-40.826Z is finished. The final status was:

All tests succeeded!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/xqxq.xq.src/xqxq.cpp'
2--- src/xqxq.xq.src/xqxq.cpp 2012-10-19 01:22:47 +0000
3+++ src/xqxq.xq.src/xqxq.cpp 2012-10-20 02:20:24 +0000
4@@ -179,20 +179,39 @@
5 String errDescription(aErrorMessage);
6 throw USER_EXCEPTION(errQName, errDescription);
7 }
8-
9- /*******************************************************************************************
10- *******************************************************************************************/
11-
12+
13+ /*******************************************************************************************
14+ *******************************************************************************************/
15+
16+ QueryData::QueryData(XQuery_t aQuery, URIMapper *aMapper, URLResolver *aResolver)
17+ : theQuery(aQuery),
18+ theURIMapper(aMapper),
19+ theURLResolver(aResolver)
20+ {
21+ }
22+
23+ QueryData::~QueryData()
24+ {
25+ theQuery->close();
26+ delete theURIMapper;
27+ delete theURLResolver;
28+ }
29+
30+ /*******************************************************************************************
31+ *******************************************************************************************/
32+
33 QueryMap::QueryMap()
34 {
35 QueryMap::queryMap = new QueryMap_t();
36 }
37
38 bool
39- QueryMap::storeQuery(const String& aKeyName, XQuery_t aQuery)
40+ QueryMap::storeQuery(const String& aKeyName, XQuery_t aQuery,
41+ URIMapper* aMapper, URLResolver* aResolver)
42 {
43+ QueryData_t lQueryData(new QueryData(aQuery, aMapper, aResolver));
44 std::pair<QueryMap_t::iterator,bool> ret;
45- ret = queryMap->insert(std::pair<String, XQuery_t>(aKeyName, aQuery));
46+ ret = queryMap->insert(std::pair<String, QueryData_t>(aKeyName, lQueryData));
47 return ret.second;
48 }
49
50@@ -204,7 +223,7 @@
51 if(lIter == queryMap->end())
52 return NULL;
53
54- XQuery_t lQuery = lIter->second;
55+ XQuery_t lQuery = lIter->second->getQuery();
56
57 return lQuery;
58 }
59@@ -216,14 +235,27 @@
60
61 if(lIter == queryMap->end())
62 return false;
63-
64- lIter->second->close();
65
66 queryMap->erase(lIter);
67-
68 return true;
69 }
70
71+ void
72+ QueryMap::destroy() throw()
73+ {
74+ if(queryMap)
75+ {
76+ for (QueryMap_t::const_iterator lIter = queryMap->begin();
77+ lIter != queryMap->end(); ++lIter)
78+ {
79+ deleteQuery(lIter->first);
80+ }
81+ queryMap->clear();
82+ delete queryMap;
83+ }
84+ delete this;
85+ }
86+
87 /*******************************************************************************************
88 *******************************************************************************************/
89 static void streamReleaser(std::istream* aStream)
90@@ -344,7 +376,6 @@
91 {
92 DynamicContext* lDynCtx = const_cast<DynamicContext*>(aDctx);
93 StaticContext_t lSctxChild = aSctx->createChildContext();
94- StaticContext_t lMapperSctx = aSctx->createChildContext();
95
96 QueryMap* lQueryMap;
97 if(!(lQueryMap = dynamic_cast<QueryMap*>(lDynCtx->getExternalFunctionParameter("xqxqQueryMap"))))
98@@ -360,16 +391,16 @@
99 XQuery_t lQuery;
100
101 StaticContext_t ltempSctx = lZorba->createStaticContext();
102- XQXQURLResolver* lResolver = NULL;
103- XQXQURIMapper* lMapper = NULL;
104+ std::auto_ptr<XQXQURLResolver> lResolver;
105+ std::auto_ptr<XQXQURIMapper> lMapper;
106
107 if ( aArgs.size() > 2 )
108 {
109 Item lMapperFunctionItem = getItemArgument(aArgs, 2);
110 if (!lMapperFunctionItem.isNull())
111 {
112- lMapper = new XQXQURIMapper(lMapperFunctionItem, lSctxChild);
113- ltempSctx->registerURIMapper(lMapper);
114+ lMapper.reset(new XQXQURIMapper(lMapperFunctionItem, lSctxChild));
115+ ltempSctx->registerURIMapper(lMapper.get());
116 }
117 }
118
119@@ -378,8 +409,8 @@
120 Item lResolverFunctionItem = getItemArgument(aArgs, 1);
121 if (!lResolverFunctionItem.isNull())
122 {
123- lResolver = new XQXQURLResolver(lResolverFunctionItem, lSctxChild);
124- ltempSctx->registerURLResolver(lResolver);
125+ lResolver.reset(new XQXQURLResolver(lResolverFunctionItem, lSctxChild));
126+ ltempSctx->registerURLResolver(lResolver.get());
127 }
128
129 }
130@@ -390,6 +421,7 @@
131 }
132 catch (XQueryException& xe)
133 {
134+ lQuery = NULL;
135 std::ostringstream err;
136 err << "The query compiled using xqxq:prepare-main-module raised an error at"
137 << " line " << xe.source_line() << " column " << xe.source_column() << ": " << xe.what();
138@@ -399,6 +431,7 @@
139 }
140 catch (ZorbaException& e)
141 {
142+ lQuery = NULL;
143 std::ostringstream err;
144 err << "The query compiled using xqxq:prepare-main-module raised an error: "
145 << e.what();
146@@ -406,7 +439,7 @@
147 e.diagnostic().qname().ns(), e.diagnostic().qname().localname());
148 throw USER_EXCEPTION(errQName, err.str());
149 }
150-
151+
152 uuid lUUID;
153 uuid::create(&lUUID);
154
155@@ -415,13 +448,8 @@
156
157 String lStrUUID = lStream.str();
158
159- lQueryMap->storeQuery(lStrUUID, lQuery);
160+ lQueryMap->storeQuery(lStrUUID, lQuery, lMapper.release(), lResolver.release());
161
162- if (lResolver)
163- delete lResolver;
164- if (lMapper)
165- delete lMapper;
166-
167 return ItemSequence_t(new SingletonItemSequence(XQXQModule::getItemFactory()->createAnyURI(lStrUUID)));
168 }
169
170
171=== modified file 'src/xqxq.xq.src/xqxq.h'
172--- src/xqxq.xq.src/xqxq.h 2012-10-18 00:34:20 +0000
173+++ src/xqxq.xq.src/xqxq.h 2012-10-20 02:20:24 +0000
174@@ -49,35 +49,39 @@
175
176 };
177
178-
179- class QueryMap : public ExternalFunctionParameter{
180- private:
181- typedef std::map<String, XQuery_t> QueryMap_t;
182+ /**
183+ * @brief Bag class for objects associated with a prepared query
184+ */
185+ class QueryData : public SmartObject
186+ {
187+ private:
188+ XQuery_t theQuery;
189+ URIMapper* theURIMapper;
190+ URLResolver* theURLResolver;
191+
192+ public:
193+ QueryData(XQuery_t aQuery, URIMapper* aMapper, URLResolver* aResolver);
194+ virtual ~QueryData();
195+ XQuery_t getQuery() { return theQuery; }
196+ };
197+ typedef SmartPtr<QueryData> QueryData_t;
198+
199+ class QueryMap : public ExternalFunctionParameter
200+ {
201+ private:
202+ typedef std::map<String, QueryData_t> QueryMap_t;
203 QueryMap_t* queryMap;
204
205 public:
206 QueryMap();
207 bool
208- storeQuery(const String&, XQuery_t);
209+ storeQuery(const String&, XQuery_t, URIMapper*, URLResolver*);
210 XQuery_t
211 getQuery(const String&);
212 bool
213 deleteQuery(const String&);
214 virtual void
215- destroy() throw()
216- {
217- if(queryMap)
218- {
219- for (QueryMap_t::const_iterator lIter = queryMap->begin();
220- lIter != queryMap->end(); ++lIter)
221- {
222- lIter->second->close();
223- }
224- queryMap->clear();
225- delete queryMap;
226- }
227- delete this;
228- };
229+ destroy() throw();
230 };
231
232 class XQXQFunction : public ContextualExternalFunction
233
234=== added file 'test/ExpQueryResults/xqxq/url-schema-resolver3.xml.res'
235--- test/ExpQueryResults/xqxq/url-schema-resolver3.xml.res 1970-01-01 00:00:00 +0000
236+++ test/ExpQueryResults/xqxq/url-schema-resolver3.xml.res 2012-10-20 02:20:24 +0000
237@@ -0,0 +1,2 @@
238+<?xml version="1.0" encoding="UTF-8"?>
239+<test:test xmlns:test="http://test"><test:subtest>a</test:subtest><test:subtest2>a</test:subtest2></test:test>
240
241=== added file 'test/Queries/xqxq/error-in-query.spec'
242--- test/Queries/xqxq/error-in-query.spec 1970-01-01 00:00:00 +0000
243+++ test/Queries/xqxq/error-in-query.spec 2012-10-20 02:20:24 +0000
244@@ -0,0 +1,2 @@
245+Error: http://www.w3.org/2005/xqt-errors:XQST0033
246+
247
248=== added file 'test/Queries/xqxq/error-in-query.xq'
249--- test/Queries/xqxq/error-in-query.xq 1970-01-01 00:00:00 +0000
250+++ test/Queries/xqxq/error-in-query.xq 2012-10-20 02:20:24 +0000
251@@ -0,0 +1,21 @@
252+import module namespace xqxq = 'http://www.zorba-xquery.com/modules/xqxq';
253+
254+declare namespace op = 'http://www.zorba-xquery.com/options/features';
255+declare namespace f = 'http://www.zorba-xquery.com/features';
256+declare option op:enable 'f:hof';
257+
258+declare function local:url-resolver($namespace as xs:string, $entity as xs:string) {
259+switch($entity)
260+case 'schema'
261+ return switch($namespace)
262+ case 'http://www.w3.org/XQueryTest' return doc('/tmp/atomic.xsd')
263+ default return ()
264+default return ()
265+};
266+
267+variable $queryID := xqxq:prepare-main-module(
268+ "declare namespace foo='http://test';
269+ declare namespace foo='http://test';
270+ 1",
271+ local:url-resolver#2, ());
272+xqxq:evaluate($queryID)
273
274=== added file 'test/Queries/xqxq/test.xml'
275--- test/Queries/xqxq/test.xml 1970-01-01 00:00:00 +0000
276+++ test/Queries/xqxq/test.xml 2012-10-20 02:20:24 +0000
277@@ -0,0 +1,1 @@
278+<test:test xmlns:test="http://test"><test:subtest>a</test:subtest><test:subtest2>a</test:subtest2></test:test>
279\ No newline at end of file
280
281=== added file 'test/Queries/xqxq/url-schema-resolver3.xq'
282--- test/Queries/xqxq/url-schema-resolver3.xq 1970-01-01 00:00:00 +0000
283+++ test/Queries/xqxq/url-schema-resolver3.xq 2012-10-20 02:20:24 +0000
284@@ -0,0 +1,29 @@
285+import module namespace xqxq = 'http://www.zorba-xquery.com/modules/xqxq';
286+
287+declare namespace resolver = 'http://www.zorba-xquery.com/modules/xqxq/url-resolver';
288+
289+declare namespace op = "http://www.zorba-xquery.com/options/features";
290+declare namespace f = "http://www.zorba-xquery.com/features";
291+declare option op:enable "f:hof";
292+
293+declare function resolver:url-resolver($namespace as xs:string, $entity as xs:string) {
294+ if($namespace = 'http://test' and $entity = 'schema')
295+ then
296+ doc('test.xsd')
297+ else
298+ ()
299+};
300+
301+variable $contextQueryID := xqxq:prepare-main-module(
302+ "import schema namespace test = 'http://test';
303+ declare variable $cwd as xs:anyURI external;
304+ validate { doc(resolve-uri('test.xml', $cwd)) }",
305+ resolver:url-resolver#2, ());
306+xqxq:bind-variable($contextQueryID, fn:QName("", "cwd"), resolve-uri("."));
307+variable $contextItem := xqxq:evaluate($contextQueryID);
308+
309+variable $queryID := xqxq:prepare-main-module(
310+ "import schema namespace test = 'http://test'; //*:test",
311+ resolver:url-resolver#2, ());
312+xqxq:bind-context-item($queryID, $contextItem);
313+xqxq:evaluate($queryID)

Subscribers

People subscribed via source and target branches

to all changes: