昨天发现了是m_request指针被删除的次数少于前后打印的次数,证明删除指针这里有一些猫腻,今天继续查看m_request到底是什么。
查看requestHandler.h头文件,看到m_request的定义:
99 //! The Request we're currently working on 100 Request* m_request;
发现m_request是一个Request类型的指针,继续查看Request类, 在Request.h头文件里:
71 class Request 72 { 73 public: 74 enum 75 { 76 REQUEST_OK = 0, 77 REQUEST_NOTFOUND, 78 REQUEST_TIMEOUT 79 }; 80 virtual ~Request() { std::cout<< "~Request" << std::endl; }; 81 /** The action to perform to fulfill the request. */ 82 virtual int execute(void)=0; 83 virtual std::ostream& put(std::ostream& stream)const=0; 84 virtual std::string what()=0; 85 bool checkAge(unsigned int ageLimit); 86 bool checkAge(); 87 static void setDelay(unsigned int delay); 88 static void setMaxAge(unsigned int maxAge); 89 protected: 90 std::vector<DataChannel*> *m_dataChannels; 91 Request(std::vector<DataChannel*> *dataChannels); 92 bool timeToExecute(); 93 94 enum {MAX_NUMBER_OF_FRAGMENT_MANAGERS=1500}; 95 tstamp m_ts1; 96 static unsigned int s_delay; 97 static unsigned int s_maxAge; // in us 98 };
可以看出Request是一个抽象类,使用m_request时,有没有指向其子类对象的过程呢? 在头文件里找到FragmentRequest类是继承于Request类,通过加打印并运行程序,确定m_request这个基类指针指向的是子类FragmentRequest。查看FragmentRequest的析构函数:
92 FragmentRequest::~FragmentRequest() { 93 std::cout << "~FragmentRequest" << std::endl; 94 if (m_deleteChannelVector) { 95 delete m_dataChannels; 96 } 97 }
ROS运行过程中执行析构函数时,没有进入if语句,没有删除掉m_dataChannels.
(批注:只有收到DFM_CLEAR消息时,才会进行request的clear操作,所以FragmentRequest的析构函数本来就不带删除操作,不进入if是符合逻辑的。)