SOLVED: [SLOEBER] program crashes

Development environment specific, Arduino, Eclipse, VS2013,Em::Blocks etc
Post Reply
kostbill
Posts: 51
Joined: Mon Aug 07, 2017 7:56 am

SOLVED: [SLOEBER] program crashes

Post by kostbill » Tue Aug 08, 2017 9:35 pm

[UPDATE BY ADMIN]

This thread discusses problems with the Sloeber IDE, which does not fully support the STM32.

The problems described in this post do not occur if you use the Arduino IDE

See this post http://www.stm32duino.com/viewtopic.php ... =70#p33060








Hello, first post here.

I am working with the Sloeber, but I am adding Roger Clark's github libraries.

I am trying to port the KNX library from Franck Marini and so far I am having problems with malloc.

When I remove the malloc call, the program does not crash, when I leave it in there, it crashes.

I think it has to do with the Sloeber configuration.
In the menu->preferences, in Arduino:
Private Library path: C:\Arduino\hardware\Arduino_STM32\STM32F1\libraries
Private hardware path: C:\Arduino\hardware\Arduino_STM32\STM32F1
The above are, as I mentioned, from Roger Clark's github.

When I right click->go to definition on the malloc function, the choices I have, are not from these paths, instead they are from some Sloeber paths.

Do you also think that this may cause the problem?
How can I change that?

Apart from that, the ILI9341 test code works great when the program does not crash.

Thanks everyone!
Bill.
Last edited by kostbill on Wed Aug 16, 2017 9:00 pm, edited 4 times in total.

stevestrong
Posts: 1611
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: Program crashes with call to malloc.

Post by stevestrong » Tue Aug 08, 2017 10:05 pm

Replace the malloc call by fix arrays/objects, then it should work.
Malloc is not recommended for devices with such small memories.

kostbill
Posts: 51
Joined: Mon Aug 07, 2017 7:56 am

Re: Program crashes with call to malloc.

Post by kostbill » Tue Aug 08, 2017 10:30 pm

This is what I plan to do, but I wanted something to verify that the library works.
This should not be the case, it should work, right? Is there any known bug for malloc?

User avatar
RogerClark
Posts: 6918
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Program crashes with call to malloc.

Post by RogerClark » Wed Aug 09, 2017 12:25 am

You have not described your problem very well in the title of our post

I think what you are saying is that you tried to use a specific 3rd party library, which did not work.

The library you vaguely referred to seems to be

https://github.com/franckmarini/KnxDevice

I suspect the bug has nothing to do with malloc, unless its the size its malloc'ing is effected by the size native types on this 32 bit device vs size of objects on the 8 bit AVR device e.g. you could end up attempting to malloc 4 times as much memory as on the AVR

However the problem is more likely to be something to do with global constructor instantiation where the constructor attemps to use the hardware before parts of the core have been initialised

kostbill
Posts: 51
Joined: Mon Aug 07, 2017 7:56 am

Re: Program crashes with call to malloc.

Post by kostbill » Wed Aug 09, 2017 7:50 am

Hi,

Yes this is the library.

I think you are correct, the problem is caused in the construction of the KNX objects.
Since the constructor of the KNX objects is using malloc, I imagine that malloc tries to allocate non-existing memory since the init isn't fully done yet.

I don't think I would ever think of that (well, from now on I hope I will :) )

What is your suggestion on solving this? How do I tell the software to construct the objects after the full init of the core?
Is there an attribute that can specify this?
Or is it a good approach to first KNXObject pointers and init them in setup() and never destruct them?

Thanks for the insight,
Bill.

User avatar
RogerClark
Posts: 6918
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Program crashes with call to malloc.

Post by RogerClark » Wed Aug 09, 2017 7:55 am

Are you sure its malloc that is crashing ?

In the constructor, if you change the code to return just prior to the malloc, I presume its OK, and if you return just after the malloc, it never returns ?

You also need to make a really simple example, that just has the global instance of that library object and don't try to use the object.

PS.

Looking at the code, it looks like one of the constructors calls

https://github.com/franckmarini/KnxDevi ... ct.cpp#L39

calls a function lengthCalculation

https://github.com/franckmarini/KnxDevi ... pp#L28-L30

which uses program memory reads to load from this array

https://github.com/franckmarini/KnxDevi ... #L109-L171

I think its far more likely that these program memory arrays have not been setup when the code attempts to use them, and therefore tries to malloc a huge amount of memory

