Skip navigation.
Home
The QNX Community Portal

View topic - SIGBUS error.

SIGBUS error.

For discussion of realtime and/or embedded programming.

RE: SIGBUS error.

Postby mario » Wed Aug 25, 2010 12:19 am

Oh I see, make sense now.
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

Re: RE: SIGBUS error.

Postby Ericxx » Tue Aug 31, 2010 1:08 pm

AG wrote:Try to use memcpy() instead of assignment...
Actually, post here the line where you got SIGBUS.


I defined a clsMatrix and used it in my user application, the code snippet as follows:

Code: Select all
class clsMatrix {
protected:
   double *pM;
   int m;
   int n;

   BOOL m_bAutoDelete;
   //m_bAutoDeletenewbAutoDeleteTRUEbAutoDeleteFALSE

public:
   clsMatrix();
   clsMatrix(int m, int n, double *p = NULL, BOOL bAssign = FALSE);

   ~clsMatrix();

   void Reset();
   void Reset(int i, int j, double *p = NULL, BOOL bAssign = FALSE);

//   Attributes
   int GetM() { return m; }
   int GetN() { return n; }
   double *GetP() { return pM; }

   BOOL EqualSize(clsMatrix &M) { return ((m == M.m) && (n == M.n)); }

public:
//   Operators
   double * operator [] (int i) { return pM+i*n; }

   clsMatrix & operator = (clsMatrix &M);
   clsMatrix & operator = (double *p);
   clsMatrix & operator = (double x);
   clsMatrix & operator += (clsMatrix &M);
   clsMatrix & operator += (double *p);
   clsMatrix & operator -= (clsMatrix &M);
   clsMatrix & operator -= (double *p);
   clsMatrix & operator *= (clsMatrix &mtrx);            //
   clsMatrix & operator *= (double *p);
   clsMatrix & operator *= (double a);
   clsMatrix & operator /= (double a);

   static void X(clsMatrix &mtrx1, clsMatrix &mtrx2, clsMatrix &mtrx);            //multiple
   static void X(clsMatrix &mtrx1, clsVector &vctr2, clsVector &vctr);
   static void X(clsVector &vctr1, clsMatrix &mtrx2, clsVector &vctr);
   static void T(clsMatrix &mtrx1, clsMatrix &mtrx);            //transpose
   static void R(clsMatrix &mtrx1, clsMatrix &mtrx);            //inverse
   static void S(clsMatrix &mtrx1, int m0, int m1, int n0, int n1, clsMatrix &mtrx, int m = 0, int n = 0);
   static void S(clsVector &vctr1, int m0, int m1, clsVector &vctr, int m = 0);

   double e();
   double e2();
   double ei();

public:
   BOOL LoadT(char *pszFile);
   BOOL SaveT(char *pszFile);

   friend class clsVector;
};

clsMatrix::clsMatrix()
{
   m = n = 0;
   pM = NULL;
   m_bAutoDelete = FALSE;
}

the problem happens in the clsMatrix constructor, whenever a clsMatrix object is declared, the SIGBUS error occurs, unless the contructor is empty.


Eric
Ericxx
Senior Member
 
Posts: 158
Joined: Mon Jun 09, 2008 1:38 pm

RE: Re: RE: SIGBUS error.

Postby maschoen » Tue Aug 31, 2010 1:23 pm

Well there are two things strange about this. First, why you are getting the SIGBUS.

The second is why you didn't sequentially comment out lines in the constructor until you figured out which variable being set is causing it.

Assuming you go ahead and do this, I think the next step would either be to get an assembler listing to see if somehow bad code is being generated or step through with a debugger to see if that doesn't explain the problem.
maschoen
QNX Master
 
Posts: 2644
Joined: Wed Jun 25, 2003 5:18 pm

RE: Re: RE: SIGBUS error.

Postby mario » Tue Aug 31, 2010 2:19 pm

There is no new information in your post. You were asked to print the address of each variable to help determine if it is a alignment issue.

clsMatrix::clsMatrix()
{
std::cout << &m << std::endl;
std::cout << &n << std::endl;
std::cout << &pM << std::endl;
std::cout << &m_bAutoDelete << std::endl;
}

What type is BOOL?
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

RE: Re: RE: SIGBUS error.

Postby Ericxx » Tue Aug 31, 2010 2:53 pm

Thanks. BOOL is self-defined "int". this is the output, since another class has several member of type clsMatrix, the output generates as follows:

