[英]Seg Fault when using std::string on an embedded Linux platform
我在嵌入式Arm Linux平台上運行的應用程序問題已經解決了幾天。 不幸的是,該平台使我無法使用任何常用的有用工具來查找確切的問題。 當在運行Linux的PC上運行相同的代碼時,我沒有得到這樣的錯誤。
在下面的示例中,我可以通過取消注釋字符串,列表或向量行來可靠地重現該問題。 讓他們留下評論的結果使應用程序運行完成。 我希望某些東西正在破壞堆,但是我看不到什么? 在給出分段錯誤之前,該程序將運行幾秒鍾。
該代碼使用arm-linux交叉編譯器進行編譯:
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 );
}
也許您正在使用標准庫的單線程版本,包括new
和delete
運算符?
這些對象是在互斥對象的保護范圍內構造的,但在這些邊界之外會被破壞,因此析構函數可能會互相踩踏。 一種快速的測試方法是將范圍界定符{}
置於killer
聲明的周圍。
有關更多信息,請參見gcc文檔 。
您沒有說什么是PTHREAD_MUTEX_INITIALIZER,但是您是在TestSeg :: _ logLock上調用pthread_mutex_init嗎? 如果您使用的是未初始化的互斥鎖,則這些構造函數的堆棧和/或堆操作會干擾您的互斥鎖。
您是否嘗試過-Os和-O0? 您的arm-linux-g ++ --version是什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.