Pages: [1]   Go Down
Author Topic: Dynamic memory allocation in Audio library  (Read 4099 times)
0 Members and 1 Guest are viewing this topic.
Nice, France
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I noticed this in arduino-1.5\hardware\arduino\sam\libraries\Audio\Audio.cpp

Code:
buffer = (uint32_t *) malloc(bufferSize * sizeof(uint32_t));

Sadly, memory allocation has bugs at present. Boilerplate:

Please note that, at present, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

Does this use of malloc imply that Audio will have he same issues that the use of the String clas has at present? Or does the C compiler and library used on the SAM architecture not have those issues? (And if not, does that mean String is also safe to use, on Due?)
Logged

Forum Administrator
Milano, Italy
Offline Offline
Sr. Member
*****
Karma: 23
Posts: 292
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I didn't make deep tests about malloc to verify the fragmentation problem, so i don't know if we'll have the same String issues.
What i can say for sure is that in many months of tests i didn't had any issue with it.

In any case, Audio library does only one allocation at the moment of instantiation of the Audio object, so it should be safe even with a buggy malloc.
C
Logged

C.

Offline Offline
God Member
*****
Karma: 32
Posts: 830
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

In any case, it isn't malloc() that is the problem in the AVR libs, it's free(). So if you malloc(), but never free(), there shouldn't be issues.
Logged

WiFi shields/Yun too expensive? Embeddedcoolness.com is now selling the RFXduino nRF24L01+ <-> TCP/IP Linux gateway: Simpler, more affordable, and even more powerful wireless Internet connectivity for *all* your Arduino projects! (nRF24L01+ shield and dev board kits available too.)

Nice, France
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

C, pico, thanks for the responses.

The Audio library does use free:

Code:
void AudioClass::end() {
dac->end();
free( buffer);
}

although it is not clear to me if that destructor is ever called, due to the setup+infinite-loop Arduino program structure.
Logged

Forum Administrator
Milano, Italy
Offline Offline
Sr. Member
*****
Karma: 23
Posts: 292
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Strictly speaking, this is not the destructor, but a method called "end", so you can call it explicitly using

Code:
audio.end();

in your sketch.
Logged

C.

Pages: [1]   Go Up
Jump to: