大神们好!
    最近小弟在linux下遇到了一个麻烦,大致是在new一个对象时,发生异常了。部分源代码:
if (m_pclsSALDupCheck == NULL)
    {
        #ifdef __DF_VERSION__
        m_pclsSALDupCheck = new dupcheck::CMdbDupCheckDFMapReduce;
        #else
        m_pclsSALDupCheck = new dupcheck::CMdbDupCheckMapReduce;
        #endif
        DUP_LOG_S("xhb: 3!");
        if (m_pclsSALDupCheck == NULL)
        {
            DUP_LOG_X("Can't get SAL env!");
            nError = DUP_ERR_POINT_NULL;
            goto done;
        }
    }
在以下代码,线程退出了,然后进程也退出了。
m_pclsSALDupCheck = new dupcheck::CMdbDupCheckMapReduce;我使用gdb attach 进程,然后跟踪代码如下:
(gdb) stop
(gdb) b dupcheck.cpp:159
Breakpoint 1 at 0x7f9117223767: file ../src/dupcheck.cpp, line 159.
(gdb) c
Continuing.
[Switching to Thread 0x7f91168bb700 (LWP 6416)]Breakpoint 1, MediationDupcheck::CDupCheck::Start (this=0x7f9117639b60 <MediationDupcheck::global_DupCheck>)
    at ../src/dupcheck.cpp:159
159     ../src/dupcheck.cpp: No such file or directory.
(gdb) p m_pclsSALDupCheck
$1 = (dupcheck::CMdbDupCheck *) 0x0
(gdb) s
operator new (sz=sz@entry=112) at ../../../../libstdc++-v3/libsupc++/new_op.cc:43
43      ../../../../libstdc++-v3/libsupc++/new_op.cc: No such file or directory.
(gdb) p p
$2 = <optimized out>
(gdb) s
48      in ../../../../libstdc++-v3/libsupc++/new_op.cc
(gdb) p sz
$3 = 112
(gdb) p sz
$4 = 112
(gdb) p sz
$5 = 112
(gdb) s
43      in ../../../../libstdc++-v3/libsupc++/new_op.cc
(gdb) p sz
$6 = 112
(gdb) n
48      in ../../../../libstdc++-v3/libsupc++/new_op.cc
(gdb) psz
Undefined command: "psz".  Try "help".
(gdb) p sz
$7 = 112
(gdb) n
50      in ../../../../libstdc++-v3/libsupc++/new_op.cc
(gdb) p sz
$8 = 112
(gdb) p p
$9 = <optimized out>
(gdb) n
59      in ../../../../libstdc++-v3/libsupc++/new_op.cc
(gdb) p sz
$10 = 112
(gdb) p p
$11 = (void *) 0x7f910c098930
(gdb) s
[Thread 0x7f9120782700 (LWP 6406) exited]
[Thread 0x7f911e001700 (LWP 6409) exited]
[Thread 0x7f911cf22700 (LWP 6412) exited]
[Thread 0x7f91168bb700 (LWP 6416) exited]
[Thread 0x7f912b91d280 (LWP 6397) exited]
[Inferior 1 (process 6397) exited with code 0177]
(gdb) 网上找的new_op.cc源代码如下:
// Support routines for the -*- C++ -*- dynamic memory management.
2
3 // Copyright (C) 1997-2017 Free Software Foundation, Inc.
4 //
5 // This file is part of GCC.
6 //
7 // GCC is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11 //
12 // GCC is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 #include <bits/c++config.h>
27 #include <cstdlib>
28 #include <bits/exception_defines.h>
29 #include "new"
30
31 using std::new_handler;
32 using std::bad_alloc;
33 #if _GLIBCXX_HOSTED
34 using std::malloc;
35 #else
36 // A freestanding C runtime may not provide "malloc" -- but there is no
37 // other reasonable way to implement "operator new".
38 extern "C" void *malloc (std::size_t);
39 #endif
40
41 _GLIBCXX_WEAK_DEFINITION void *
42 operator new (std::size_t sz) _GLIBCXX_THROW (std::bad_alloc)
43 {
44   void *p;
45
46   /* malloc (0) is unpredictable; avoid it.  */
47   if (sz == 0)
48     sz = 1;
49
50   while (__builtin_expect ((p = malloc (sz)) == 0, false))
51     {
52       new_handler handler = std::get_new_handler ();
53       if (! handler)
54         _GLIBCXX_THROW_OR_ABORT(bad_alloc());
55       handler ();
56     }
57
58   return p;
59 }现在实在不知道是什么原因了。
CMdbDupCheckMapReduce头文件,构造函数为空实现,调用基类的。
class CMdbDupCheckMapReduce : public CMdbDupCheck
{
public:
    CMdbDupCheckMapReduce(sal::CTransaction* trans = 0);
    virtual ~CMdbDupCheckMapReduce();  
private:
    void sort(MDupCheckDef::SNumAddressList& lstSortNumAddress);  
    MDupCheckDef::SNumAddressListList m_lstLstNum;
    int32 m_iCount;//数量    
    
};基类构造函数:
CMdbDupCheck::CMdbDupCheck(sal::CTransaction* trans)
    : m_bIsOwnTrans(trans == 0), m_pTrans(trans)
{
    if ( m_bIsOwnTrans )
        m_pTrans = new sal::CTransaction("kv");
}贴的有点多,各位大神求解答,感激不尽!!

解决方案 »

  1.   

    你的代码看gdb信息是11行退出的,对应的源码是DUP_LOG_X("Can't get SAL env!");这一行,
    #ifdef __DF_VERSION__
            m_pclsSALDupCheck = new dupcheck::CMdbDupCheckDFMapReduce;
            #else
            m_pclsSALDupCheck = new dupcheck::CMdbDupCheckMapReduce;是不是要这样
    #ifdef __DF_VERSION__
            m_pclsSALDupCheck = new dupcheck::CMdbDupCheckDFMapReduce();
            #else
            m_pclsSALDupCheck = new dupcheck::CMdbDupCheckMapReduce();