Using SimXMLDocument in C++
by Robert Fritzen · in Torque 3D Professional · 11/21/2012 (9:14 am) · 5 replies
I'm having an issue with my code, I'm trying to read and write XML documents from inside the engine for my latest project.
The problem I'm getting is that the SimXMLDocument is causing access violation errors when I attempt to load my XML file, and according to Platform::isFile(), the file is perfectly there and has read/write access.
* Yes I have done a non-pointer reference to SimXMLDocument.
Any help would be greatly appreciated, as I need to get this working ASAP.
*EDIT*
This is a major failure in the source code.. There is no constructor call to the onAdd() call made by SimXMLDocument. Addin xml->onAdd(); right after creating it has fixed my issue.
The problem I'm getting is that the SimXMLDocument is causing access violation errors when I attempt to load my XML file, and according to Platform::isFile(), the file is perfectly there and has read/write access.
void survivalControl::loadInfo(const char *file) {
SimXMLDocument *xml = new SimXMLDocument();
Con::printf("LoadInfo");
xml->loadFile(file); // <-- Access Violation
Con::printf("File Loaded");
Con::printf("Done...");
delete xml;
}* Yes I have done a non-pointer reference to SimXMLDocument.
Any help would be greatly appreciated, as I need to get this working ASAP.
*EDIT*
This is a major failure in the source code.. There is no constructor call to the onAdd() call made by SimXMLDocument. Addin xml->onAdd(); right after creating it has fixed my issue.
About the author
Illinois Grad. Retired T3D Developer / Pack Dev.
#3
Now as for why SimXMLDocument class even cares whether or not it is successfully placed into the sim in order to initialize a valid tinyxml document reference I don't know. Seems to me the correct fix would be to move the m_qDocument = new TiXmlDocument(); into the constructor and delete(m_qDocument) into the destructor instead of them being in onAdd() and onRemove().
Now that I think about it you should probably be using tinyxml directly in your C++ code anyway as SimXMLDocument was only designed to be used within TorqueScript, and creating any sim objects manually is going to cause issues when you're not careful about proper object initialization. Since after all they're meant to be initialized by the sim/scripting subsystem, not via C++. Just a word of caution. ;)
11/23/2012 (11:17 pm)
Sim object's constructor isn't suppose to call onAdd() as that is called upon the object is being registered into the sim subsystem and then when it is being removed from the sim onRemove() is called. new operator in TorqueScript creates an object and then SimObject::registerObject() is called on it via __create() macro DEFINE_CREATE_METHOD.Now as for why SimXMLDocument class even cares whether or not it is successfully placed into the sim in order to initialize a valid tinyxml document reference I don't know. Seems to me the correct fix would be to move the m_qDocument = new TiXmlDocument(); into the constructor and delete(m_qDocument) into the destructor instead of them being in onAdd() and onRemove().
Now that I think about it you should probably be using tinyxml directly in your C++ code anyway as SimXMLDocument was only designed to be used within TorqueScript, and creating any sim objects manually is going to cause issues when you're not careful about proper object initialization. Since after all they're meant to be initialized by the sim/scripting subsystem, not via C++. Just a word of caution. ;)
#4
I only went with the usage of SimXMLDocument because the pre-defined methods in the class had everything I needed to successfully create and use my data storage system. I understand that they are meant to be defined in TS, however, I would think that they should also be readily available in C++, which shouldn't be too hard to accomplish in this case anyways (the fix was already mentioned).
11/24/2012 (9:11 am)
I agree for the fix for that issue. I only went with the usage of SimXMLDocument because the pre-defined methods in the class had everything I needed to successfully create and use my data storage system. I understand that they are meant to be defined in TS, however, I would think that they should also be readily available in C++, which shouldn't be too hard to accomplish in this case anyways (the fix was already mentioned).
#5
I wrote this in the ticket on GitHub but I thought I would repeat it here.
A SimObject's onAdd() is not supposed to be called from its constructor. That would cause issues with how T3D operates. The proper way to construct a SimXMLDocument is:
And then when you're done with it, you should be able to:
- Dave
11/28/2012 (3:28 pm)
Greetings!I wrote this in the ticket on GitHub but I thought I would repeat it here.
A SimObject's onAdd() is not supposed to be called from its constructor. That would cause issues with how T3D operates. The proper way to construct a SimXMLDocument is:
SimXMLDocument* xml = new SimXMLDocument; xml->registerObject();
And then when you're done with it, you should be able to:
xml->destroySelf();
- Dave
Associate David Montgomery-Blake
David MontgomeryBlake