在嵌入式 Linux 平台上使用 std::string 时出现段错误 [英] Seg Fault when using std::string on an embedded Linux platform

查看:42
本文介绍了在嵌入式 Linux 平台上使用 std::string 时出现段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在嵌入式 Arm Linux 平台上运行时出现问题,我已经研究了几天.不幸的是,该平台使我无法使用任何常用的有用工具来查找确切的问题.在运行 Linux 的 PC 上运行相同的代码时,我没有收到此类错误.

I have been working for a couple of days on a problem with my application running on an embedded Arm Linux platform. Unfortunately the platform precludes me from using any of the usual useful tools for finding the exact issue. When the same code is run on the PC running Linux, I get no such error.

在下面的示例中,我可以通过取消对字符串、列表或向量行的注释来可靠地重现问题.给他们留下评论会导致应用程序运行完成.我希望有什么东西在破坏堆,但我看不到什么?程序将运行几秒钟,然后出现分段错误.

In the sample below, I can reliably reproduce the problem by uncommenting the string, list or vector lines. Leaving them commented results in the application running to completion. I expect that something is corrupting the heap, but I cannot see what? The program will run for a few seconds before giving a segmentation fault.

代码使用 arm-linux 交叉编译器编译:

The code is compiled using a arm-linux cross compiler:

arm-linux-g++ -Wall -otest fault.cpp -ldl -lpthread
arm-linux-strip test

非常感谢任何想法.

#include <stdio.h>
#include <vector>
#include <list>
#include <string>

using namespace std;
/////////////////////////////////////////////////////////////////////////////

class TestSeg
{
 static pthread_mutex_t     _logLock;

 public:
  TestSeg()
  {
  }

  ~TestSeg()
  {
  }

  static void* TestThread( void *arg )
  {
   int i = 0;
   while ( i++ < 10000 )
   {
    printf( "%d\n", i );
    WriteBad( "Function" );
   }
   pthread_exit( NULL );
  }

  static void WriteBad( const char* sFunction )
  {
   pthread_mutex_lock( &_logLock );

   printf( "%s\n", sFunction );
   //string sKiller;     //       <----------------------------------Bad
   //list<char> killer;    //       <----------------------------------Bad
   //vector<char> killer;    //       <----------------------------------Bad

   pthread_mutex_unlock( &_logLock );
   return;
  }

  void RunTest()
  {
   int threads = 100;
   pthread_t     _rx_thread[threads];
   for ( int i = 0 ; i < threads ; i++ )
   {
    pthread_create( &_rx_thread[i], NULL, TestThread, NULL );
   }

   for ( int i = 0 ; i < threads ; i++ )
   {
    pthread_join( _rx_thread[i], NULL );
   }
  }

};

pthread_mutex_t       TestSeg::_logLock = PTHREAD_MUTEX_INITIALIZER;


int main( int argc, char *argv[] )
{
 TestSeg seg;
 seg.RunTest();
 pthread_exit( NULL );
}

推荐答案

也许您正在使用标准库的单线程版本,包括 newdelete 运营商?

Maybe you're using a single-threaded version of the standard library, including the new and delete operators?

那些对象正在互斥锁的保护内构造,但在这些边界外被销毁,因此析构函数可能会相互踩踏.一种快速测试是将范围括号 {} 放在 killer 的声明周围.

Those objects are being constructed within the guards of your mutex, but are destructed outside those bounds, so the destructors might be stepping on each other. One quick test would be to put scoping brackets {} around the declaration of killer.

有关更多信息,请参阅gcc 文档.

See the gcc documentation for more.

这篇关于在嵌入式 Linux 平台上使用 std::string 时出现段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