Problem solved, but why?

Hi,

Most of my posts on this forum are along the lines of 'how do I fix this...'. This one is different, I've actually fixed the problem but I'd now like to know why my original method wouldn't work.

In one of my libraries I've defined this value....

#define max_chars      150

Which I occasionally need to use elsewhere. This is my original (very simple) function that doesn't work....

    inline byte getWidthx(){return (byte)max_chars;}     // Get the maximum width of messages in the stack

And this is my new improved version that does work.

/**************************************************************************
// Get the maximum width of messages in the stack
**************************************************************************/
byte MiniStack2::getWidth()
{
  return (byte)max_chars;
}

To my mind they appear to be identical. However, when I call them from another library like this....

void EventManager::begin()
{
  _maxMessLen = EventManStack->getWidthx();
  Serial.print("_maxMessLen x=<");Serial.print(_maxMessLen,DEC);Serial.println(">");
  _maxMessLen = EventManStack->getWidth();
  Serial.print("_maxMessLen=<");Serial.print(_maxMessLen,DEC);Serial.println(">");
}

I get the following....

_maxMessLen x=<247>
_maxMessLen=<150>

So, the first function return 247 - which is wrong, and the 2nd returns 150 - which is correct.

Why? they both look to me like they should be returning the same values.

Apologies for only including snippets, the full code is quite extensive and mostly irrelevant to the question (I think!)

thanks

What was the previous value of _maxMessLen before calling getWidthx, 247?

remove the byte casts '(byte)'
and put the function calls directly inline:

void EventManager::begin()
{
  Serial.print("_maxMessLen x=<");Serial.print(EventManStack->getWidthx(),DEC);Serial.println(">");
  Serial.print("_maxMessLen=<");Serial.print(EventManStack->getWidth(),DEC);Serial.println(">");
}

If nothing changes, use the original and declare _maxMessLen as volatile. Maybe nothing will change, but it would be interesting to see.
It also looks like you are using an outdated arduino IDE version.

Thanks for the suggestions pYro

What was the previous value of _maxMessLen

Value was zero.

remove the byte casts '(byte)'

tried that - made no difference

put the function calls directly inline:

tried that too, still no change!

declare _maxMessLen as volatile.

I can't do that, _maxMessLen is used to define the length of some character strings so needs to be declared up front.

looks like you are using an outdated arduino IDE version.

Yes, I'm using version 21. I'm working on the principle that unless there's a positive need for an upgrade I'd prefer not to risk the possibility of an 'upgrade hell'

Thanks for the suggestions. Sometimes it's re-assuring when I don't get flooded with a deluge of posts pointing out an obvious error - at least it means I'm not as stupid as I thought.

Cheers

There has to be a reason for it, can you post more or all of your code?

Can you explain how the call to EventManStack->getWidthx() works? The definition you showed seems to be a regular function and not a class method, so perhaps there is something else going on that we have overlooked.

Preferably, reduce your big complex sketch to a minimal test case that shows this specific behaviour and then post the whole of it.

I'm trying to cut the code down to a couple of stripped-down libraries to post here. In the meantime here's some more details.
max_chars and non-functioning function getWidthx() are defined in the file MiniStack2.h. The working function getWidth() is defined in MiniStack2.cpp

I have then used the MiniStack2 library to create a singleton version of this library called sMiniStack2

I have a 2nd library called EventManager, which uses a singleton object from the sMiniStack2 library.

Finally I have a test sketch that creates an EventManager object (which in turn creates a singleton version of MiniStack2) and then invokes EventManager.begin() to give the results previously shown.

Strangely, I seem to be capable of writing code that once I've written I have trouble understanding myself!