2012-07-31 10 views
5

मेरे पास क्लास "कैरेक्टर" समेत एक बड़ा सी ++ प्रोग्राम है। "Character.h" में पहले स्ट्रक्चर कैरेक्टर सेटिंग्स को घोषित किया जाता है, और फिर क्लास कैरेक्टर (उनके कन्स्ट्रक्टर समेत)।सार्वजनिक चर जोड़ने पर क्रैश

कैरेक्टर (दूसरों के बीच) एक कैरेक्टर सेटिंग्स * सेटिंग्स और पॉइंट पॉज़ है। कैरेक्टरसेटिंग में पॉइंट पसंदीदा वेग है।

यह ठीक काम करता है।

हालांकि, जब मैं चरित्र के लिए किसी भी सार्वजनिक चर जोड़ने के लिए, प्रोग्राम क्रैश हो जाता है जब मैं इस फोन:

drawLine(character.pos, character.pos+character.settings->preferredVelocity, character.radius/3.0, 128, 80, 0); 

इस लाइन पर कार्यक्रम दुर्घटनाओं:

Point operator + (const Point &p2) const 
    { return Point(x + p2.x, y + p2.y); } 

मैं यह करने के कोशिश कर रहा है मान character.pos + character.settings-> preferredVelocity। मुझे प्राप्त त्रुटि संदेश

Unhandled exception at 0x004bc4fc in ECMCrowdSimulation.exe: 0xC0000005: Access violation reading location 0x7f80000f. 

जब मैं इसे देखता हूं, p2.x और p2.y अपरिभाषित हैं। अतिरिक्त चर के बिना, वे नहीं हैं। मुझे बिल्कुल पता नहीं है कि क्या हो रहा है, डिबगिंग कैसे शुरू करें या मेरी मदद करने के लिए आपको कौन सी जानकारी की आवश्यकता होगी! किसी भी तरह की सहायता का स्वागत किया जाएगा!

संपादित करें: ठीक है यहां कम से कम Character.h फ़ाइल है!

#pragma once 

/* 
* ECM navigation mesh/crowd simulation software 
* (c) Roland Geraerts and Wouter van Toll, Utrecht University. 
* --- 
* Character: A single moving character in the simulation. 
*/ 

#include "../ECM/GraphComponents/CMNode.h" 
#include "VectorOperation.h" 
#include "IndicativeRoute.h" 
#include "float.h" 

#define _USE_MATH_DEFINES 
#include <math.h> 

#include <vector> 
using std::vector; 
#include <queue> 
using std::queue; 

#define CHARACTER_RELAXATIONTIME 0.5f 

typedef vector<CMNode>::iterator node_ptr; 
class Corridor; 
class CMMResult; 

    struct CMEdge; 
    class CMMInterface; 
    class MicroInterface; 
    class CMMSceneTransfer; 

    struct CharacterSettings 
    { 
    private: 
     bool index_bb_initialized, index_bb_cp_initialized, index_ir_circle_initialized, index_ir_circle_mu_initialized; 
     bool index_2nd_ir_circle_initialized, index_2nd_ir_circle_mu_initialized; 

    public: 
     // --- Unique identifier within the simulation 
     int id; 

     // --- Velocity and speed 
     Point preferredVelocity;// Newly computed velocity *before* local collision avoidance 
     Point newVelocity;  // Newly computed velocity (+ collision avoidance), to be applied in the "next" simulation step 

     float total_max_speed; // Maximum possible speed throughout the entire simulation 
     float max_speed;  // Maximum speed at this point in time 
     float min_desired_speed;// Minimum speed that the character tries to reach when it is not standing still 

     Point lastAttractionPoint; 

     // --- IRM parameters 
     CMMInterface* cmmImplementation; // the type of indicative route to follow within the corridor, e.g. "shortest path" or "weighted side". 
     // Only used in WEIGHTED_SIDE: 
     float sidePreference;  // bias to following a certain "side" of the corridor. Must be between -1 (left) and 1 (right). 
     float sidePreferenceNoise; // extra noise factor that will be added to sidePreference at each route element. 
     // Used in WEIGHTED_SIDE and SHORTEST_PATH 
     float preferred_clearance; // Distance (m) by which the agent prefers to stay away from obstacles. 

     // --- Micro simulation model (e.g. for collision avoidance between characters) 
     MicroInterface* microImplementation;// the local model to use 
     short micro_maxNrNeighbours;  // the number of neighbours to check in the local model 
     float micro_personalSpaceRadius; // radius of the personal space (m), on top of the character's physical radius. 
              // Entering this disk (radius + personalSpace) is seen as a 'collision'. 

     // --- Corridor/Path pointers 
     node_ptr index_bb;   // point on backbone path (used for computing attraction force) 
     node_ptr index_bb_cp;  // point on the backbone path(used for computing the closest point) 
     curve_ptr index_ir_circle; // index to last point on the indicative route that intersects with a circle 
     float index_ir_circle_mu; // factor wrt to point on the indicative route that intersects with a circle 

     friend Character; // only the Character class can look into private members (WvT: ugly C++ practice, but it does work) 

     CharacterSettings(int _id, 
      // speed 
      float _total_max_speed, float _min_desired_speed, 
      // type of indicative route 
      CMMInterface* _cmmImplementation, float _sidePreference, float _sidePreferenceNoise, float _clearance, 
      // type of micro simulation model 
      MicroInterface* _microImplementation) : 

      id(_id), total_max_speed(_total_max_speed), min_desired_speed(_min_desired_speed), 
      cmmImplementation(_cmmImplementation), sidePreference(_sidePreference), sidePreferenceNoise(_sidePreferenceNoise), preferred_clearance(_clearance), 
      microImplementation(_microImplementation) 
     { 
      // velocities 
      newVelocity = Point(0, 0); 
      max_speed = total_max_speed; 
      preferredVelocity = Point(0, 0); 

      // corridor/IRM pointers 
      index_bb_initialized = false; 
      index_bb_cp_initialized = false; 
      index_ir_circle_initialized = false; 
      index_ir_circle_mu_initialized = false; 

      // default micro settings 
      micro_maxNrNeighbours = 5; // default for Karamouzas 2010: 5 
      micro_personalSpaceRadius = 0.0f; // default for Karamouzas 2010: 0.5 
     } 
    }; 

    class Character 
    { 
    public: 
     Point pos; 
     float radius; 
     Point prevPos; 
     int i; //The thing that is pretending to be the culprit, without this, it works fine. 

     // goal data 
     Point goalPos; 
     float goalRadius; 

     // status flags 
     bool reachedGoal; 
     bool freeze; // whether or not the character is temporarily frozen 
     bool freezeNotified; 
     bool reachedDestSet; 

     Point velocity; // last used velocity 

     // corridor/path pointers 
     Point retraction, cp; 

     //Contains more detailed settings of agent 
     CharacterSettings * settings; 

    public: 
     // --- constructor 
     Character(int _id, Point &_pos, float _radius, 
      // speeds 
      float _total_max_speed, float _min_desired_speed, 
      // type of indicative route 
      CMMInterface* _cmmImplementation, float _sidePreference, float _sidePreferenceNoise, float _clearance, 
      // type of micro simulation model 
      MicroInterface* _microImplementation) : 

      pos(_pos), radius(_radius) 
     { 
      settings = new CharacterSettings(_id, _total_max_speed, _min_desired_speed, 
       _cmmImplementation, _sidePreference, _sidePreferenceNoise, _clearance, _microImplementation); 

      velocity = Point(0, 0); 
      prevPos=_pos; 

      reachedGoal = true; 
      freeze = false; 
      freezeNotified = false; 
      reachedDestSet = false; 
      //isProxy = false; 
     } 

     // --- destructor 
     void removeSettings(); 

     // computing the new actual velocity through an acceleration vector: Euler integration 
     inline void integrateEuler(const Point &acc, float dtSim) 
     {     
      settings->newVelocity = velocity + dtSim * acc; 
      trim(settings->newVelocity, settings->max_speed); 
     } 

     inline void updatePos(float dtSim) 
     {  
      prevPos=pos; 

      // update velocity 
      velocity = settings->newVelocity; 

      // update position 
      pos += dtSim * velocity; 

      // if the character is close to its goal, it should stop moving  
      if(!reachedGoal // goal was not already reached 
       && settings->lastAttractionPoint == goalPos 
       && distSqr(pos, goalPos) < 0.25)//goalRadius) 
      { 
       reachedGoal = true; 
       // (do not reset the velocity, so that we can keep the last walking direction) 
      } 
     } 

     void resetIndices(); 
     node_ptr &getIndex_bb(Corridor &corridor); 
     node_ptr &getIndex_bb_cp(Corridor &corridor); 
     curve_ptr &getIndex_ir_circle(IndicativeRoute &ir); 
     float &getIndex_ir_circle_mu(); 
     Point &getRetraction() { return retraction; } 
     Point &getClosestPoint() { return cp; } 

     // computing the cost of some edge (in A*), by using personal preferences 
     float getEdgeCost(const CMEdge& edge, float activeFraction); 

     // computing the character's area, based on its radius 
     float getArea() const; 


    }; 

