Project

Profile

Help

Revision 146cfacb

Added by O'Neil Delpratt 5 months ago

Saxon/C memory management patches

View differences:

src/main/c/Saxon.C.API/XPathProcessor.cpp
4 4
#include "XdmNode.h"
5 5
#include "XdmAtomicValue.h"
6 6
#include "XdmFunctionItem.h"
7
#include "XdmMap.h"
8
#include "XdmArray.h"
7 9

  
8 10
XPathProcessor::XPathProcessor() {
9 11
	SaxonProcessor *p = new SaxonProcessor(false);
......
101 103
		jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
102 104
		jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
103 105
		jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
106
        jclass mapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
107
        jclass arrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmArray");
104 108

  
105 109
		XdmValue * value = new XdmValue();
106 110
		XdmItem * xdmItem = nullptr;
107 111
		for (int p=0; p < sizex; ++p) 
108 112
		{
109 113
			jobject resulti = SaxonProcessor::sxn_environ->env->GetObjectArrayElement(results, p);
110
			//value->addUnderlyingValue(resulti);
111 114

  
112 115
			if(SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, atomicValueClass)           == JNI_TRUE) {
113 116
				xdmItem = new XdmAtomicValue(resulti);
......
117 120

  
118 121
			} else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, functionItemClass)           == JNI_TRUE) {
119 122
				xdmItem =  new XdmFunctionItem(resulti);
120
			}
121
			if(sizex == 1) {
122
				value->decrementRefCount();
123
			} else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, mapClass)           == JNI_TRUE) {
124
                xdmItem =  new XdmMap(resulti);
125
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, arrayClass)           == JNI_TRUE) {
126
                xdmItem =  new XdmArray(resulti);
127
            }
128
            if(sizex == 1) {
123 129
				delete value;
124 130
				SaxonProcessor::sxn_environ->env->DeleteLocalRef(results);
125 131
				return xdmItem;		
......
380 386
            it = parameters.find(skey);
381 387
            if (it != parameters.end()) {
382 388
                XdmValue * valuei = it->second;
383
                valuei->decrementRefCount();
384
                if(valuei != nullptr && valuei->getRefCount() < 1){
385
                    delete value;
389
                if(valuei != nullptr) {
390
                    valuei->decrementRefCount();
391
                    if (valuei->getRefCount() < 1) {
392
                        delete value;
393
                    }
394
                    parameters.erase(skey);
395
                    parameters[skey] = value;
386 396
                }
387
                parameters.erase(skey);
388
                parameters[skey] = value;
389 397
            }
390 398
		}
391 399
	}
......
413 421
}
414 422

  
415 423
void XPathProcessor::clearParameters(bool delVal) {
416
	if(delVal){
417
       		for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
418
			XdmValue * value = itr->second;
419
			value->decrementRefCount();
424
	if(delVal) {
425
        for (std::map<std::string, XdmValue *>::iterator itr = parameters.begin(); itr != parameters.end(); itr++) {
426
            XdmValue *value = itr->second;
427
            if (value != nullptr) {
428
                value->decrementRefCount();
420 429
#ifdef DEBUG
421
			std::cerr<<"XPathProc.clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
430
                std::cerr<<"XPathProc.clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
422 431
#endif
423
			if(value != nullptr && value->getRefCount() < 1){
424
	        		delete value;
425
			}
426
        	}
427
		parameters.clear();
428
	}
432
                if (value->getRefCount() < 1) {
433
                    delete value;
434
                }
435
            }
436
        }
437
    }
438
	parameters.clear();
439

  
429 440
}
430 441

  
431 442
void XPathProcessor::clearProperties() {

Also available in: Unified diff