Memory corruption?

Go To Last Post
20 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Guys,

 

Wonder if someone could give me some pointers as to what's going on.

 

Studio 7

 

I have a program that runs on both an Atmega324pb and 328p

 

I'm having an issue where the same code behaves badly on the 328p as if I'm trampling on memory, but the 324pb is fine

 

stats on build

Program Memory Usage : 3664 bytes   11.2 % Full

Data Memory Usage : 535 bytes   26.1 % Full

EEPROM Memory Usage : 308 bytes   30.1 % Full

 

It occurs where I'm passing a 32 byte array to a function in a class (by reference) and I'm reading the contents into another array (only half of it)

 

The debugger shows random data being retrieved and my loop counter x is a large value even when initialised to 0.  If I remove the array from the call, it's all it should be.  In addition, some local variables seem to be missing in the Autos and Local windows.

 

 

Scratching my head here.

 

regards

 

Nik Middleton

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

nikm wrote:
I have a program that runs on both an Atmega324pb and 328p

This is one source program, but has been compiled for the appropriate target micro, correct?  Are you loading the correct hex file for the target? 

Or are you saying you are loading the same program (hex file) in to both targets?  This is not likely to work!

 

Jim

 

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No I'm compiling and debugging on each target with a work pc and a home PC. There is a #define in a header file which defines the target.  All of the rest of the code is fine.

 

regards

 

Nik Middleton

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

nikm wrote:
 If I remove the array from the call,

Are you passing this data by value, or by reference?   Show the function declaration. 

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi,

 

here's the definition

 

uint8_t groupSet(int8_t group, uint8_t num_count, uint8_t data[]);

 

all values are scrambled but only with the 328P.  remove the last element and it's fine

 

 

regards

 

Nik Middleton

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hypothesis : the extent of data is incorrect "somehow"

test : linter over the entire source code, the file, or the pertinent snippet

Some linters will create a caution, warning, or information statement about source code that's valid wrt a compiler.

 

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The first two are pass by value so are copies of group and num_count, the last one is a pointer so you might try defining it that way, i.e.  uint8_t* data and see if that helps. 

not sure why it works with one and not the other.  

Can you create and post a small complete program that demos the problem, then a freak can compile and help you debug it. 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for that. I'll give it a try.  Working up a demo may take a little time as it's a mix of C and C++.  I guess it could also be the optimisation level that's messing up the debugger.  I haven't tried to see if that data is copied outside of the debug window.  It wouldn't be the first time I've seen it happen

 

regards

 

Nik Middleton

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Always use -Og for debug!

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tried using that debug  level and now it's silly values.  

This line:

 

for(int8_t x=2; x <18;x++){

}

 

Before was showing the correct values, but now in the autos it shows a value of 464 and the type is 'unsigned long@X' !

 

So confused.

 

regards

 

Nik Middleton

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Turn on all compiler warnings and make sure to

fix all of them.

 

--Mike

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
for(int8_t x=2; x <18;x++){

}

spaces make it easier to read (and troubleshoot too)

for(int8_t x = 2; x < 18; x++){

}

not that, that is the problem above, just style nit picking.

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Turned on warnings.  Wasn't aware that it needs to be done in two sections (C & C++) as its a mix.

 

No errors, just this one warning

 

void ATL::writeDigit(int8_t buffOffset,char data,bool DP)
{

    DisplayBuffer[buffOffset -1] = data;
    if(DP)
          DPBuffer[buffOffset -1] = MAX_DP;

    
}

 

Warning        overflow in implicit constant conversion [-Woverflow]   this line - ->   (DPBuffer[buffOffset -1] = MAX_DP;)

 

I'm not sure why it's complaining though

 

regards

 

Nik Middleton

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The value of MAX_DP cannot fit into DPBuffer[].

 

Or, if MAX_DP is an expression, some part of it overflows.

 

Show the declaration of DPBuffer[], and the #define for MAX_DP.

 

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1) Can you use a data memory breakpoint to check what part of the code modifies the memory? I'm not sure those devices support it though...

2) Are you using interrupts? If so have you tried turning interrupts off?

3) Did you check that all variables in your application are initialized? 

4) Did you lint your code?

 

/Jakob Selbing

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Post the complete project e.g. ZIP up your AS7.0 project and attach the zip file.
.
These things generally come down to exceeding array bounds e.g. by using the wrong loop counters.
It is quicker to post the buildable code. Readers' eyes might spot things that you missed.
Yes, you can post the snippets that YOU think might be the problem. Your preparation might show you the error but more likely you omit the fatal snippet.
The beauty of the internet is that you can post a project that can be built by anyone, anywhere in the world e.g. AS7.0
.
David..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

volatile int8_t DPBuffer[32];

 

#define MAX_DP   0x80

 

regards

 

Nik Middleton

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I know what you're saying, but the project is more than the code.  It consists of SPI, TWI and to exercise the code you need a master TWI to send commands/requests.  My original post was really to determine if anyone had come across the IDE/debugger behaving this way.

 

So that I can work at work and also at home, I transfer the code to myself in email. (I have GIT in place as well)

As it happens, I copied what I thought were identical files from home(code worked there) to work and the issue has gone away!  I guess it must of been something very subtle.  I'm looked at a diff, but there's nothing obvious 

 

regards

 

Nik Middleton

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jaksel wrote:
I'm not sure those devices support it though...
supported for mega324, maybe not for mega328

Atmel Studio 7 - General Information on Data Breakpoint

...

 

Maximum number of data breakpoint supported: (this may vary based on specific device/family refer data sheet)

[table : CPU architecture, Maximum Data Breakpoint Supported]

 

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MAX_DP has the value 128 which doesn't fit in an

int8_t (range is -128..127).

 

--Mike