<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[求问一个C++的惊天bug]]></title><description><![CDATA[<p>这个bug很诡异，从头到尾不知道为什么会segmentfault。<br />
大概代码是这样的：</p>
<pre><code>nclude &quot;cv_bridge/cv_bridge.h&quot;
#include &quot;glog/logging.h&quot;


using namespace std;
using namespace google;
using namespace cv;


SSDDetector* detector;

void cbImg() {
    // will can detector
    detector-&gt;detect();
}

int main() {
    detector = new SSDDetector();
}

</code></pre>
<p>也就是说，我把所有的代码写道了一个文件里面，没有用头文件，其中有一个detector指针需要定义为全局变量，这样可以在我的方法中调用。然后我在主函数里面初始化了这个指针。<br />
代码能够编译，但是运行的时候segmentfault。</p>
<p>用gdb debug的时候显示，恰恰就是detector这个内存引起了一段错误。那么问题来了，这种方式哪里不对吗？</p>
<p>更新：</p>
<pre><code>#include &lt;string&gt;
#include &lt;pluginlib/class_list_macros.h&gt;

#include &lt;string&gt;
#include &lt;image_transport/image_transport.h&gt;
#include &lt;object_msgs/ObjectsInBoxes.h&gt;
#include &quot;detector/mb_ssd_voc.h&quot;
#include &quot;cv_bridge/cv_bridge.h&quot;
#include &quot;glog/logging.h&quot;


using namespace std;
using namespace google;
using namespace cv;


ros::Publisher pub;
SSDDetector* detector = nullptr;


void cbImage(const sensor_msgs::ImagePtr image_msg)
{
  cv_bridge::CvImagePtr cv_ptr;
  cv_ptr = cv_bridge::toCvCopy(image_msg);
  cv::Mat img = cv_ptr-&gt;image;

  std::vector&lt;vector&lt;float&gt;&gt; detections = detector-&gt;Detect(img);

  object_msgs::ObjectsInBoxes objects;
  for (int j = 0; j &lt; detections.size(); ++j) {
	const vector&lt;float&gt; &amp;d = detections[j];

	object_msgs::ObjectInBox object_in_box;
	object_in_box.object.object_name = detector-&gt;CLASSES[d[1&rsqb;&rsqb;;
	object_in_box.object.probability = d[2];
	object_in_box.roi.x_offset = img.cols * d[3];
	object_in_box.roi.y_offset = img.rows * d[4];
	object_in_box.roi.height = img.cols*(d[5]-d[3]);
	object_in_box.roi.width = img.rows*(d[6]-d[4]);
	objects.objects_vector.push_back(object_in_box);
  }
  pub.publish(objects);

}


int main(int argc, char **argv) {

  string _model_file = &quot;/media/jintain/wd/ros/workspaces/cs_rs/src/ros_gpu_caffe/models/ssdlite/mb2_ssdlite_voc_new.prototxt&quot;;
  string _weights_file = &quot;/media/jintain/wd/ros/workspaces/cs_rs/src/ros_gpu_caffe/models/ssdlite/mb2_ssdlite_voc_new.caffemodel&quot;;
  string _mean_file;
  string _mean_value = &quot;0.5,0.5,0.5&quot;;
  float _confidence_threshold = 0.7;
  float _normalize_value = 0.007843;
  string _cpu_mode = &quot;gpu&quot;;
  int _resize_mode = 0;


  ros::init(argc, argv, &quot;talker&quot;);
  ros::NodeHandle nh(&quot;~&quot;);

  if (!nh.getParam(&quot;net_config_path&quot;, _model_file))
  {
    ROS_WARN(&quot;param net_cfg_path not set, use default&quot;);
  } else {
    LOG(INFO) &lt;&lt; &quot;net_config_path: &quot; &lt;&lt; _model_file &lt;&lt; endl;
  }
  if (!nh.getParam(&quot;weights_path&quot;, _weights_file))
  {
    ROS_WARN(&quot;param weights_path not set, use default&quot;);
  } else {
	LOG(INFO) &lt;&lt; &quot;weights_path: &quot; &lt;&lt; _weights_file &lt;&lt; endl;
  }


  detector = new SSDDetector(_model_file, _weights_file, _mean_file, _mean_value,
							 _confidence_threshold, _normalize_value, _cpu_mode,
							 _resize_mode);
  LOG(INFO) &lt;&lt; &quot;detector initialized!!\n&quot;;
  ros::Subscriber sub = nh.subscribe(&quot;/usb_cam/image_raw&quot;, 1000, cbImage);
  pub = nh.advertise&lt;object_msgs::ObjectsInBoxes&gt;(&quot;detection&quot;, 100);

}

</code></pre>
<p>这个detector是我封装的类，里面的实现其实不用管，因为其他的程序运行没有任何问题，但是就是i这种写法除了问题。。。<br />
为什么在文件开头初始化一个这样的东西，就crash呢？ 我吧nullptr去掉也是一样</p>
]]></description><link>http://t.manaai.cn/topic/96/求问一个c-的惊天bug</link><generator>RSS for Node</generator><lastBuildDate>Tue, 17 Nov 2026 17:20:01 GMT</lastBuildDate><atom:link href="http://t.manaai.cn/topic/96.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 04 Apr 2019 08:15:38 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to 求问一个C++的惊天bug on Thu, 04 Apr 2019 09:24:55 GMT]]></title><description><![CDATA[<p>这个bug很诡异，从头到尾不知道为什么会segmentfault。<br />
大概代码是这样的：</p>
<pre><code>nclude &quot;cv_bridge/cv_bridge.h&quot;
#include &quot;glog/logging.h&quot;


using namespace std;
using namespace google;
using namespace cv;


SSDDetector* detector;

void cbImg() {
    // will can detector
    detector-&gt;detect();
}

int main() {
    detector = new SSDDetector();
}

</code></pre>
<p>也就是说，我把所有的代码写道了一个文件里面，没有用头文件，其中有一个detector指针需要定义为全局变量，这样可以在我的方法中调用。然后我在主函数里面初始化了这个指针。<br />
代码能够编译，但是运行的时候segmentfault。</p>
<p>用gdb debug的时候显示，恰恰就是detector这个内存引起了一段错误。那么问题来了，这种方式哪里不对吗？</p>
<p>更新：</p>
<pre><code>#include &lt;string&gt;
#include &lt;pluginlib/class_list_macros.h&gt;

#include &lt;string&gt;
#include &lt;image_transport/image_transport.h&gt;
#include &lt;object_msgs/ObjectsInBoxes.h&gt;
#include &quot;detector/mb_ssd_voc.h&quot;
#include &quot;cv_bridge/cv_bridge.h&quot;
#include &quot;glog/logging.h&quot;


using namespace std;
using namespace google;
using namespace cv;


ros::Publisher pub;
SSDDetector* detector = nullptr;


void cbImage(const sensor_msgs::ImagePtr image_msg)
{
  cv_bridge::CvImagePtr cv_ptr;
  cv_ptr = cv_bridge::toCvCopy(image_msg);
  cv::Mat img = cv_ptr-&gt;image;

  std::vector&lt;vector&lt;float&gt;&gt; detections = detector-&gt;Detect(img);

  object_msgs::ObjectsInBoxes objects;
  for (int j = 0; j &lt; detections.size(); ++j) {
	const vector&lt;float&gt; &amp;d = detections[j];

	object_msgs::ObjectInBox object_in_box;
	object_in_box.object.object_name = detector-&gt;CLASSES[d[1&rsqb;&rsqb;;
	object_in_box.object.probability = d[2];
	object_in_box.roi.x_offset = img.cols * d[3];
	object_in_box.roi.y_offset = img.rows * d[4];
	object_in_box.roi.height = img.cols*(d[5]-d[3]);
	object_in_box.roi.width = img.rows*(d[6]-d[4]);
	objects.objects_vector.push_back(object_in_box);
  }
  pub.publish(objects);

}


int main(int argc, char **argv) {

  string _model_file = &quot;/media/jintain/wd/ros/workspaces/cs_rs/src/ros_gpu_caffe/models/ssdlite/mb2_ssdlite_voc_new.prototxt&quot;;
  string _weights_file = &quot;/media/jintain/wd/ros/workspaces/cs_rs/src/ros_gpu_caffe/models/ssdlite/mb2_ssdlite_voc_new.caffemodel&quot;;
  string _mean_file;
  string _mean_value = &quot;0.5,0.5,0.5&quot;;
  float _confidence_threshold = 0.7;
  float _normalize_value = 0.007843;
  string _cpu_mode = &quot;gpu&quot;;
  int _resize_mode = 0;


  ros::init(argc, argv, &quot;talker&quot;);
  ros::NodeHandle nh(&quot;~&quot;);

  if (!nh.getParam(&quot;net_config_path&quot;, _model_file))
  {
    ROS_WARN(&quot;param net_cfg_path not set, use default&quot;);
  } else {
    LOG(INFO) &lt;&lt; &quot;net_config_path: &quot; &lt;&lt; _model_file &lt;&lt; endl;
  }
  if (!nh.getParam(&quot;weights_path&quot;, _weights_file))
  {
    ROS_WARN(&quot;param weights_path not set, use default&quot;);
  } else {
	LOG(INFO) &lt;&lt; &quot;weights_path: &quot; &lt;&lt; _weights_file &lt;&lt; endl;
  }


  detector = new SSDDetector(_model_file, _weights_file, _mean_file, _mean_value,
							 _confidence_threshold, _normalize_value, _cpu_mode,
							 _resize_mode);
  LOG(INFO) &lt;&lt; &quot;detector initialized!!\n&quot;;
  ros::Subscriber sub = nh.subscribe(&quot;/usb_cam/image_raw&quot;, 1000, cbImage);
  pub = nh.advertise&lt;object_msgs::ObjectsInBoxes&gt;(&quot;detection&quot;, 100);

}

</code></pre>
<p>这个detector是我封装的类，里面的实现其实不用管，因为其他的程序运行没有任何问题，但是就是i这种写法除了问题。。。<br />
为什么在文件开头初始化一个这样的东西，就crash呢？ 我吧nullptr去掉也是一样</p>
]]></description><link>http://t.manaai.cn/post/219</link><guid isPermaLink="true">http://t.manaai.cn/post/219</guid><dc:creator><![CDATA[刘看山]]></dc:creator><pubDate>Thu, 04 Apr 2019 09:24:55 GMT</pubDate></item><item><title><![CDATA[Reply to 求问一个C++的惊天bug on Thu, 04 Apr 2019 10:14:20 GMT]]></title><description><![CDATA[<p>依赖的其他 so  存在 同名全局变量？</p>
]]></description><link>http://t.manaai.cn/post/220</link><guid isPermaLink="true">http://t.manaai.cn/post/220</guid><dc:creator><![CDATA[haithink]]></dc:creator><pubDate>Thu, 04 Apr 2019 10:14:20 GMT</pubDate></item><item><title><![CDATA[Reply to 求问一个C++的惊天bug on Sat, 06 Apr 2019 02:35:38 GMT]]></title><description><![CDATA[<p>换了一台机器就可以了，自此，这问题成了世界未解之谜</p>
]]></description><link>http://t.manaai.cn/post/221</link><guid isPermaLink="true">http://t.manaai.cn/post/221</guid><dc:creator><![CDATA[刘看山]]></dc:creator><pubDate>Sat, 06 Apr 2019 02:35:38 GMT</pubDate></item></channel></rss>