Helpful ReplyHot!PIC32MK dynamic allocation -> malloc issue

Author
hugo.g
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2019/04/11 05:04:34
  • Location: 0
  • Status: offline
2019/04/11 07:53:44 (permalink)
0

PIC32MK dynamic allocation -> malloc issue

Hi everyone !
 
I face to an issue when i use malloc in my code, i try to use __pic32_alloc_coherent (sizeof(mystruct)) but the issue still here !
The simple code:
 char * ptr;
    ptr = (ptr*) malloc(sizeof(ptr));
 Does'nt run correctly, debug crash at malloc ! 
 
I already set the  heap size  (xc32-ld menu) to 1024 bytes !
 
Is it possible to use dynamic allocation with PIC32MK1024GPE ?
 
I work on MPLAB X IDE v5.10
 
Thanks !
 
#1
rodims
Super Member
  • Total Posts : 1466
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: PIC32MK dynamic allocation -> malloc issue 2019/04/11 08:08:28 (permalink)
+1 (1)
What is a "debug crash at malloc" ?
#2
Mysil
Super Member
  • Total Posts : 3277
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: PIC32MK dynamic allocation -> malloc issue 2019/04/11 08:36:40 (permalink)
+2 (2)
Hi,
Firstly, calling    malloc(...)
have no guarantee that t will succeed.
 
If the dynamic allocation is not successfull, it will return a NULL pointer,
so after allocation you have to test the returned pointer.
    if (ptr == NULL)
    {   Nop();   /* Make error handling code here.  */ }  

If you use a invalid pointer, there will be a invalid address exception.
 
    Mysil
#3
NKurzman
A Guy on the Net
  • Total Posts : 17127
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: PIC32MK dynamic allocation -> malloc issue 2019/04/11 09:25:49 (permalink)
+1 (1)
ptr = (ptr*) malloc(sizeof(ptr));
you are getting Space for the sizeof the pointer? 4 bytes?
1K is a pretty small heap.
it is crashing during the malloc call?
If Not what does ptr have in it?
#4
andersm
Super Member
  • Total Posts : 2563
  • Reward points : 0
  • Joined: 2012/10/07 14:57:44
  • Location: 0
  • Status: offline
Re: PIC32MK dynamic allocation -> malloc issue 2019/04/11 09:41:23 (permalink)
+3 (3)
The code in the origial post doesn't even compile (ptr is not a type, you can't use it in a typecast). If you want any useful answers, post the actual code.
#5
hugo.g
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2019/04/11 05:04:34
  • Location: 0
  • Status: offline
Re: PIC32MK dynamic allocation -> malloc issue 2019/04/11 23:21:51 (permalink)
0
Hi !
 
Sorry, i want to apologize, i did'nt copy correctly my code !
 
So, this is the original code:
 
There is a structure :
 
typedef struct trame trame;
struct trame
{
int indicetrame;
CANRxMessageBuffer *donnees;
struct trame *suiv;
struct trame *prec;

};
 
 
int main() {

struct trame *ptr;
ptr= (trame*) malloc(sizeof(trame));
if (ptr == NULL)
{ exit(0); }
....
....
  return (EXIT_SUCCESS);
}
 
And this is, what i call the "debug crash", it happens during malloc: 
 
Runtime exception @ PC address 0xbfc00000. Function and line number unavailable.    at   : 0
 
Thanks !
#6
rodims
Super Member
  • Total Posts : 1466
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: PIC32MK dynamic allocation -> malloc issue 2019/04/12 05:49:48 (permalink)
0
malloc should never crash that way. 
Replace that
   ptr= (trame*) malloc(sizeof(trame));
with
  ptr= (trame*) malloc(16);
any change ?
Do you run simulator or PIC ?
To be sure, check your heapsize in your map file

Dynamic Data-Memory Reservation
section                    address  length [bytes]      (dec)  Description
-------                 ----------  -------------------------  -----------
heap                    0xa0000008         0x400        1024  Reserved for heap
stack                   0xa0000030         0x1fc0        8128  Reserved for stack

 
How do you test ?
This code
if (ptr == NULL)
{ exit(0); }
....
....
  return (EXIT_SUCCESS);

does not make sense in an embedded app. You cannot exit or return to something meaningful. If you are not stepping/debugging correctly this might give you some additional headache or crashes. For testing change it to
if (ptr == NULL)
{ while (1); }
....
...
while(1);
} // end main

 
 
#7
hugo.g
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2019/04/11 05:04:34
  • Location: 0
  • Status: offline
Re: PIC32MK dynamic allocation -> malloc issue 2019/04/15 00:04:00 (permalink)
0
Hi ! 
So i replaced ptr= (trame*) malloc(sizeof(trame)); with ptr= (trame*) malloc(16);
 
Also : 
if (ptr == NULL)
{ exit(0); }          with
if (ptr == NULL) { while (1); } 
 
Unfortunately i still have the same issue and i don't understand why !
 
I debug with my pic32MK ---> Issues:  
 
Call stack Window:
 
Runtime exception @ PC address 0xbfc00000. Function and line number unavailable.    at   : 0
 
 Debugger Console:
 
PIC32 runtime exception detected. Use Call Stack window to resolve source line location.
 
I setted the  heap size  (xc32-ld menu) to 512 bytes !
 
My memory map:  
 
Dynamic Data-Memory Reservation
section      address              length [bytes]                   (dec) Description
-------       ----------            -------------------------        -----------
heap        0x80000c98           0x200                            512 Reserved for heap
stack       0x80000eb0            0x3f148                        258376 Reserved for stack
 
