سؤال

I've got an object to delete:

XMLDoc = new TXMLDocument(NULL);  

but I have no idea how to do that... it's not working:

delete XMLDoc;
XMLDoc->free();

When I try just delete object the program crashes.

So it's a piece of my code. I've got a class:

class C_XMLhandler{
    private:
        TXMLDocument *XMLDoc;
...
...
    public:
        void loadXMLfile(String name);
...
...
};

and example declaration of method:
bool C_XMLhandler::loadXMLfile(String name)
{
        XMLDoc = new TXMLDocument(NULL);
        Box=GetMagicBox(XMLDoc); //it's XML parser method, and it's doesen't matter
        delete XMLDoc; //when i reach this line the program scrashes
}
هل كانت مفيدة؟

المحلول

http://docwiki.embarcadero.com/Libraries/XE6/en/Xml.XMLDoc.TXMLDocument.Destroy

The object is deleted by the component it is connected to new (this) otherwise not really sure, I read somewhere that it is auto deleted. (hmmm)

نصائح أخرى

It's because you delete the object before calling the free method in the now deleted object.

When you do delete XMLDoc the memory allocated for XMLDoc is marked as free, and the object destructor is executed. Now when using the pointer in calling XMLDoc->free() you are accessing memory that is no longer allocated, and you experience undefined behavior, which some times leads to a crash.

You should first check if the free method is not called inside the object destructor, and if it isn't then do it the other way around. If free is called in the destructor, you don't need to call it at all.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top