Confused, ScopedPointers breaking while parsing XML


#1

I’ve been trying to convert my pointers to Scoped Pointers, but all I seem to do is break my code :frowning: I’m sure this is due to my amazing lack of understanding when it comes to these. My First attempt at using ScopedPointers seems weird. I wanted to parse an XML document so I wrote the following. If I use the code commented out, then this breaks. It sometimes jumps me to juce_XmlElement.cpp line 148 XmlElement* const nextChild = child->nextElement; in the ~XmlElement()

I’m pretty new to C++ so thank you in advance for any help, and sorry if this stuff is pretty obvious.

bool DataSet::LoadDataSet(const String& fileName)
{	
	File datafile(fileName); //open the data file
		
	if(datafile.hasFileExtension(T(".xml")) )
	{

//****** I tried to make this ScopedPointer <XmlElement> mainXmlElement(XmlDocument::parse(datafile) ); but it keeps crashing? 
//****** I think it gets hung up when asking for the "childElementVariableName = childElementVariableName->getNextElement()"
//****** inside the "forEachXmlChildElement" macro

		XmlElement *mainXmlElement = XmlDocument::parse(datafile);
		
		//count the number of Observations
		numberOfObservations = mainXmlElement->getNumChildElements();
		
		//count the number of Descriptors
		numberOfDescriptors = mainXmlElement->getFirstChildElement()->getNumAttributes();
		
		//now save the actual values of the endpoints and the descriptors
		ScopedPointer<Observation> currentObservation;
		
		long i;
		
		forEachXmlChildElement (*mainXmlElement, child)
		{
			//make a new observation to hold the data
			currentObservation = new Observation;
			currentObservation->SetIndex(i++);
			
			//unpack and save the Descriptor values into the current Observation
			for(int j = 0; j < numberOfDescriptors; j++)
			{
				currentObservation->AddDescriptor(child->getDoubleAttribute(String(j)) );
			}
			
			//add current Observation to data collection
			data.add(currentObservation.release());
		}
		deleteAndZero(mainXmlElement);
	}		
	
	return true;
}

My second question is I’m trying to create a pointer to a Time object (see code below) I would like to do something along the lines of the commented code, but I can only seem to compile this way?

//******* I would like to do this but it won't work??
//   ScopedPointer <Time> currentTime = new Time(Time::getCurrentTime());
//******* This seems to work fine though? What am I missing/not understanding?
//   Time *currentTime = new Time(Time::getCurrentTime());

   File SOMlog("../../../../SOMlog.txt");

	ScopedPointer <Time> currentTime;
	currentTime = new Time(Time::getCurrentTime());
	
	SOMlog.appendText(String(currentTime->getMonth()+1) );
	SOMlog.appendText("/");
	SOMlog.appendText(String(currentTime->getDayOfMonth()) );
	SOMlog.appendText("/");
	SOMlog.appendText(String(currentTime->getYear()) );
	SOMlog.appendText("    ");
	SOMlog.appendText(String(currentTime->getHours()) );
	SOMlog.appendText(":");
	SOMlog.appendText(String(currentTime->getMinutes()) );
	SOMlog.appendText(":");
	SOMlog.appendText(String(currentTime->getSeconds()) );
	SOMlog.appendText("\n");
	SOMlog.appendText("\n");

Owen


#2

edited post


#3

What’s the compiler error?


#4

There’s no reason at all to ever create a Time object on the heap. Copy it around using copy-by-value semantics.


#5

Thanks Jules, I’ve changed the code that calls the Time object. But I’m still confused about the ScopedPointer and the XmlElement.

Am I misunderstanding the correct use of ScopedPointers?

[code]bool DataSet::LoadDataSet(const String& fileName)
{
File datafile(fileName); //open the data file

if(datafile.hasFileExtension(T(".xml")) )
{

//****** I tried to make this ScopedPointer mainXmlElement(XmlDocument::parse(datafile) ); but it keeps crashing?
//****** I think it gets hung up when asking for the “childElementVariableName = childElementVariableName->getNextElement()”
//****** inside the “forEachXmlChildElement” macro

  XmlElement *mainXmlElement = XmlDocument::parse(datafile);
  
  //count the number of Observations
  numberOfObservations = mainXmlElement->getNumChildElements();
  
  //count the number of Descriptors
  numberOfDescriptors = mainXmlElement->getFirstChildElement()->getNumAttributes();
  
  //now save the actual values of the endpoints and the descriptors
  ScopedPointer<Observation> currentObservation;
  
  long i;
  
  forEachXmlChildElement (*mainXmlElement, child)
  {
     //make a new observation to hold the data
     currentObservation = new Observation;
     currentObservation->SetIndex(i++);
     
     //unpack and save the Descriptor values into the current Observation
     for(int j = 0; j < numberOfDescriptors; j++)
     {
        currentObservation->AddDescriptor(child->getDoubleAttribute(String(j)) );
     }
     
     //add current Observation to data collection
     data.add(currentObservation.release());
  }
  deleteAndZero(mainXmlElement);

}

return true;
}[/code]


#6

Can’t see any obvious problems with that… When you put your main element inside a ScopedPointer, did you remember to remove the deleteAndZero at the end?


#7

Weird, I swear I didn’t leave that in… but obviously I must have. It compiles and runs just fine now. Thanks for the help.