[vtkusers] [advice needed] - c++ memory management

hampycalc i.am.tom.hampshire at googlemail.com
Fri Dec 18 06:20:41 EST 2009


Hi all,

I am hoping to get a little advice for good coding practice in VTK. I have
been creating some applications, but not been doing a very good job with my
memory management. Could someone advise as to how to code the following
situation:
Simply, I have a FileIO class, and within a method taking a filename to a
.vtk polydata file, and returning a vtkPolyData pointer. As you can see, I
have not been very clever with my memory management  (please see code
below).... however, I have been banging my head against the monitor trying
to find the most sensible way to do this.
Presumably incrementing the reference count of the object returned by
reader->GetOutput() (assigned to data) is going to have no effect when the
reader->Delete() is called, so data->Register(NULL) or creating a
smartpointer to data is not going to have any effect. Should I:
- create a new vtkPolyData object and do a shallow or deep copy of
reader->GetOutput() (I don't really understand how the references are
effected in either of these cases)
- is there some other way I can delete the reader object and still return
the data object?
- is there something I have missed that you can do with smartpointers or
register/unregister methods?
It pains me to do so, but at the moment I will have to leave my methods as
they are; I would like to however, 'start as I mean to go on' with good
coding practice.

vtkPolyData * FileIO::loadPolyData(const char *filename, int &error){
	vtkErrorCallback* observeErrorWarning = vtkErrorCallback::New(); 
//vtkCommand subclass
	vtkPolyDataReader *reader = vtkPolyDataReader::New();
	vtkPolyData * data;
	error = NOTERROR;

	// Add observer to reader object. Observe errors
	reader->AddObserver(vtkCommand::ErrorEvent, observeErrorWarning);

	reader->SetFileName(filename);
	reader->Update();
	data = reader->GetOutput();

	if (observeErrorWarning->isError()){
		std::cerr << "Error: file [" << filename << "] could not be opened" <<
std::endl;
		error = LOADERROR;
	}

	return data;
}
-- 
View this message in context: http://old.nabble.com/-advice-needed----c%2B%2B-memory-management-tp26842088p26842088.html
Sent from the VTK - Users mailing list archive at Nabble.com.




More information about the vtkusers mailing list