开发者

Member value changes between successive calls of the same function

开发者 https://www.devze.com 2022-12-12 20:47 出处:网络
I have a CognitiveEntity class, defined this way: class CognitiveEntity : public Object { public: CognitiveEntity (FuzzyCognitiveMap fcm, SystemState s);

I have a CognitiveEntity class, defined this way:

class CognitiveEntity : public Object
{
public:
  CognitiveEntity (FuzzyCognitiveMap fcm, SystemState s);
  ~CognitiveEntity ();

  template <typename T> void RegisterChange (std::string context, T value);

  bool operator!= (const CognitiveEntity& rhs) const;

private:
  FuzzyCognitiveMap m_fuzzyCognitiveMap;
  SystemState       m_systemState;

  std::vector <SystemState> RunFuzzyCognitiveMap ();
};

As shown, a CognitiveEntity has a SystemState object, which in turn has a vector of Concept objects (only the most relevant lines are shown):

cl开发者_StackOverflow中文版ass SystemState
{
public:
  SystemState ();
  ~SystemState ();

  void       AddConcept (Concept c) { m_L.push_back(c); }
  std::vector <Concept> m_L;
};

Inside the CognitiveEntity::RegisterChange, I mark a Concept as a potential cause (by calling Concept::IsPotentialCause (bool) which merely sets a private member with the value passed):

template <typename T>
void
CognitiveEntity::RegisterChange (std::string context, T value)
{
  std::string name = context.substr(context.find_last_of ("/") +1);
  int pos = m_systemState.FindConcept(name);
  if (pos > -1)
  {
    int intValue = value ? 1 : 0;
    m_systemState.m_L[pos].SetConceptValue (intValue, false);

    if (m_systemState.m_L[pos].CheckVariation ())
    {
      m_systemState.m_L[pos].IsPotentialCause (true); // Mark this concept as a potential cause

      for (int cause = 0; cause < m_systemState.GetSize (); cause++)
      {
        if ( (cause != pos) && (m_systemState.m_L[cause].MayBeCause ()))
        {
          m_fuzzyCognitiveMap.UpdateFuzzyCognitiveMapEntry (cause, pos, m_systemState);
          m_systemState.m_L[cause].IsPotentialCause (false);
        }
      }
    }
  }

}

What happens is that as soon as RegisterChange is called another time, the Concept that was marked as potential cause, is marked no more. I tried running gdb and I am sure that that member is not set elsewhere.

I'm not sure if this little information is enough for you to give me some hints about such a behavior (I didn't want to flood the post with the code of both SystemState and Concept classes).

Regards, Jir


If this was a multi-threaded system, I'd say it sounds like a classic case of shared, mutable state that wasn't properly synchronized.

If you don't have a multi-threaded situation, I'd say set a watch on that variable and see what changes it.


Turns out the problem lied in how the code was called from within the network simulator (the code was meant to be used in the "ns-3" network simulator).

So, the problem wasn't even in the code I posted, and yet you managed to help me find the solution: thanks to the suggestions you gave me, I prepared a standalone version of the code and I watched the variable.

The problem was how I was passing the object. Specifically, instead of passing around the object by reference (as I thought I was doing) I should have used smart pointers.

Thank you all for the great insights! (and sorry for the mess... next time I'll be more accurate!)

0

精彩评论

暂无评论...
验证码 换一张
取 消