Project

Profile

Help

Revision 55b80284

Added by O'Neil Delpratt 5 months ago

Saxon/C added memory debugging and bug fixes

View differences:

src/main/c/Saxon.C.API/SaxonProcessor.cpp
14 14
#include "XdmArray.h"
15 15
#include "XdmAtomicValue.h"
16 16

  
17

  
17 18
//#define DEBUG
18 19
#ifdef DEBUG
19 20
#include <signal.h>
......
22 23
#include <stdio.h>
23 24

  
24 25

  
26
#if defined MEM_DEBUG
27

  
28
void* newImpl(std::size_t sz,char const* file, int line){
29
    static int counter{};
30
    void* ptr= std::malloc(sz);
31
    std::cerr << "Saxon/C Mem test: "<<file << ": " << line << " " <<  ptr << std::endl;
32
    myAlloc.push_back(ptr);
33
    return ptr;
34
}
35

  
36
void* operator new(std::size_t sz,char const* file, int line){
37
return newImpl(sz,file,line);
38
}
39

  
40
void* operator new [](std::size_t sz,char const* file, int line){
41
return newImpl(sz,file,line);
42
}
43

  
44
void operator delete(void* ptr) noexcept{
45
    auto ind= std::distance(myAlloc.begin(),std::find(myAlloc.begin(), myAlloc.end(),ptr));
46
    myAlloc[ind]= nullptr;
47
    std::free(ptr);
48
}
49

  
50
#define new new(__FILE__, __LINE__)
51

  
52
void SaxonProcessor::getInfo(){
53

  
54
    std::cout << std::endl;
55

  
56
    std::cout << "Allocation: " << std::endl;
57
    for (auto i: myAlloc){
58
        if (i != nullptr ) std::cout << " " << i << std::endl;
59
    }
60

  
61
    std::cout << std::endl;
62

  
63
}
64

  
65
#endif
66

  
25 67
//jobject cpp;
26 68
const char *failure;
27 69
sxnc_environment *SaxonProcessor::sxn_environ = 0;
......
37 79
    return message;
38 80
}
39 81

  
82

  
83

  
40 84
SaxonApiException *SaxonProcessor::checkAndCreateException(jclass cppClass) {
41 85
    if (SaxonProcessor::sxn_environ->env->ExceptionCheck()) {
42 86
        SaxonApiException *exception = checkForExceptionCPP(SaxonProcessor::sxn_environ->env, cppClass, nullptr);
......
62 106
        SaxonProcessor::sxn_environ->env->ExceptionClear();
63 107
    }
64 108
    if (exception != nullptr) {
109
        std::cerr<< "saxonProc exception= "<< exception <<std::endl;
65 110
        delete exception;
111
        exception = nullptr;
66 112
    }
67 113

  
68 114
}
......
79 125

  
80 126
void SaxonProcessor::createException(const char * message) {
81 127

  
82
    if(exceptionOccurred()) {
83
        delete exception;
84
        SaxonProcessor::sxn_environ->env->ExceptionClear();
85
    }
128
    exceptionClear();
86 129

  
87 130
    if(message == nullptr) {
88 131
        exception = checkAndCreateException(saxonCAPIClass);
......
302 345
    }
303 346
    if (versionStr != nullptr) {
304 347
        delete versionStr;
348
        versionStr = nullptr;
305 349
    }
306 350
    exceptionClear();
307 351
}
......
609 653
        int verLen = strlen(tempVersionStr) + 22 + strlen(CVERSION) + 1;
610 654
        versionStr = new char[verLen];
611 655
        snprintf(versionStr, verLen, "Saxon/C %s %s %s", CVERSION, "running with", tempVersionStr);
612
        delete tempVersionStr;
656
        SaxonProcessor::sxn_environ->env->ReleaseStringUTFChars(jstr, tempVersionStr);
657

  
613 658

  
614 659
    }
615 660

  
......
691 736
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
692 737
                                                                                        source));
693 738
    if (xdmNodei) {
694
        XdmNode *value = new XdmNode(xdmNodei);
739
        XdmNode *value = nullptr;
740
        value = new XdmNode(xdmNodei);
741
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(xdmNodei);
695 742
        return value;
696 743
    } else if (exceptionOccurred()) {
697 744
        createException();
......
811 858
        //std::cerr<<"SaxonProc: JVM finalized calling !"<<std::endl;
812 859
        finalizeJavaRT(SaxonProcessor::sxn_environ->jvm);
813 860

  
814
        //delete SaxonProcessor::sxn_environ;
861
        delete SaxonProcessor::sxn_environ;
815 862
        /*clearParameters();
816 863
        clearProperties();*/
817 864
    } else {

Also available in: Unified diff