चीज जो सबकुछ दुर्घटनाग्रस्त हो जाती है 'int i' जोड़ रही है।

+0

क्या आप हमें न्यूनतम पूर्ण उदाहरण दे सकते हैं? यही है, क्या आप दो फाइलों को कोड को कम कर सकते हैं जो पोस्ट करने के लिए पर्याप्त छोटे हैं, जो संकलित करते हैं, और समस्या को पुन: उत्पन्न करते हैं? एक अच्छा मौका है कि ऐसा करने में आप स्वयं को बग उजागर करेंगे, और यदि आप नहीं करते हैं, तो हमारे पास देखने के लिए असली कोड होगा। – Beta

+0

क्या आप इसे वर्णित करने के बजाय 'वर्ण' की परिभाषा पोस्ट कर सकते हैं? – hmjd

+0

मुझे डर है कि आपके कोड का कुछ टुकड़ा अपरिभाषित व्यवहार का कारण बनता है और इससे आपका कोड यादृच्छिक या "अजीब" स्थानों पर क्रैश हो सकता है जो समस्या की जड़ से पूरी तरह से असंबंधित हो सकता है। हमें एक न्यूनतम कोड प्रदान करना जो इसे पुन: उत्पन्न करता है, बहुत मदद करेगा। – ereOn

उत्तर

8

आमतौर पर ऐसी अजीब समस्याएं होती हैं जब आपके पास नई संस्करणों के साथ जुड़े शीर्षलेख के पुराने संस्करण के साथ संकलित फ़ाइलों के बीच असंगतताएं होती हैं। ऐसे मामलों में, ऑब्जेक्ट का लेआउट अलग-अलग हो सकता है (कम से कम, इसकी sizeof अलग है) दो फ़ाइलों के बीच।

इसलिए, एक फ़ाइल में ऐसा लगता है कि ऑब्जेक्ट के साथ सबकुछ सही है, लेकिन एक बार दूसरी फ़ाइल में किसी फ़ंक्शन में पास हो जाने पर, आपको पूरी तरह से यादृच्छिक व्यवहार मिलता है।

ऐसे मामलों में, पूरे प्रोजेक्ट का पुनर्निर्माण समस्या हल करता है। बेहतर अभी भी, अगर आप मेकफ़ाइल मैन्युअल रूप से लिख रहे हैं तो निर्भरताओं को ठीक करने का प्रयास करें। यदि आप gcc/g ++ का उपयोग कर रहे हैं, तो आप मेकफ़ाइल स्वीकार्य निर्भरताओं को उत्पन्न करने के लिए इस तरह के कमांड का उपयोग कर सकते हैं:

g++ -MM -MG -Ipath/to/includes/that/should/be/part/of/dependency *.cpp 
+0

फिर से धन्यवाद :)। – Tessa

+0

बहुत बहुत धन्यवाद। मैं अपनी परियोजना को छोड़ने वाला था क्योंकि मैंने सोचा था कि कुछ प्रकार की स्मृति भ्रष्टाचार थी या नहीं। धन्यवाद! –

+0

प्रश्न के लिए और उत्तर के लिए धन्यवाद। मैंने सोचा कि मैं पागल हो रहा था, एक साधारण चर जोड़ने से पूरे कार्यक्रम को एक बहुत ही अजीब बिंदु पर गिरने का कारण बन गया। और अन्य चरों के संबंध में जहां मैंने नया जोड़ा, दुर्घटनाग्रस्त होने का बिंदु कहीं अलग था। –

संबंधित मुद्दे