I may have forgotten to code something explicity about the Kseg0 or Kseg1 to allow dynamic allocation ?
 
 
Memory Configuration
Name    Origin                                                         Length                    Attributes
kseg0_   program_mem      0x9d000000                   0x00100000               xr
kseg0_   boot_mem            0x9fc20490                    0x00000000
debug_   exec_mem           0x9fc20490                    0x00003b20
kseg1_   boot_mem            0xbfc20000                    0x00000490
kseg1_  boot_mem_4B0      0xbfc004b0                    0x00003b00
 
Thanks ! 
 
 
 
post edited by hugo.g - 2019/04/15 00:06:12
#8
rodims
Super Member
  • Total Posts : 1466
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: PIC32MK dynamic allocation -> malloc issue 2019/04/15 04:55:33 (permalink)
0
No idea.
I would create a small XC32 project in MpLabX  (File / New Project) and in the main function do nothing more than e.g.
 
 
char * ptr = malloc(100);
* ptr = 42;
while (1);
 
Step through it with the debugger and a) your PIC, b) the simulator.
 
#9
hugo.g
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2019/04/11 05:04:34
  • Location: 0
  • Status: offline
Re: PIC32MK dynamic allocation -> malloc issue 2019/04/15 05:09:15 (permalink)
0
It's the same..
 
I created a new project with a simple main -> 
 
char * ptr = malloc(100);
* ptr = 42;
while (1);
 
I setted the  heap size  (xc32-ld menu) to 512 bytes but in the call stack window, i have ->
 
Runtime exception @ PC address 0xbfc00000. Function and line number unavailable.    at   : 0
#10
rodims
Super Member
  • Total Posts : 1466
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: PIC32MK dynamic allocation -> malloc issue 2019/04/15 05:13:10 (permalink)
0
Still no idea.
Did you debug with the simulator ?
Do you have another xc32 installation to test with ?
#11
JPortici
Super Member
  • Total Posts : 622
  • Reward points : 0
  • Joined: 2012/11/17 06:27:45
  • Location: Grappaland
  • Status: online
Re: PIC32MK dynamic allocation -> malloc issue 2019/04/15 06:36:31 (permalink)
0
hugo.g
It's the same..
 
I created a new project with a simple main -> 
 
char * ptr = malloc(100);
* ptr = 42;
while (1);
 
I setted the  heap size  (xc32-ld menu) to 512 bytes but in the call stack window, i have ->
 
Runtime exception @ PC address 0xbfc00000. Function and line number unavailable.    at   : 0




are you somehow unable to post the complete code?
 
I tried in the simulator, as i have the 32MK devboard at home but the following code didn't show any issue in the simulator.
#include <xc.h>
 
int main(void) {
  char *ptr = malloc(100);
  *ptr = 42;
 
  while(1) {
  }
  return 0;
}

 
(this is what people expect when they ask you to post the complete code.)
#12
hugo.g
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2019/04/11 05:04:34
  • Location: 0
  • Status: offline
Re: PIC32MK dynamic allocation -> malloc issue 2019/04/15 06:51:48 (permalink)
0
Maybe i wasn't explicit, but the following code works perfectly with the simumlator ! 
#include <xc.h>
 
int main(void) {
  char *ptr = malloc(100);
  *ptr = 42;
 
  while(1) {
  }
  return 0;
}
 It is the same with the previous code..
 
But when i debug with my target pic32MK i have these Runtime exception issues !
#13
JPortici
Super Member
  • Total Posts : 622
  • Reward points : 0
  • Joined: 2012/11/17 06:27:45
  • Location: Grappaland
  • Status: online
Re: PIC32MK dynamic allocation -> malloc issue 2019/04/15 11:19:55 (permalink) ☄ Helpfulby hugo.g 2019/04/16 04:43:58
+3 (3)
Yes, be more explicit in the future, you'll get better answers faster :)
 
I've tested and the code doesn't work on a real device, until i set up the configuration bits.
Usually the default state should be enough to get something out of the controller.. but apparently it's not the case for the PIC32MK
 
Disabling the Dead Man Timer made it possible to excecute the code
 
Things to do when you have resets: Find the cause of the reset.
-Use the general exception handler, so the code will go there instead of just resetting
-Use all trap exception handlers, so the code will go there instead of just resetting
-In a debug build at the moment of the reset the excecution will stop at the reset location (before the startup handler) so that you can inspect with memory watch the content of RCON,the interrupt controller and others
 
But in this case.. There is an errata on the DMT
"The Deadman Timer module does not cause a Non-maskable Interrupt (NMI) on a BAD1, BAD2,or DMTEVENT."
So i think you will get a reset without getting the cause for it... or at least this is my interpretation of what's happening... Because this is happening, a reset without an apparent cause unless i disable the DMT.
I never used the DMT, which is why i never stumbled upon the issue before.
 
#14
hugo.g
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2019/04/11 05:04:34
  • Location: 0
  • Status: offline
Re: PIC32MK dynamic allocation -> malloc issue 2019/04/16 04:52:10 (permalink)
0
 
Indeed there is a reset, I didn't pay enough attention..
 
In order to solve the issue, as you specify, it is necessary to disable the DMT and the WDT, my problem is solved !
 
Thank you everyone ! 
#15
mlp
boots too small
  • Total Posts : 709
  • Reward points : 0
  • Joined: 2012/09/10 15:12:07
  • Location: previously Microchip XC8 team
  • Status: offline
Re: PIC32MK dynamic allocation -> malloc issue 2019/04/16 09:36:03 (permalink)
0
By the way, never cast the result of a call to malloc(), calloc(), or realloc().
It is unnecessary and can mask the error of not having a valid prototype in scope.

Mark (this opinion available for hire)
#16
Jump to:
© 2019 APG vNext Commercial Version 4.5