[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [lwip-users] UDP server on LwIP socket falls into hard fault if inpu
From: |
Noam Weissman |
Subject: |
Re: [lwip-users] UDP server on LwIP socket falls into hard fault if input string longer than 19 bytes |
Date: |
Tue, 7 Jun 2016 10:18:54 +0000 |
Hi,
Why are you calling malloc inside the thread ? if you allocate a buffer once,
there is no need to use malloc
define it as an array.
malloc allocates memory from the micro's heap, not from the FreeRTOS heap.
Check that you heap has sufficient memory. Normally in an average STM example
the heap is
small around 0x200 == 512 bytes !
You have a few options instead of calling malloc:
1. allocate a static buffer outside of your thread and use it (global).
2. allocate an array inside your thread but you must set that thread, stack
size properly.
In option 2 all the none dynamic data inside the thread is allocation from the
FreeRTOS heap.
Hope the above helped in some way.
BR,
Noam.
-----Original Message-----
From: lwip-users [mailto:address@hidden On Behalf Of ????
Sent: Tuesday, June 07, 2016 12:13 PM
To: address@hidden
Subject: [lwip-users] UDP server on LwIP socket falls into hard fault if input
string longer than 19 bytes
Hello, people.
I need help in solving very annoying problem, which I cannot track. Here is my
question on stackoverflow.com
All works fine with short messages. But 20 bites and more knocks out uC into
hard fault. I think it because of NULL pointer exception. My embedded system
consists of STM32F4, FreeRTOS and LwIP. Core was taken from ST example. Here is
the code of thread of simple UDP server.
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include "lwip/sockets.h"
#include "./udpCLI.h"
//#include "Sledge.h" //for assert_amsg(), which print what and where fails
before stop execution
static void udpCLI_thread(void *arg)
{
int sockfd; // socket file descriptor
const uint16_t serverport = UDP_PORT_NUMBER_CLI;
struct sockaddr_in serveraddr = {0}, clientaddr = {0};
socklen_t addrlen;// = sizeof(struct sockaddr_in);
const uint16_t bufin_SIZE = 200, bufout_SIZE = 1024;
char *bufin = malloc(bufin_SIZE); assert_amsg(bufin != NULL); // stops
execution with printing message "bufin != NULL" in stdout/stderr
char *bufout = malloc(bufout_SIZE); assert_amsg(bufout != NULL);
LWIP_UNUSED_ARG(arg); //(void)arg;
debugf2( "udpCLI_thread"NEWLINE );
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
assert_msg("udpCLI_thread: ERROR opening socket", sockfd>=0);
/*
* build the server's Internet address
*/
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(serverport);
/*
* bind: associate the socket with a port
*/
if( bind(sockfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0 )
error("udpCLI_thread: ERROR on binding");
// main loop: wait for a datagram, then echo it
while(1)
{
addrlen = sizeof(struct sockaddr_in); // THIS DID NOT HELP
// recvfrom: receive a UDP datagram from a client
int n = recvfrom(sockfd, bufin, bufin_SIZE, /*flags*/0, (struct
sockaddr*)&clientaddr, &addrlen);
// handle rusult
//if (n < 0)
// future process and response
//....
}
}
Program control never returns from recvfrom() function if input message length
is more than 19 bytes. And never returns from function
sys_arch_mbox_fetch(&conn->recvmbox, &buf, 0); in function netconn_recv_data()
in file api_lib.c at line 371.
The same thing met for another example code.
Please help to resolve this problem.
This message has no MIME, only plain text.
--
Ivan Kuvaldin
_______________________________________________
lwip-users mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/lwip-users