segmentation fault by using virtual method in C++
I got a C++ object oriented program that was working right. I have decided to modify it by adding some polimorpysm definining a class hierarchy with virtual methods. When I call the virtual method it produces an fault segmentation error, likely because I have trash in the object.
This is the call and the warming up
  GPUAntColony *colony; // Base class
  GPUAntColonyConfiguration config;
  set_config(config);
  set_initial_pheromone(problem, config);
  colony = (GPUAntColony *)new GPUSimpleAntColony(problem, config);//inhereted class
  colony->run(); //Virtual method
Now let me show you the base class
 class GPUAntColony {
 private:
    void reset_ants() {
    for(unsigned int i=0; i<configuration_.number_of_ants;i++) {
         ants_[i]= Util::random_number(problem_->number_of_vertices());
    }
  }
  void initialize_Pheromone(){
   for(unsigned int i=0; i<problem_->number_of_vertices()*problem_->number_of_vertices();i++) {
       pheromones_[i]=(float)configuration_.initial_pheromone;
   }
  }
   protected:
  float * pheromones_;
    float alpha_;
    float beta_;
  unsigned int iterations;
    GPUAntColonyConfiguration::LocalSearchType local_search_type_;
  GPUAntColonyConfiguration configuration_;
  unsigned int * ants_;
    GPUOptimizationProblem *problem_;
  public:  
 ///Class Constructor for the Class GPU Ant Colony        
 GPUAntColony(GPUOptimizationProblem *problem, const GPUAntColonyConfiguration &config){
   iterations=4096; 
      problem_ = problem; // Including distance array
   configuration_ = config;
   ants_= (unsigned int*) malloc(config.number_of_ants*sizeof(unsigned int));   
      pheromones_ = (float *) malloc(problem->number_of_vertices()*problem->number_of_vertices()*sizeof(float)); 
   alpha_ = config.alpha;
   std::cout << "alpha_ " << alpha_ << std::endl;
      beta_ = config.beta;
      local_search_type_ = config.local_search;
 }
  virtual void run();  
  virtual ~GPUAntColony() {
      delete problem_;
      free(ants_);
      free (pheromones_);
    };
};
The definition of the child class
class GPUSimpleAntColony : public GPUAntColony{
public:
    GPUSimpleAntColony(GPUOptimizationProblem *problem, const GPUAntColonyConfiguration &config);
    void run();
};
And finally the implementation of such method
void GPUAntColony::run(){ 
  reset_ants();
  initialize_Pheromone();  
}
GPUSimpleAntColony::GPUSimpleAntColony(GPUOptimizationProblem *p开发者_StackOverflow社区roblem, const GPUAntColonyConfiguration &config):GPUAntColony(problem, config) {
}
void GPUSimpleAntColony::run() {
 GPUAntColony::run();
  antColonyGPULauncher(configuration_.number_of_ants, problem_->number_of_vertices(), problem_->get_distances(), pheromones_,ants_,alpha_, beta_,
             configuration_.evaporation_rate, iterations, 0, 0, 0, 0, ACO_SIMPLE);
}
Hopefully you can help me.
Many thanks in advance.
I notice you're allocating memory with malloc without checking the return value. Any chance you're getting back a NULL? Especially with pheromones_ which appears to require n*n space, and your sample code does not give us the value n.
Thank you every one mates, but it was absolutely a silly thing as always. I had a statical variable that was affecting the execution. Now is working perfectly,
Thank you a lot
cheers
In the base class make run pure virtual or provide a default implementation for it.
virtual void run() = 0;
In the child class, declare run virtual.
virtual void run();
EDIT: The real problem is that the base class run() isn't pure virtual. As mentioned in comments below the child class run() will automatically become virtual, but I think it is still clearer to explicitly declare it as such.
Compare http://codepad.org/6MlrH5Q4 to http://codepad.org/lBWaEefT
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论