4

問題は、ベクトルにアクセスしてイテレータを削除する別のクラスからの愚かなエラーでした。以下のコードとは何の関係もありません。時間を無駄にしてすみません。

初歩的なものが欠けているに違いない。オブジェクトを作成し、そのデータを操作して、それをベクトルにプッシュする関数があります。関数が終了した瞬間、プログラムはSIGSEVでクラッシュし、(Kdevelop gcc 4.5 gdb)を見つめたままになります。

   /**
   *  The dtor only erases the elements, and note that if the
   *  elements themselves are pointers, the pointed-to memory is
   *  not touched in any way.  Managing the pointer is the user's
   *  responsibility.
   */
  ~vector()
  { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
          _M_get_Tp_allocator()); }

ポインタを保存していません。インスタンス化されたオブジェクトを保存しようとしています。

void Init::initIndividual(int ID, int gen)
{
  Individual temp_person = Individual(ID,gen);
  int inst_size = getRandom<int>(1,max_inst_size);
  for (int k=0;k<inst_size;k++)
  {
    retry:
    // (1) randomly choose a body part
    int body_num = getRandom<int>(1,20);
    body_part temp_part = get_body_part(body_num);
    // NOTE: We need to make sure that the body part is unique!
    std::vector<Instruction> already_existing = temp_person.get_instructions();
    if (already_existing.size() > 0)
    {
      for (int a=0; a< already_existing.size();a++)
      {
       std::string name = already_existing[a].get_body_part();
       if ( name.compare(temp_part.name) == 0 )
       { //if body part already exists in the list, retry!
         goto retry;
       }
      }
    }    
    // (2) Create a new Instruction for this body part
    Instruction temp_inst = Instruction(temp_part.name,temp_part.max_angle,temp_part.min_angle);
    // (3) Randomly pick a number of body parameters to use
    int paramsize = getRandom<int>(1,max_params_size);
    // (4) Randomly choose time and degree trajectory parameters for this body part and append!
    for (int x=0;x < paramsize; x++)
    {
     float time = 0.0f;
     int choice = 0;
     // (4.a) If begin of body parameters
     if (x==0)
     {
   //if always start at time = 0
   if (static_time_init)
   {
     time = 0.0f;
   }
   //if randomly choose the init time
   else if (!static_time_init)
   {
     time = getRandom<float>(0.0f,(float)(time_constrain-1));
   }
     }
     // (4.b) if not @ start of params
     else if(x!=0)
     {
       redo:  
       float previous_time = temp_inst.parameters.back().time; //get previous time
       double incrementor = getRandom<double>(0.1,1.0); //increment time by min 0.1 max 1.0
       time = previous_time + (float)incrementor;
       if (time > time_constrain) //if current time is more than time constrain, redo
       { 
        goto redo;
       }
     }
     // (5) Randomly pick a degree to move to (within body part constrains)
     float degree = getRandom<float>(temp_inst.get_min_angle(),temp_inst.get_max_angle());
     Parameter foo = Parameter(time,degree);
     temp_inst.add_parameter(Parameter(time,degree));
   }
  temp_person.add_Instruction(temp_inst);
  }
  temp_person.endtime = time_constrain;
 }

それが全体の機能です。

 std::vector<Individual> population;

push_back関数は、オブジェクトをプッシュバックするときにオブジェクトをコピーしませんか?push_backがtemp_personを破棄しようとしているため、デストラクタが呼び出されますか?クラスIndividualでコピー演算子を定義していません。私は以前にこの問題に遭遇したことがあり、それを理解したことはありません。これは、関数の最後でtemp_personがスコープ外にあるために発生しますか?ありがとうございました !

編集:クラス個人

class Individual 
{
   friend class Population;
   friend class Crossover;
   friend class Init;
 private:
   std::string xml_file;
   char *arg4;
 protected:
   bool saved, mutated, dead;
   unsigned int UID, generation;
   int executions;
   std::vector<Instruction> instructions;
   int father_UID, mother_UID;
   double eta,endtime;
 public:
   int uniform;
   float fitness;
   pthread_mutex_t thread_mutex;
   //Some other functions irrelevant

命令のベクトルには別の構造体のベクトルがあることに注意してください。

class Instruction 
{
  friend class Crossover;
 private:  
  unsigned int param_size;
  float max_angle, min_angle;
  bool micro_mutated;
 public:
  std::string body_part;
  std::vector<Parameter> parameters;
  //other stuff

class Parameter
{
  public:
   float time;
   float degree;
   Parameter(float t,float d);
};

ここでクレイジーなことは何もありません。

これは、population.push_backによって取得された浅いコピーの問題である可能性がありますか?

4

3 に答える 3

6
pthread_mutex_t thread_mutex;

aをコピーしpthread_mutex_tても意味がありません。それが問題の一部だと思います。私もあなたのことを疑っていますchar*。誰がそれを所有していますか?


pthread_mutex_tコピーできないと私が信じる理由についての私の正当化:初期化されたミューテックスを取得するための唯一の文書化された方法は、を使用することpthread_mutex_initです。さらに、すべてのpthread_mutex_*関数は、(egとは異なり)ポインタ渡しを使用してミューテックスを操作しますpthread_thread_t

于 2011-06-24T17:59:09.590 に答える
2
Doesn't the push_back function copy the object when pushing it back ? 

はい、そうです。

Is the destructor invoked because push_back is trying to destroy temp_person?

いいえそうではありません。これは、関数の最後に一時オブジェクトが破棄されるために呼び出されます。

I have not defined a copy operator in class Individual. 

クラスの宣言を見てみましょう。

Does this happen because at the end of the function temp_person is out of scope ?

はい、一時オブジェクトは破棄されますが、クラスに問題がなければ、これは問題にはなりません。繰り返しますが、コードを確認する必要があります。

私はそれがコンストラクタ/デストラクタ(:

ちなみに、populationグローバルですか?アウトチ!

于 2011-06-24T17:49:57.157 に答える
0

問題は、ベクトルにアクセスしてイテレータを削除する別のクラスからの愚かなエラーでした。上記のコードとは何の関係もありません。時間を無駄にしてすみません。

于 2011-06-24T18:49:32.680 に答える