ofXml in child class

Hi all,

If I have a child class I can not use ofXml in this class.
The error I get is (when opening the App, compiling is OK)

Unhandled exception at 0x00CA28BD in XXX.exe: 0xC0000005: Access violation reading location 0x00000000.

Line of code mentioned is:

ofXml::ofXml(const ofXml & rhs){
    document = new Poco::XML::Document();
x   Poco::XML::Node *n = document->importNode(rhs.getPocoDocument()->documentElement(), true);
    document->appendChild(n);
    
    element = document->documentElement();
}

Here’s the setting. (I do nothing but declare an ofXml in the header file of the class - that’s the culprit):

ofApp.h:

#pragma once
#include "ofMain.h"
#include "myClass.h"

class ofApp : public ofBaseApp{
	public:
		void setup();
		void update();
		void draw();

		ofXml settingsXML;
		vector <myClass> content;
};

ofApp.cpp:

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
	if (settingsXML.load("settings.xml")) {
	}

	content.assign(4, myClass());
	for (int i = 0; i < content.size();i++) {
		content[i].init();
	}
}

//--------------------------------------------------------------
void ofApp::update(){
}

//--------------------------------------------------------------
void ofApp::draw(){
}

myClass.h:

#pragma once
#include "ofMain.h"

class ofApp;

class myClass : public ofBaseApp {
public:
	myClass();
	void init();

	ofXml classXML;
};

myClass.cpp:

#include "myClass.h"

myClass::myClass() {
}

void myClass::init() {
}

is it possible to use ofXml here? If someone can explain my error, I’d be happy!

thanks & have a good day
oe

hi. there shouldn’t be any problem using an ofXml instance where ever you want to. Although in myClass you are extending ofBaseApp, which I don’t see any need to do so. Also there seems to be no need to have class ofApp;
Also, use content.resize(4) instead of content.assign(4, myClass());

hope it helps.
best

1 Like

HI roymacdonald,

thank you very much! ‘resize(4)’ made the difference.

You made my day!
oe