Basically the code is written for the AVR processor which does not use the same system architecture (flat memory addressing),
e.g. AVR uses Harvard architecture https://en.wikipedia.org/wiki/Atmel_AVR
where as ARM devices Von Neumann architecture use https://en.wikipedia.org/wiki/Von_Neumann_architecture

I'd suggest you get rid of the progmem stuff first

kostbill
Posts: 51
Joined: Mon Aug 07, 2017 7:56 am

Re: Program crashes with call to malloc.

Post by kostbill » Wed Aug 09, 2017 8:55 am

RogerClark wrote:
Wed Aug 09, 2017 7:55 am
Are you sure its malloc that is crashing ?

In the constructor, if you change the code to return just prior to the malloc, I presume its OK, and if you return just after the malloc, it never returns ?

You also need to make a really simple example, that just has the global instance of that library object and don't try to use the object.
I have done that, I commented out just the malloc part, I constructed the objects and didn't use it, and it didn't crash.
PS.

Looking at the code, it looks like one of the constructors calls

https://github.com/franckmarini/KnxDevi ... ct.cpp#L39

calls a function lengthCalculation

https://github.com/franckmarini/KnxDevi ... pp#L28-L30

which uses program memory reads to load from this array

https://github.com/franckmarini/KnxDevi ... #L109-L171

I think its far more likely that these program memory arrays have not been setup when the code attempts to use them, and therefore tries to malloc a huge amount of memory

Basically the code is written for the AVR processor which does not use the same system architecture (flat memory addressing),
e.g. AVR uses Harvard architecture https://en.wikipedia.org/wiki/Atmel_AVR
where as ARM devices Von Neumann architecture use https://en.wikipedia.org/wiki/Von_Neumann_architecture

I'd suggest you get rid of the progmem stuff first
I was reading about progmem in a post here on how to import libraries and I got rid of the PROGMEM word at the first stages of compiling. I also removed pgm_read_byte:
pgm_read_byte(&KnxDPTIdToFormat[dptId]) becomes -----> KnxDPTIdToFormat[dptId]

However, it still crashes. I feel very bad about this, I have no idea what to do.

Thanks,
Bill.

User avatar
RogerClark
Posts: 6918
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Program crashes with call to malloc.

Post by RogerClark » Wed Aug 09, 2017 10:22 am

I'm sure someone will have time to help you debug this, but to start with, I'd try to isolate the problem

chance the code so that it just uses a length of 100, and see if that still crashes
e.g.

_length(100)

Code: Select all

#ifdef KNX_COM_OBJ_SUPPORT_ALL_PRIORITIES
KnxComObject::KnxComObject(word addr, e_KnxDPT_ID dptId, e_KnxPriority prio, byte indicator )
: _addr(addr), _dptId(dptId), _indicator(indicator), _length(100), _prio(prio)
#else
KnxComObject::KnxComObject(word addr, e_KnxDPT_ID dptId, byte indicator )
: _addr(addr), _dptId(dptId), _indicator(indicator), _length(100)
#endif
{
	if (_length <= 2) _longValue = NULL; // short value case
	else { // long value case
		_longValue = (byte *) malloc(_length-1);
		for (byte i=0; i <_length-1 ; i++) _longValue[i] = 0;
	}  
	if (_indicator & KNX_COM_OBJ_I_INDICATOR) _validity = false; // case of object with "InitRead" indicator
	else _validity = true; // case of object without "InitRead" indicator
}

kostbill
Posts: 51
Joined: Mon Aug 07, 2017 7:56 am

Re: Program crashes with call to malloc.

Post by kostbill » Wed Aug 09, 2017 10:29 am

Thanks I will try again today after work.

Bill.

kostbill
Posts: 51
Joined: Mon Aug 07, 2017 7:56 am

Re: Program crashes with call to malloc.

Post by kostbill » Wed Aug 09, 2017 8:14 pm

What I see today is different from what I was seeing yesterday.
Perhaps I didn't notice yesterday.

When I add the library in the project (Sloeber menu->Arduino->Add a library to the selected project) it crashes (or it doesn't even run, cannot tell).

I am doing nothing else, I am not calling anything or even creating Objects the start of the file, not even including files from the library. I am just adding the library.

If I remove the library, it works.

Has anyone seen something like that?

If I add another library, for example the Wire library, it doesn't have any problems.

Thanks,
Bill.

Post Reply