Arduino freezing segfault?

Hi,

Recently, I encountered a problem about.. Arduino freezing.
Actually, I'm coding a class to use friendly the Zigbee protocol in API mode.
This class is called ZigbeeStack but implement some additionnal fields in the protocol for specifics needs.

The problem is the following:
I try to run a simple program running the ZigbeeStack class to test it but when using sending process, the arduino freez after few packets sent ( about 60). The previous packets are correctly sent and receive by other Zigbee modules.
It seems there is a memory overflow or segfault but I'm not able to find any code that can cause this kind of problem.

Here is my class and main Arduino program in attachment.

I've some debugg Serial instruction and here is the output of Serial just before freezing:

Break Point 4                                                                   
Break Point 5                                                                   
Break Point 6                                                                   
Break Point 2                                                                   
Break Point 3                                                                   
Sous Break Point 1                                                              
Sous Break Point 2                                                              
Sous Break Point 3                                                              
Sous Break Point 4                                                              
Sous Break Point 5                                                              
Sous Break Point 6                                                              
Sous Break Point 7                                                              
1                                                                               
Break Point 4                                                                   
Break Point 5                                                                   
Break Point 6                                                                   
Break Point 2                                                                   
Break Point 3                                                                   
Sous Break Point 1                                                              
Sous Break Point 2                                                              
Sous Break Point 3                                                              
Sous Break Point 4                                                              
Sous Break Point 5                                                              
Sous Break Point 6                                                              
Sous Break Point 7                                                              
1

It seems the problem comes from the end of the constructPacket function, any idea? :confused:

I tried to see the free memory with severial class and method proposed on arduino playground but those returns strange results (-31 ...).
Thanks in advance!

ZigbeeStack.cpp (10.2 KB)

ZigbeeStack.h (5.97 KB)

zigbee_test.ino (806 Bytes)

I tried to see the free memory with severial class and method proposed on arduino playground but those returns strange results (-31 ...).

What would you suppose that a negative number meant? How much memory is available if you see a value of 126? How much is available if you see -32?

	TX_request_header(std::vector<uint8_t> arg){

Are you serious? You need to get the special Arduino model that has 2 terabytes of memory. Unfortunately, they are made mostly of unobtainium, so they are in limited supply most of the time.

Thanks.
Should I try StandardCplusplus/README.md at master · maniacbug/StandardCplusplus · GitHub then?
Why should I avoid regular C++ vector? It's just to stock few hundred bytes maximum.
About the freeRam() function it returns -28 even with this basic program:

void setup(){
	Serial.begin(115200);
	Serial.println("Hello");
	Serial.println(freeRam());
}

void loop(){
    Serial.print("FreeRam : ");
    Serial.println(freeRam());
	delay(100);
}

int freeRam () {
  extern int __heap_start, *__brkval; 
  int v; 
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}

Thank you for your help.

Why should I avoid regular C++ vector? It's just to stock few hundred bytes maximum.

A few hundred bytes of Flash or a few hundred bytes of SRAM? Details matter.

http://playground.arduino.cc/Code/AvailableMemory

Does your Library code require more than about 200 bytes of SRAM at max?

If so, how is it going to fit alongside other libraries the user might select, or alongside the user's own code?

And, in my simple world, it would far more reliable to allocate all the needed space for variables and constants at startup - not while the program is running.

...R

In my case, I'm using the SRAM on a DUE arduino board for a buffer space and what I don't understand is why the program freez after few loop of doing exactly the same thing as if a memory leak was here inspite of freeing the memory for each dynamique allocation.
Even when using a 100 or 50 bytes buffer space, it's the same thing except that the freez happen later.

Just which Arduino are you using?

Mark

void loop(){
	uint8_t * lbuffer = new uint8_t [2500];
	if(lbuffer == NULL)
		Serial.println("Fatal Error: Fail to allocate memory");

	for(ushort i = 0; i <2500; i++)
		lbuffer[i] = 0xAA;

So, you've got a fatal error, but you carry on as if nothing had happened?

Arduino DUE with Arduino IDE 1.5.7 bêta.

@AWOL: indeed it was a quick test and since I haven't any error message, I didn't add any special treatment : /

I didn't add any special treatment

Not writing to the array you didn't allocate hardly qualifies as "special treatment".

On a microcontroller things are very different from under a multitasking OS with
memory management.

Firstly you must avoid techniques that assume RAM is cheap, because it isn't,
its quite literally a million times scarcer and more valuable. Forget any libraries
written for a general purpose machine completely.

Secondly if you have a fatal error you must report it and stop, and then maybe reset.

  Serial.println (".......some message describing the problem....") ;
  // maybe dump some state to serial here.
  Serial.flush () ;
  // maybe wait a bit and reset, or...
  while (true)   // during development you want to stop here, not "bash on regardless"
  {}

Of course, you don't want to make things worse. . .

Serial.println (F(".......some message describing the problem....")) ;

Good point - although that error handling should have been there from the start
and doesn't have to be a long string, "E3" meaning error 3 would do.

"E3" meaning error 3 would do.

Sure. Just ask any new coder about those cryptic messages from the compiler.

PaulS:

"E3" meaning error 3 would do.

Sure. Just ask any new coder about those cryptic messages from the compiler.

Don't be so negative! I'm sure the OP will write very comprehensive documentation that includes full details for all the error codes.

Well - there is always an opportunity to do a "first" in the open-source world.

...R