From b9db5886a4d8e50a6940d7ceea622de32cfb230a Mon Sep 17 00:00:00 2001 From: srit Date: Fri, 25 Mar 2011 13:39:53 +0000 Subject: [PATCH] Attempt to remove some leaks. DicomRT_Contour, DicomRT_ROI and DicomRT_StructureSet are now itk::LightObjects with private constructors --- common/clitkDicomRT_Contour.h | 54 ++++++------ common/clitkDicomRT_ROI.cxx | 3 +- common/clitkDicomRT_ROI.h | 100 ++++++++++++----------- common/clitkDicomRT_StructureSet.cxx | 9 +- common/clitkDicomRT_StructureSet.h | 68 +++++++-------- tools/clitkDicomRTStruct2BinaryImage.cxx | 18 ++-- vv/vvROIActor.h | 2 +- vv/vvToolStructureSetManager.cxx | 50 +++++------- vv/vvToolStructureSetManager.h | 10 +-- 9 files changed, 158 insertions(+), 156 deletions(-) diff --git a/common/clitkDicomRT_Contour.h b/common/clitkDicomRT_Contour.h index 940f25a..8596702 100644 --- a/common/clitkDicomRT_Contour.h +++ b/common/clitkDicomRT_Contour.h @@ -29,31 +29,35 @@ namespace clitk { - //-------------------------------------------------------------------- - class DicomRT_Contour { - - public: - DicomRT_Contour(); - ~DicomRT_Contour(); - - void Print(std::ostream & os = std::cout) const; - bool Read(gdcm::SQItem * item); - vtkPolyData * GetMesh(); - vtkPoints * GetPoints() {return mData;} - double GetZ() const {return mZ;} - - protected: - void ComputeMesh(); - unsigned int mNbOfPoints; - std::string mType; - vtkPoints * mData; - vtkPolyData * mMesh; - bool mMeshIsUpToDate; - ///Z location of the contour - double mZ; - - }; - //-------------------------------------------------------------------- +//-------------------------------------------------------------------- +class DicomRT_Contour : public itk::LightObject{ + +public: + typedef DicomRT_Contour Self; + typedef itk::SmartPointer Pointer; + itkNewMacro(Self); + + void Print(std::ostream & os = std::cout) const; + bool Read(gdcm::SQItem * item); + vtkPolyData * GetMesh(); + vtkPoints * GetPoints() {return mData;} + double GetZ() const {return mZ;} + +protected: + void ComputeMesh(); + unsigned int mNbOfPoints; + std::string mType; + vtkPoints * mData; + vtkPolyData * mMesh; + bool mMeshIsUpToDate; + ///Z location of the contour + double mZ; + +private: + DicomRT_Contour(); + ~DicomRT_Contour(); +}; +//-------------------------------------------------------------------- diff --git a/common/clitkDicomRT_ROI.cxx b/common/clitkDicomRT_ROI.cxx index 6bb2e25..6096ddc 100644 --- a/common/clitkDicomRT_ROI.cxx +++ b/common/clitkDicomRT_ROI.cxx @@ -39,7 +39,6 @@ clitk::DicomRT_ROI::DicomRT_ROI() //-------------------------------------------------------------------- clitk::DicomRT_ROI::~DicomRT_ROI() { - mImage->Delete(); } //-------------------------------------------------------------------- @@ -152,7 +151,7 @@ void clitk::DicomRT_ROI::Read(std::map & rois, gdcm::SQItem * bool delta_computed=false; double last_z=0; for(gdcm::SQItem* j=contours->GetFirstSQItem(); j!=0; j=contours->GetNextSQItem()) { - DicomRT_Contour * c = new DicomRT_Contour; + DicomRT_Contour::Pointer c = DicomRT_Contour::New(); bool b = c->Read(j); if (b) { mListOfContours.push_back(c); diff --git a/common/clitkDicomRT_ROI.h b/common/clitkDicomRT_ROI.h index 7fde1bf..d69ec1e 100644 --- a/common/clitkDicomRT_ROI.h +++ b/common/clitkDicomRT_ROI.h @@ -25,57 +25,63 @@ namespace clitk { - //-------------------------------------------------------------------- - class DicomRT_ROI { - - public: - DicomRT_ROI(); - ~DicomRT_ROI(); +//-------------------------------------------------------------------- +class DicomRT_ROI : public itk::LightObject +{ + +public: + typedef DicomRT_ROI Self; + typedef itk::SmartPointer Pointer; + itkNewMacro(Self); - void Print(std::ostream & os = std::cout) const; - void Read(std::map & rois, gdcm::SQItem * item); - void SetFromBinaryImage(vvImage * image, int n, - std::string name, - std::vector color, - std::string filename); + void Print(std::ostream & os = std::cout) const; + void Read(std::map & rois, gdcm::SQItem * item); + void SetFromBinaryImage(vvImage * image, int n, + std::string name, + std::vector color, + std::string filename); - int GetROINumber() const; - const std::string & GetName() const; - const std::string & GetFilename() const; - const std::vector & GetDisplayColor() const; - vtkPolyData * GetMesh(); - vvImage * GetImage() const; + int GetROINumber() const; + const std::string & GetName() const; + const std::string & GetFilename() const; + const std::vector & GetDisplayColor() const; + vtkPolyData * GetMesh(); + vvImage * GetImage() const; - void SetDisplayColor(double r, double v, double b); - std::vector & GetDisplayColor() { return mColor; } - - double GetBackgroundValueLabelImage() const; - void SetBackgroundValueLabelImage(double bg); - - double GetForegroundValueLabelImage() const; - void SetForegroundValueLabelImage(double bg); - - void SetImage(vvImage * im); - DicomRT_Contour* GetContour(int n); + void SetDisplayColor(double r, double v, double b); + std::vector & GetDisplayColor() { return mColor; } + + double GetBackgroundValueLabelImage() const; + void SetBackgroundValueLabelImage(double bg); + + double GetForegroundValueLabelImage() const; + void SetForegroundValueLabelImage(double bg); + + void SetImage(vvImage * im); + DicomRT_Contour* GetContour(int n); - double GetContourSpacing() const {return mZDelta;} - - protected: - void ComputeMesh(); - std::string mName; - std::string mFilename; - int mNumber; - std::vector mColor; - std::vector mListOfContours; - vtkPolyData * mMesh; - bool mMeshIsUpToDate; - vvImage * mImage; - double mBackgroundValue; - double mForegroundValue; - ///Spacing between two contours - double mZDelta; - }; - //-------------------------------------------------------------------- + double GetContourSpacing() const {return mZDelta;} + +protected: + void ComputeMesh(); + std::string mName; + std::string mFilename; + int mNumber; + std::vector mColor; + std::vector mListOfContours; + vtkPolyData * mMesh; + bool mMeshIsUpToDate; + vvImage::Pointer mImage; + double mBackgroundValue; + double mForegroundValue; + ///Spacing between two contours + double mZDelta; + +private: + DicomRT_ROI(); + ~DicomRT_ROI(); +}; +//-------------------------------------------------------------------- } // end namespace clitk #endif // CLITKDICOMRT_ROI_H diff --git a/common/clitkDicomRT_StructureSet.cxx b/common/clitkDicomRT_StructureSet.cxx index 41cddc2..9d128b8 100644 --- a/common/clitkDicomRT_StructureSet.cxx +++ b/common/clitkDicomRT_StructureSet.cxx @@ -37,9 +37,6 @@ clitk::DicomRT_StructureSet::DicomRT_StructureSet() //-------------------------------------------------------------------- clitk::DicomRT_StructureSet::~DicomRT_StructureSet() { - for(uint i=0; i & clitk::DicomRT_StructureSet::GetListOfROI() const +const std::vector & clitk::DicomRT_StructureSet::GetListOfROI() const { return mListOfROI; } @@ -201,7 +198,7 @@ void clitk::DicomRT_StructureSet::Read(const std::string & filename) assert(roi_contour_seq); // TODO error message int n=0; for (gdcm::SQItem* r=roi_contour_seq->GetFirstSQItem(); r!=0; r=roi_contour_seq->GetNextSQItem()) { - DicomRT_ROI * roi = new DicomRT_ROI; + DicomRT_ROI::Pointer roi = DicomRT_ROI::New(); roi->Read(mMapOfROIName, r); mListOfROI.push_back(roi); mMapOfROIIndex[roi->GetROINumber()] = n; @@ -240,7 +237,7 @@ int clitk::DicomRT_StructureSet::AddBinaryImageAsNewROI(vvImage * im, std::strin color.push_back(0); // Create ROI - DicomRT_ROI * roi = new DicomRT_ROI; + DicomRT_ROI::Pointer roi = DicomRT_ROI::New(); roi->SetFromBinaryImage(im, max, oss.str(), color, n); mListOfROI.push_back(roi); mMapOfROIIndex[mListOfROI.size()-1] = max; diff --git a/common/clitkDicomRT_StructureSet.h b/common/clitkDicomRT_StructureSet.h index 541a91c..84d7486 100644 --- a/common/clitkDicomRT_StructureSet.h +++ b/common/clitkDicomRT_StructureSet.h @@ -26,42 +26,46 @@ namespace clitk { - //-------------------------------------------------------------------- - class DicomRT_StructureSet { - - public: - DicomRT_StructureSet(); - ~DicomRT_StructureSet(); +//-------------------------------------------------------------------- +class DicomRT_StructureSet : public itk::LightObject{ + +public: + typedef DicomRT_StructureSet Self; + typedef itk::SmartPointer Pointer; + itkNewMacro(Self); - void Print(std::ostream & os = std::cout) const; - void Read(const std::string & filename); + void Print(std::ostream & os = std::cout) const; + void Read(const std::string & filename); - const std::vector & GetListOfROI() const; - clitk::DicomRT_ROI * GetROI(int n); - const std::string & GetStudyID() const; - const std::string & GetStudyTime() const; - const std::string & GetStudyDate() const; - const std::string & GetLabel() const; - const std::string & GetName() const; - const std::string & GetDate() const; - const std::string & GetTime() const; + const std::vector & GetListOfROI() const; + clitk::DicomRT_ROI * GetROI(int n); + const std::string & GetStudyID() const; + const std::string & GetStudyTime() const; + const std::string & GetStudyDate() const; + const std::string & GetLabel() const; + const std::string & GetName() const; + const std::string & GetDate() const; + const std::string & GetTime() const; - int AddBinaryImageAsNewROI(vvImage * i, std::string name); - - protected: - std::string mStudyID; - std::string mStudyTime; - std::string mStudyDate; - std::string mLabel; - std::string mName; - std::string mDate; - std::string mTime; - std::map mMapOfROIName; - std::map mMapOfROIIndex; - std::vector mListOfROI; + int AddBinaryImageAsNewROI(vvImage * i, std::string name); + +protected: + std::string mStudyID; + std::string mStudyTime; + std::string mStudyDate; + std::string mLabel; + std::string mName; + std::string mDate; + std::string mTime; + std::map mMapOfROIName; + std::map mMapOfROIIndex; + std::vector mListOfROI; - }; - //-------------------------------------------------------------------- +private: + DicomRT_StructureSet(); + ~DicomRT_StructureSet(); +}; +//-------------------------------------------------------------------- } // end namespace clitk #endif // CLITKDICOMRT_STRUCTURESET_H diff --git a/tools/clitkDicomRTStruct2BinaryImage.cxx b/tools/clitkDicomRTStruct2BinaryImage.cxx index 1e94853..d46e6ec 100644 --- a/tools/clitkDicomRTStruct2BinaryImage.cxx +++ b/tools/clitkDicomRTStruct2BinaryImage.cxx @@ -28,27 +28,27 @@ int main(int argc, char * argv[]) { GGO(clitkDicomRTStruct2BinaryImage, args_info); // Read and display information - clitk::DicomRT_StructureSet s; - s.Read(args_info.input_arg); - // s.Print(std::cout); + clitk::DicomRT_StructureSet::Pointer s = clitk::DicomRT_StructureSet::New(); + s->Read(args_info.input_arg); + // s->Print(std::cout); // New filter to convert to binary image clitk::DicomRT_ROI_ConvertToImageFilter filter; filter.SetCropMaskEnabled(args_info.crop_flag); filter.SetImageFilename(args_info.image_arg); // Used to get spacing + origin if (args_info.roi_arg != -1) { - filter.SetROI(s.GetROI(args_info.roi_arg)); + filter.SetROI(s->GetROI(args_info.roi_arg)); filter.SetOutputImageFilename(args_info.output_arg); filter.Update(); } else { - for(unsigned int i=0; iGetListOfROI().size(); i++) { clitk::DicomRT_ROI_ConvertToImageFilter filter; filter.SetCropMaskEnabled(args_info.crop_flag); filter.SetImageFilename(args_info.image_arg); // Used to get spacing + origin - std::string name = s.GetListOfROI()[i]->GetName(); - int num = s.GetListOfROI()[i]->GetROINumber(); - filter.SetROI(s.GetListOfROI()[i]); + std::string name = s->GetListOfROI()[i]->GetName(); + int num = s->GetListOfROI()[i]->GetROINumber(); + filter.SetROI(s->GetListOfROI()[i]); name.erase(remove_if(name.begin(), name.end(), isspace), name.end()); std::string n = std::string(args_info.output_arg).append (clitk::toString(num)).append @@ -56,7 +56,7 @@ int main(int argc, char * argv[]) { (name).append (".mhd"); if (args_info.verbose_flag) { - std::cout << i << " " << s.GetListOfROI()[i]->GetName() << " num=" << num << " : " << n << std::endl; + std::cout << i << " " << s->GetListOfROI()[i]->GetName() << " num=" << num << " : " << n << std::endl; } filter.SetOutputImageFilename(n); filter.Update(); diff --git a/vv/vvROIActor.h b/vv/vvROIActor.h index d20259e..19aca3a 100644 --- a/vv/vvROIActor.h +++ b/vv/vvROIActor.h @@ -58,7 +58,7 @@ public slots: void UpdateImage(); protected: - clitk::DicomRT_ROI * mROI; + clitk::DicomRT_ROI::Pointer mROI; vvSlicerManager * mSlicerManager; std::vector mImageContour; std::vector mOverlayActors; diff --git a/vv/vvToolStructureSetManager.cxx b/vv/vvToolStructureSetManager.cxx index 20ecad3..227ad43 100644 --- a/vv/vvToolStructureSetManager.cxx +++ b/vv/vvToolStructureSetManager.cxx @@ -100,19 +100,13 @@ vvToolStructureSetManager::vvToolStructureSetManager(vvMainWindowBase * parent, vvToolStructureSetManager::~vvToolStructureSetManager() { m_NumberOfTool--; - mStructureSetActorsList.clear(); - mMapROIToTreeWidget.clear(); - - /* - for(uint i=0; iGetListOfROI().size();i++) { - DD(i); - DD(mStructureSetsList[0]->GetROI(i)->GetImage()->GetReferenceCount()); - // mStructureSetsList[0]->GetROI(i)->GetImage()->Delete(); - } - */ - mStructureSetsList.clear(); - mOpenedBinaryImage.clear(); + for(unsigned int i=0; i< mStructureSetActorsList.size(); i++) + delete mStructureSetActorsList[i]; + + for (std::map::iterator it = mMapROIToTreeWidget.begin(); + it!=mMapROIToTreeWidget.end(); it++) + delete it->second; } //------------------------------------------------------------------------------ @@ -202,7 +196,7 @@ void vvToolStructureSetManager::AddRoiInTreeWidget(clitk::DicomRT_ROI * roi, QTr //------------------------------------------------------------------------------ void vvToolStructureSetManager::UpdateStructureSetInTreeWidget(int index, clitk::DicomRT_StructureSet * s) { // Insert ROI - const std::vector & rois = s->GetListOfROI(); + const std::vector & rois = s->GetListOfROI(); for(unsigned int i=0; i filenames; filenames.push_back(filename[i].toStdString()); - mReader->SetInputFilenames(filenames); - mReader->Update(IMAGE); + mReader.SetInputFilenames(filenames); + mReader.Update(IMAGE); QApplication::restoreOverrideCursor(); - if (mReader->GetLastError().size() != 0) { + if (mReader.GetLastError().size() != 0) { std::cerr << "Error while reading " << filename[i].toStdString() << std::endl; QString error = "Cannot open file \n"; - error += mReader->GetLastError().c_str(); + error += mReader.GetLastError().c_str(); QMessageBox::information(this,tr("Reading problem"),error); - delete mReader; return; } - vvImage::Pointer binaryImage = mReader->GetOutput(); + vvImage::Pointer binaryImage = mReader.GetOutput(); AddImage(binaryImage, filename[i].toStdString(), mBackgroundValueSpinBox->value()); mOpenedBinaryImage.push_back(binaryImage); - delete mReader; } UpdateImage(); } @@ -307,7 +299,7 @@ void vvToolStructureSetManager::AddImage(vvImage * binaryImage, std::string file int index; if (mCurrentStructureSet == NULL) { if (mStructureSetsList.size() == 0) { // Create a default SS - clitk::DicomRT_StructureSet * mStructureSet = new clitk::DicomRT_StructureSet; + clitk::DicomRT_StructureSet::Pointer mStructureSet = clitk::DicomRT_StructureSet::New(); index = AddStructureSet(mStructureSet); } else { // Get first SS @@ -592,15 +584,15 @@ void vvToolStructureSetManager::ChangeContourWidth(int n) { //------------------------------------------------------------------------------ void vvToolStructureSetManager::ReloadCurrentROI() { // Reload image - vvImageReader * mReader = new vvImageReader; - mReader->SetInputFilename(mCurrentROI->GetFilename()); - mReader->Update(IMAGE); - if (mReader->GetLastError() != "") { - QMessageBox::information(mMainWindowBase, tr("Sorry, error. Could not reload"), mReader->GetLastError().c_str()); + vvImageReader mReader; + mReader.SetInputFilename(mCurrentROI->GetFilename()); + mReader.Update(IMAGE); + if (mReader.GetLastError() != "") { + QMessageBox::information(mMainWindowBase, tr("Sorry, error. Could not reload"), mReader.GetLastError().c_str()); return; } mCurrentROI->GetImage()->GetFirstVTKImageData()->ReleaseData(); - mCurrentROI->SetImage(mReader->GetOutput()); + mCurrentROI->SetImage(mReader.GetOutput()); // Update visu" mCurrentROIActor->UpdateImage(); diff --git a/vv/vvToolStructureSetManager.h b/vv/vvToolStructureSetManager.h index 8060e8f..7ccb50f 100644 --- a/vv/vvToolStructureSetManager.h +++ b/vv/vvToolStructureSetManager.h @@ -67,20 +67,20 @@ public slots: protected: Ui::vvToolStructureSetManager ui; - clitk::DicomRT_StructureSet * mCurrentStructureSet; + clitk::DicomRT_StructureSet* mCurrentStructureSet; vvStructureSetActor * mCurrentStructureSetActor; int mCurrentStructureSetIndex; - clitk::DicomRT_ROI * mCurrentROI; + clitk::DicomRT_ROI::Pointer mCurrentROI; vvROIActor * mCurrentROIActor; vtkSmartPointer mDefaultLUTColor; bool mIsAllVisibleEnabled; int mNumberOfVisibleROI; int mNumberOfVisibleContourROI; - std::vector mStructureSetsList; + std::vector mStructureSetsList; std::vector mStructureSetActorsList; std::map mMapStructureSetIndexToTreeWidget; - std::map mMapROIToTreeWidget; - std::map mMapTreeWidgetToROI; + std::map mMapROIToTreeWidget; + std::map mMapTreeWidgetToROI; std::vector mLoadedROIIndex; std::vector mOpenedBinaryImage; -- 2.47.1