memory of m: 13d1f6
memory of n: 13d1fa
memory of pM: 13d1f2
memory of BOOL: 13d1fe
memory of m: 13d286
memory of n: 13d28a
memory of pM: 13d282
memory of BOOL: 13d28e
memory of m: 14a884
memory of n: 14a888
memory of pM: 14a880
memory of BOOL: 14a88c


Seems to me they are all even-aligned?
Ericxx
Senior Member
 
Posts: 158
Joined: Mon Jun 09, 2008 1:38 pm

Re: RE: Re: RE: SIGBUS error.

Postby Tim » Tue Aug 31, 2010 3:16 pm

Maschoen,

maschoen wrote:The second is why you didn't sequentially comment out lines in the constructor until you figured out which variable being set is causing it.



In one of his earlier posts he said he already did this step. That each line he commented out pushed the crash down to the next uncommented line.

I'm curious about the C++ idioms that we *don't* see here such as:

1) The function table pointer that belongs to all C++ classes.
2) The fact that the constructor is assigning (not initializing) values to variables inside the constructor *after* the compiler has already done initialization via the initialization list (that isn't in this code) Ie

Code: Select all
clsMatrix::clsMatrix :
  pM(NULL),
  m(0),
  n(0)
  m_bAutoDelete(false)
{
}


is how I'd normally write such a constructor. Afterall, if you don't use the initializer list the compiler silently does so for you anyway and you basically repeat the work again when you do the manual assignments inside the constructor.

One or both of those could be the problem.

Tim
Tim
Senior Member
 
Posts: 1390
Joined: Wed Mar 10, 2004 12:28 am

Re: RE: Re: RE: SIGBUS error.

Postby Tim » Tue Aug 31, 2010 3:26 pm

Eric,

Ericxx wrote:memory of m: 13d1f6
memory of n: 13d1fa
memory of pM: 13d1f2
memory of BOOL: 13d1fe
memory of m: 13d286
memory of n: 13d28a
memory of pM: 13d282
memory of BOOL: 13d28e
memory of m: 14a884
memory of n: 14a888
memory of pM: 14a880
memory of BOOL: 14a88c

Seems to me they are all even-aligned?


The are even aligned if you mean divisible by 2. But 13d1f6 is definitely not 4 byte aligned. Is your processor 2 or 4 byte aligned? If it's 4 byte aligned you have a problem.

Tim
Tim
Senior Member
 
Posts: 1390
Joined: Wed Mar 10, 2004 12:28 am

RE: Re: RE: Re: RE: SIGBUS error.

Postby mario » Tue Aug 31, 2010 3:36 pm

Each variable is 4 bytes apart, that looks good, but the whole object is NOT aligned on a 4 byte boundary. Are the objects instantiated on the stack or via new. My guess is it`s on the stack. I have seen that happen, it`s the stack that is not aligned. Just a guess try compiling with -mstack-align
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

RE: Re: RE: Re: RE: SIGBUS error.

Postby Ericxx » Wed Sep 01, 2010 3:17 am

Hi, mario. I have tried to add the compile option in the build property, but "-mstack-align" is not recognizable. And my target is ARM cortex-A8 OMAP3530.
Ericxx
Senior Member
 
Posts: 158
Joined: Mon Jun 09, 2008 1:38 pm

Re: RE: Re: RE: SIGBUS error.

Postby Ericxx » Wed Sep 01, 2010 5:25 am

Code: Select all
clsMatrix::clsMatrix :
  pM(NULL),
  m(0),
  n(0)
  m_bAutoDelete(false)
{
}


Code: Select all
#ifndef BOOL
#define BOOL   int
#define TRUE   1
#define FALSE   0
#endif


I used this constructor, now the crash line comes to the last assignment in m_bAutoDelete. So the self-defined type BOOL causes the error?

Thanks,
Eric
Ericxx
Senior Member
 
Posts: 158
Joined: Mon Jun 09, 2008 1:38 pm

Re: RE: Re: RE: SIGBUS error.

Postby Tim » Wed Sep 01, 2010 1:57 pm

Eric,

I typo'd my example in that I used false (the C++ false) instead of FALSE (your false).

Not sure that makes a difference but you might a well use your defined FALSE for completeness sake.

Also, how do you know where the crash line is? The debugger is likely just showing the last line of the constructor definition. You'd need to examine at the assembly level to determine exactly which variable is the problem.

Just for *fun*, try doing this in your class definition:

Code: Select all
class clsMatrix {
protected:
   char foo;
   char bar;
   double *pM;
   int m;
   int n;

clsMatrix::clsMatrix :
  char foo(0),
  char bar(0)
  pM(NULL),
  m(0),
  n(0)
  m_bAutoDelete(FALSE)
{
}
 


The 2 extra characters should space the integers on a 4 byte boundary at which point the SIGBUS should stop.

Tim

P.S. Exactly which compiler are you using if the stack align Mario suggested isn't working?
Tim
Senior Member
 
Posts: 1390
Joined: Wed Mar 10, 2004 12:28 am

Postby Ericxx » Wed Sep 01, 2010 2:12 pm

Thanks. But does not work for me. I attached the assembly this around as follows:
Code: Select all

clsMatrix::clsMatrix() :
0x0012187c <clsMatrix>:                     mov    r12, sp
0x00121880 <clsMatrix+4>:                   push   {r11, r12, lr, pc}
0x00121884 <clsMatrix+8>:                   sub    r11, r12, #4   ; 0x4
0x00121888 <clsMatrix+12>:                  sub    sp, sp, #4   ; 0x4
0x0012188c <clsMatrix+16>:                  str    r0, [r11, #-16]
0x001218dc <clsMatrix>:                     mov    r12, sp
0x001218e0 <clsMatrix+4>:                   push   {r11, r12, lr, pc}
0x001218e4 <clsMatrix+8>:                   sub    r11, r12, #4   ; 0x4
0x001218e8 <clsMatrix+12>:                  sub    sp, sp, #4   ; 0x4
0x001218ec <clsMatrix+16>:                  str    r0, [r11, #-16]
   m_bAutoDelete(FALSE)
0x00121890 <clsMatrix+20>:                  ldr    r3, [r11, #-16]
0x00121894 <clsMatrix+24>:                  mov    r2, #0   ; 0x0
0x00121898 <clsMatrix+28>:                  strb   r2, [r3]
0x0012189c <clsMatrix+32>:                  ldr    r3, [r11, #-16]
0x001218a0 <clsMatrix+36>:                  mov    r2, #0   ; 0x0
0x001218a4 <clsMatrix+40>:                  strb   r2, [r3, #1]
0x001218a8 <clsMatrix+44>:                  ldr    r3, [r11, #-16]
0x001218ac <clsMatrix+48>:                  mov    r2, #0   ; 0x0
0x001218b0 <clsMatrix+52>:                  str    r2, [r3, #4]
0x001218b4 <clsMatrix+56>:                  ldr    r3, [r11, #-16]
0x001218b8 <clsMatrix+60>:                  mov    r2, #0   ; 0x0
0x001218bc <clsMatrix+64>:                  str    r2, [r3, #8]
0x001218c0 <clsMatrix+68>:                  ldr    r3, [r11, #-16]
0x001218c4 <clsMatrix+72>:                  mov    r2, #0   ; 0x0
0x001218c8 <clsMatrix+76>:                  str    r2, [r3, #12]
0x001218cc <clsMatrix+80>:                  ldr    r3, [r11, #-16]
0x001218d0 <clsMatrix+84>:                  mov    r2, #0   ; 0x0
0x001218d4 <clsMatrix+88>:                  str    r2, [r3, #16]
0x001218f0 <clsMatrix+20>:                  ldr    r3, [r11, #-16]
0x001218f4 <clsMatrix+24>:                  mov    r2, #0   ; 0x0
0x001218f8 <clsMatrix+28>:                  strb   r2, [r3]
0x001218fc <clsMatrix+32>:                  ldr    r3, [r11, #-16]
0x00121900 <clsMatrix+36>:                  mov    r2, #0   ; 0x0
0x00121904 <clsMatrix+40>:                  strb   r2, [r3, #1]
0x00121908 <clsMatrix+44>:                  ldr    r3, [r11, #-16]
0x0012190c <clsMatrix+48>:                  mov    r2, #0   ; 0x0
0x00121910 <clsMatrix+52>:                  str    r2, [r3, #4]
0x00121914 <clsMatrix+56>:                  ldr    r3, [r11, #-16]
0x00121918 <clsMatrix+60>:                  mov    r2, #0   ; 0x0
0x0012191c <clsMatrix+64>:                  str    r2, [r3, #8]
0x00121920 <clsMatrix+68>:                  ldr    r3, [r11, #-16]
0x00121924 <clsMatrix+72>:                  mov    r2, #0   ; 0x0
0x00121928 <clsMatrix+76>:                  str    r2, [r3, #12]
0x0012192c <clsMatrix+80>:                  ldr    r3, [r11, #-16]
0x00121930 <clsMatrix+84>:                  mov    r2, #0   ; 0x0
0x00121934 <clsMatrix+88>:                  str    r2, [r3, #16]
{}
0x001218d8 <clsMatrix+92>:                  ldm    sp, {r3, r11, sp, pc}
0x00121938 <clsMatrix+92>:                  ldm    sp, {r3, r11, sp, pc}
clsMatrix::clsMatrix(int i, int j, double *p, BOOL bAssign)
0x0012193c <clsMatrix>:                     mov    r12, sp
0x00121940 <clsMatrix+4>:                   push   {r11, r12, lr, pc}
0x00121944 <clsMatrix+8>:                   sub    r11, r12, #4   ; 0x4
0x00121948 <clsMatrix+12>:                  sub    sp, sp, #20   ; 0x14
0x0012194c <clsMatrix+16>:                  str    r0, [r11, #-16]
0x00121950 <clsMatrix+20>:                  str    r1, [r11, #-20]
0x00121954 <clsMatrix+24>:                  str    r2, [r11, #-24]
0x00121958 <clsMatrix+28>:                  str    r3, [r11, #-28]
   m = n = 0; pM = NULL; m_bAutoDelete = FALSE;
0x0012195c <clsMatrix+32>:                  ldr    r3, [r11, #-16]
0x00121960 <clsMatrix+36>:                  mov    r2, #0   ; 0x0
0x00121964 <clsMatrix+40>:                  str    r2, [r3, #12]
0x00121968 <clsMatrix+44>:                  ldr    r3, [r11, #-16]
0x0012196c <clsMatrix+48>:                  ldr    r2, [r3, #12]
0x00121970 <clsMatrix+52>:                  ldr    r3, [r11, #-16]
0x00121974 <clsMatrix+56>:                  str    r2, [r3, #8]
0x00121978 <clsMatrix+60>:                  ldr    r3, [r11, #-16]
0x0012197c <clsMatrix+64>:                  mov    r2, #0   ; 0x0
0x00121980 <clsMatrix+68>:                  str    r2, [r3, #4]
0x00121984 <clsMatrix+72>:                  ldr    r3, [r11, #-16]
0x00121988 <clsMatrix+76>:                  mov    r2, #0   ; 0x0
0x0012198c <clsMatrix+80>:                  str    r2, [r3, #16]


Seems all right....
btw, where can i set the options for stack alignment in the IDE?

Regards,
Eric
Ericxx
Senior Member
 
Posts: 158
Joined: Mon Jun 09, 2008 1:38 pm

Postby mario » Wed Sep 01, 2010 4:02 pm

Hum the stack-align option is not supported on ARM!

I don`t think the issue is in the code of the class. The adresses that were printed showed that for some object, alignmement was on 4 byte bondary and for other it was not. So it seems to me the stack is getting "missaligned" by something else outside the class.

I suspect that on Arm ( which I know nothing about ), it should be impossible to have a misaligned stack since everything is 32 bits. When you print the addresses at first it was misaligned and then became. Hence something is causing the stack to become missaligne. I would move around the instanciation of the object to figure out where this stack issue is appearing.

You didn`t say if the object are allocated on the heap or on the stack?
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

Postby Ericxx » Thu Sep 02, 2010 4:27 am

Well, i think it better off putting the source code here. Great thanks for helping find the bug. And please kindly specify the math library when compiling it.
And the objects as you can see, are allocated on the stack.

Regards,
Eric
Attachments
memAlignTest.rar
(13.21 KiB) Downloaded 129 times
Ericxx
Senior Member
 
Posts: 158
Joined: Mon Jun 09, 2008 1:38 pm

Postby Tim » Thu Sep 02, 2010 2:03 pm

Eric,

memAlignTest.cc doesn't come *close* to compiling. There are missing header files that cause all kinds of compiler errors.

I can compile matrix.cpp on it's own. But the main() is in memAlignTest.cc.

Also when I looked in memAlignTest.cc I see your matrix class is itself wrapped inside lots of other classes. It's so entangled that I can't easily see how deep it's nested.

When Mario/Masochen mentioned doing a test they were expecting a simple test like:

main()
{
clsMatrix m;
}

and that's it. Just literally declare an instance of your class, print out the addresses of the internal variables. Maybe add a few more lines and assign values to those internal variables.

If that works, then your problem lies somewhere inside all the class nesting going on in memAlignTest.cc


Tim
Tim
Senior Member
 
Posts: 1390
Joined: Wed Mar 10, 2004 12:28 am

PreviousNext

Return to Realtime and Embedded

Who is online

Users browsing this forum: No registered users and 2 guests

cron