2012-09-13 108 views
0

我已经努力了一个星期来修复一个程序,在开始我得到了SIGBUS,但经过很多次尝试仍然程序得到SIGSEGV段错误,在波纹管我发布seg故障日志+源代码。如果专家帮助我解决这个段错误错误,我将非常感激。任何建议是高度赞赏。 在此先感谢pthread_mutex_trylock段错误

LL_NODE *ll_prepend(LLIST *l, void *obj) 
{ 
    if (l && obj) { 
//line bellow is module-datastruct-llist.c:167 
//mentioned in segment-fault log as frame 3 

     if (!ll_lock(l)) return NULL; 
     LL_NODE *new; 
     if(!cs_malloc(&new,sizeof(LL_NODE), -1)) return NULL; 
     new->obj = obj; 
     new->nxt = l->initial; 
     l->initial = new; 
     if (!l->last) 
      l->last = l->initial; 
     l->count++; 
     ll_unlock(l); 
     return new; 
    } 
    return NULL; 
} 






int32_t ll_lock(LLIST *l) 
{ 
    int32_t res = 1; 
    res=cs_trylock(&l->lock); 

//line bellow is module-datastruct-llist.c:51 
//mentioned in segment-fault log as frame 2 

    while (l && !l->flag && res) { 
     cs_debug_mask(D_TRACE, "trylock ll_lock wait"); 
     cs_sleepms(fast_rnd()%5 + 1); 
    } 
    return !res; 
} 







int32_t cs_trylock(pthread_mutex_t *mutex){ 

    if(!mutex) return -1; 
     int32_t result, oldtype; 
     /* Make sure that we won't get interrupted while getting the lock */ 
     pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype); 

//line bellow is oscam-simples.c:1233 
//mentioned in segment-fault log as frame 1 

     if((result=pthread_mutex_trylock(mutex)) == 0){ 

      struct s_client *cl = cs_preparelock(cur_client(), mutex); 
      if(cl) 
       cl->mutexstore_used++; 
     } 
     pthread_setcanceltype(oldtype, NULL); 
     pthread_testcancel(); 
     return result; 
    } 

// in this function &l->lock is initialized 
LLIST *ll_create() 
{ 
    LLIST *l = cs_malloc(&l, sizeof(LLIST), 0); 
    pthread_mutex_init(&l->lock, NULL); 
    return l; 
} 



Segment fault log : 
Program received signal SIGSEGV, Segmentation fault. 
[Switching to LWP 1905] 
0x2979b7ba in pthread_mutex_trylock() from /lib/libpthread.so.0 
(gdb) bt 
#0 0x2979b7ba in pthread_mutex_trylock() from /lib/libpthread.so.0 
#1 0x00410d98 in cs_trylock (mutex=0x247373a4) at oscam-simples.c:1233 
#2 0x0043d4aa in ll_lock (l=0x24737398) at module-datastruct-llist.c:51 
#3 0x0043d956 in ll_prepend (l=0x24737398, obj=0x4a2410) 
    at module-datastruct-llist.c:167 
#4 0x0040a66e in get_cw (client=0x4daa80, er=0x5063a0) at oscam.c:2645 
#5 0x00439754 in dvbapi_process_input (demux_id=0, filter_num=0, 
    buffer=0x2a98bb60 "\201q=", len=320) at module-dvbapi.c:1634 
#6 0x0043c866 in stapi_read_thread (sparam=0x4d1558) at module-dvbapi.c:2441 
#7 0x29799486 in ??() from /lib/libpthread.so.0 
Backtrace stopped: frame did not save the PC 
(gdb) info args 
No symbol table info available. 

如果u需要整个源代码:

http://www.streamboard.tv/oscam/browser/trunk/module-datastruct-llist.c?rev=5375

http://www.streamboard.tv/oscam/browser/trunk/oscam-simples.c?rev=5375

我的猜测是错误来自pthread_mutex_trylock在oscam-simples.c(框架1)我的意思是:

if((result=pthread_mutex_trylock(mutex)) == 0){ 

任何建议是非常明显的。

+2

不相关的提示:你应该避免命名变量,比如'new'或'class'等。这是因为这些是C++中的关键字,并且即使你将来可能会使用纯C编译器,也可能尝试在C++编译器中使用你的代码并且会有错误。 –

+1

@JoachimPileborg:你是否也应该避免命名'finally','super'或'transient'? 'yield','pass'或'lambda'怎么样? C不是C++,并且没有理由C代码应该用C++编译器编译;实际上,C++编译器无法编译绝大多数正确的惯用语C. –

+0

@R ..我听说过并阅读过很多使用C++编译器为其C代码的人,这主要是因为更严格的类型检查。我认为从C程序开始并不少见,然后逐步将其转换为C++(从“C with classes”开始)。 –

回答

1

问题来自l->lock这是从不初始化,因此包含非NULL不可用的垃圾。

尝试添加l->lock = NULL;或通过创建互斥锁来正确初始化它。

+0

感谢您的回复。但是,当我尝试添加new-> lock = NULL;我得到编译错误:'LL_NODE'没有任何成员'锁' 任何意见,是高度赞赏 – John

+0

你是对的,我误解你的代码,并过去的细节。你有没有试过初始化'l-> lock'?你可以在哪里发布代码?我编辑了我的答案,所以它说'l-> lock' – Eregrith

+0

tnx for reply,但仍然在添加l-> lock = NULL之后;仍然得到anther编译错误:错误:赋值中的不兼容类型 这里是完整的源代码:module-datastruct.c:http://www.streamboard.tv/oscam/browser/trunk/module-datastruct-llist.c? rev = 5375 oscam-simples.c:http://www.streamboard.tv/oscam/browser/trunk/oscam-simples.c?rev=5375这里是全球辩护:http://www.streamboard.tv/ oscam/browser/trunk/globals.h?rev = 5375 – John