Announcements

Help us improve the Power & Sensing Selection Guide. Share feedback

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
MaFa_974161
Level 5
Level 5
100 sign-ins 50 replies posted 50 questions asked

Hello,

I'm using WICED STUDIO 6.6. and CYW943907AEVAL1F.
I write a code that create some threads and these threads call malloc (and obviously subsequent free).

My problem is that after time during execution (this time is random) malloc returns me NULL

and the processor resets.

 

I checked so many times if malloc is followed by properly by a free and I found a correctly use.

 

I have the dubt that malloc is not thread safe: I mean that if I call a malloc in a thread and another thread is executing a malloc this can lead a crash.

 

Can someone help me ?

0 Likes
1 Solution
Rakesh_BG
Moderator
Moderator
Moderator
50 solutions authored 100 replies posted 100 sign-ins

Hi,

I would suggest you to use wiced malloc debug function available.

The malloc debug functions in WICED allow the following:

  • memory corruption detection
  • record of maximum and current memory usage
  • memory leak detection

Allocated memory blocks are identified with a string name, a owner thread id, and a calling function address

You can find the malloc_debug.h file from the path in WICED:

43xxx_Wi-Fi/libraries/test/malloc_debug/malloc_debug.h

Please enable WICED_ENABLE_MALLOC_DEBUG to extend the malloc definitions to the functions defined in the malloc_debug.c file.

Add "test/malloc_debug" component in your application makefile to enable malloc_debug

Example:
$(NAME)_COMPONENTS := protocols/HTTP_client \
test/malloc_debug

To enable print logs on dynamic memory allocation and free, make sure following macro is active in malloc_debug.c

#define MALLOC_LOG(x) printf x

Use malloc_named() rather than malloc() if you wish to be able to identify memory blocks by name. (requires including malloc_debug.h)

 

Thanks,

Rakesh B G

View solution in original post

0 Likes
2 Replies
MaFa_974161
Level 5
Level 5
100 sign-ins 50 replies posted 50 questions asked

I have to apology: today I analyzed the code in a better way and I found a row where I forgotten to call free after malloc.
Now I understand why the processor resets.

 

Thanks to the log I saw that the pointer given from malloc increases by 0x40 every time I called malloc in my periodic routine.

0 Likes
Rakesh_BG
Moderator
Moderator
Moderator
50 solutions authored 100 replies posted 100 sign-ins

Hi,

I would suggest you to use wiced malloc debug function available.

The malloc debug functions in WICED allow the following:

  • memory corruption detection
  • record of maximum and current memory usage
  • memory leak detection

Allocated memory blocks are identified with a string name, a owner thread id, and a calling function address

You can find the malloc_debug.h file from the path in WICED:

43xxx_Wi-Fi/libraries/test/malloc_debug/malloc_debug.h

Please enable WICED_ENABLE_MALLOC_DEBUG to extend the malloc definitions to the functions defined in the malloc_debug.c file.

Add "test/malloc_debug" component in your application makefile to enable malloc_debug

Example:
$(NAME)_COMPONENTS := protocols/HTTP_client \
test/malloc_debug

To enable print logs on dynamic memory allocation and free, make sure following macro is active in malloc_debug.c

#define MALLOC_LOG(x) printf x

Use malloc_named() rather than malloc() if you wish to be able to identify memory blocks by name. (requires including malloc_debug.h)

 

Thanks,

Rakesh B G

0 Likes