2 Replies - 1963 Views - Last Post: 30 November 2007 - 10:24 AM Rate Topic: -----

#1 gholra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-November 07

Deep copy and deleting objects

Post icon  Posted 30 November 2007 - 06:54 AM

Is there any one tat can help me with the following. I have spent 2 days debagging but can not get answere to why the program crashes.

I have 3 classes: Technicians, Jobs and Solution.
An object of type Technicinas has a vector, <Jobs*>, containing pointers to a set of jobs done by the technicians.
An object of type Solution has a vector, <Technicians*>, containing pointers to those technicinas who are in the solution of the problem.

In the end of the program, the objects of type technicinas are deleted.
The destructor is:
// Destructor
Solution::~Solution() 
{   
  cout << "Calling  the destructor of Solution:" << endl; 
  
  if( !((this->assigned_tech_vector).empty()))
  {
	   for(unsigned int i = 0; i < (this->assigned_tech_vector).size(); i++) 
		  {
			   if((this->assigned_tech_vector).at(i) != NULL)
				 {
					 cout << "Deleting a technicinan... within solution " << endl;
	 delete ((this->assigned_tech_vector).at(i));
	 ((this->assigned_tech_vector).at(i)) = NULL;
				  }
				  else
				   {
					  cout << "Found null" << endl;
					}
			 }  
			  (this->assigned_tech_vector).clear();
		  }
   else
	{
	   cout << "Vector not empty inside solution." << endl;
	 }
  
cout << "Done with  the destructor of Solution:" << endl;   
}



That is, I want to delete the technicians and the technicians in trun has to delete their job objects .
Well, this has ben may aim and I dont know if I have done right.

I also have a copy constructor for the class solution that as follows. New pointers of types Technicians and new job objects are creted.

Solution::Solution(const Solution &sol)
{
  // Beacuse objective_value is not a pointer, we can shallow copy it 
  this->objective_value = sol.getSolObjectiveValue();
  
  // The assigned_tech_vector contains pointers, so we need to deep copy it 
  this->assigned_tech_vector.resize(sol.getSolTechAssignedVector().size());
	
  vector<Technicians*> anotherCopySolPtr = sol.getSolTechAssignedVector();
  
  for(unsigned int i = 0; i < (this->assigned_tech_vector).size(); i++) 
	{
	  if( anotherCopySolPtr.at(i) != NULL)
	   {
		  ((this->assigned_tech_vector).at(i))  = new Technicians(*(anotherCopySolPtr.at(i))); 
		  if((this->assigned_tech_vector).at(i) == NULL)
		   {
			  cout << "Slution.cpp: No memory for the new technician. Exiting..." << endl;
			  exit(0);
		   }
		   cout << "A technician generated by new in solution.cpp: " << endl;
		}
	  else
		{
		  cout << "Found null" << endl;
		 }
	}
anotherCopySolPtr.clear();
}

The destructor of the Technicinas is:

Technicians::~Technicians()
{
	if( !((this->tech_jobs_pointer_vector).empty()))
	{
	cout << "The size of the job vector is: " << (this->tech_jobs_pointer_vector).size() << endl;
	
	for(unsigned int i = 0; i < ((this->tech_jobs_pointer_vector).size()); i++ ) 
	  {	
		if((this->tech_jobs_pointer_vector).at(i) != NULL)
		 {
			cout << "About to delete job from technicians." << endl;
			delete ((this->tech_jobs_pointer_vector).at(i));
			((this->tech_jobs_pointer_vector).at(i)) = NULL;
		  }
		 else
		   {
			  cout << "found null in technician" << endl;
		   }
	  }
	
	(this->tech_jobs_pointer_vector).clear();
	
  }
  else
	{
	  cout << "the size is zero inside technicinas" << endl;
	}
cout << "Deleting the technicians object." << endl;
}

And the destructor of Jobs is:

Jobs::~Jobs() 
{ 
  cout << "deleting job." << endl;
}




There are 10 technicians in my test problem. When I run the program the 10 technicinas copied by copy constructre in Solution class are deleted. The jobs as well.
Then the program tries (I think) to delete the original Technicians object and while doing this it crashes. I think the jobs that the technicians point to are not there any more and that meakes the program crash. I have eaten all my nails and hit and knocked my head at the table and test a lot of things but could not find what can be wrong. Please HELP me with this issue.

Is This A Good Question/Topic? 0
  • +

Replies To: Deep copy and deleting objects

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: Deep copy and deleting objects

Posted 30 November 2007 - 10:18 AM

I really advise against using the .at() method to modify elements of your vector in this way. Try using the push_back method to insert new pointers instead.

eg, in your copy constructor,
Solution::Solution( const Solution& sol)
{
	std::vector<Technicians*>::iterator iter = sol.assigned_tech_vector.begin();

	while( iter != sol.assigned_tech_vector.end() )
	{
		Technician* new_tech = new Technician( **iter );
		if( new_tech == NULL )
		{
			exit( -1 );
		}

		assigned_tech_vector.push_back( new_tech );
		++iter;
	}
}



Also, deleting objects in your vector should be fairly trivial. Don't worry about delete'ing null pointers. its a perfectly well formed 'do nothing' operation, and will not cause your program to crash.
Solution::~Solution()
{
	while( assigned_tech_vector.begin() != assigned_tech_vector.end() )
	{
		Technician* temp = *assigned_tech_vector.begin();
		delete temp;
		assigned_tech_vector.erase( assigned_tech_vector.begin() );
	}
} 

This post has been edited by Bench: 30 November 2007 - 10:39 AM

Was This Post Helpful? 0
  • +
  • -

#3 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: Deep copy and deleting objects

Posted 30 November 2007 - 10:24 AM

You also shouldn't be calling this inside of your ~Solution()

(this->assigned_tech_vector).clear();



You are iterating over this vector to delete technician objects. Making this
call pretty much null & voids that.


[update]
Scratch this comment..
I missed read by 1 curly brace. Thought that this was being called inside the for loop.

This post has been edited by skaoth: 30 November 2007 - 10:26 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1