Will Sappington wrote:
Hi Dave,

I think I may have hit a dead end with this. When I define XalanSourceTreeParserLiaison _parserLiaison; as member data, the program
crashes with an assertion during construction of the Profile class.  The
first line of the constructor doesn't even execute so the assertion is
occurring in the construction of the member classes.  I stepped into it
and found that it occurs during construction of the parserLiaison when
it calls XalanMemMgrs::getDefaultXercesMemMgr() which is what
XALAN_DEFAULT_MEMMGR is #defined as.  After that call there is an assert
(ptr != 0) and that is where the crash occurs.  I'm assuming this is
because neither of the ::initialize() methods have been called.  Of
course there's no opportunity to do so prior to member data construction
so it would appear I'm screwed.  Is there a way to instantiate a memory
manager such that the getDefaultXercesMemMgr() call would be able to
find it?

If you're going to do Xerces-C and Xalan-C initialization in the constructor of your class, you'll need to create a simple class that initializes Xerces-C and Xalan-C in the right order, and make that class a member of your class, ensuring that it appears before any other Xerces-C or Xalan-C class in the class definition. Here's an example, in pseudo-code:

class XercesXalanInit
{
public:

    XercesXalanInit()
    {
        if (++s_initCount == 1)
        {
            PlatformUtils::Initialize();
            XalanTransformer::initialize();
        }
    }

    ~XercesXalanInit()
    {
        if (--s_initCount == 0)
        {
            XalanTransformer::terminate();
            PlatformUtils::Terminate();
        }
    }

    static unsigned long   s_initCount;
};

XercesXalanInit::s_initCount = 0;

class myClass
{
public:

    myClass() :
        m_init(),
        m_liason()
    {
    }

    // all my public member functions here...

private:

    XercesXalanInit    m_init;

    XalanSourceTreeParserLiaison   m_liason;
};


Ultimately, it's much better if you initialize Xerces-C and Xalan-C _once_, when your application starts, and not when each instance is created. Also, you'll notice that I put some counting in the XercesXalanInit init class, because you should only call them each once. Finally, if your application will be multi-threaded, you will either need to initialize the libraries once, before any threads start, or make the counting in XercesXalanInit thread-safe.

Dave

Reply via email to