Page 1 of 1
Memory allocation problem
Posted: Thu 30. Sep 2021, 12:24
by Shail
Hello,
- Why we need to Crete request and response variable every function when we are creating and free in request and response at the end of function?
Since scope of local variable within function?
- We observed request and response address same in two different local function ? how it possible since two different variable in two different function?
- UDS_MsgFree_2013 function use heap section to store memory? UDS_MsgFree_2013 function how it allocate memory?
Code: Select all
uds_status status;
uds_msg request;
uds_msg response;
uds_msg confirmation;
memset(&request, 0, sizeof(uds_msg));
memset(&response, 0, sizeof(uds_msg));
memset(&confirmation, 0, sizeof(uds_msg));
// Free messages
status = UDS_MsgFree_2013(&request);
printf(" Free request message: %i\n", status);
status = UDS_MsgFree_2013(&response);
printf(" Free response message: %i\n", status);
status = UDS_MsgFree_2013(&confirmation);
printf(" Free confirmation message: %i\n", status);
Thanks !!
Re: Memory allocation problem
Posted: Thu 30. Sep 2021, 14:33
by K.Wagner
Hello,
-
Shail wrote: ↑Thu 30. Sep 2021, 12:24
Why we need to Crete request and response variable every function when we are creating and free in request and response at the end of function?
Since scope of local variable within function?
(I am not sure to understand the question. If the following answer does not fit, please formulate this in other manner.)
The data that can be contained in a uds_msg is hugh, so that this cannot just be generated in the stack. A UDS message record must be dynamically allocated for use and deallocated when it is no longer needed, regardless of whether it is used for request or response. For this reason the functions UDS_MSgAlloc_2013 UDS_MsgFree_2013 exist.
-
Shail wrote: ↑Thu 30. Sep 2021, 12:24
We observed request and response address same in two different local function ? how it possible since two different variable in two different function?
Without known what and how you are doing things it is difficult to say anything in concret, but two thnings come to my mind:
- Since the data for those messages is allocated dynamically (on the heap), then they are visible beyond function limits.
- Since messages are allocated in the heap, and if those functions are not being called in paralell, than it is possible that you are just seeing the same memory region be allocated, freed, and reallocated again. After memory is freed, it is available again for allocation from the same or different function.
-
Shail wrote: ↑Thu 30. Sep 2021, 12:24
UDS_MsgFree_2013 function use heap section to store memory? UDS_MsgFree_2013 function how it allocate memory?
This function frees memory, rather than allocate it.
-
Shail wrote: ↑Thu 30. Sep 2021, 12:24
CODE: SELECT ALL
uds_status status;
uds_msg request;
uds_msg response;
uds_msg confirmation;
memset(&request, 0, sizeof(uds_msg));
memset(&response, 0, sizeof(uds_msg));
memset(&confirmation, 0, sizeof(uds_msg));
// Free messages
status = UDS_MsgFree_2013(&request);
printf(" Free request message: %i\n", status);
status = UDS_MsgFree_2013(&response);
printf(" Free response message: %i\n", status);
status = UDS_MsgFree_2013(&confirmation);
Be careful, you are not allocating the messages explicitly and also not using any function that allocate those implicitly (UDS_Read for instance)
Re: Memory allocation problem
Posted: Fri 1. Oct 2021, 18:00
by Shail
Hello,
Thanks for reply.
-
Code: Select all
status = UDS_WaitForService_2013(channel, &request, &response, &confirmation);
Overriding by the the status of UDS_MsgFree_2013(&request); as mentioned below. Is it required to maintain status of API and buffer allocation differently?
- We are using uds different services so incase one service failed we are not proceeding further in such scenario how we can call APIs?
Is it require to maintain status of memory buffer different from status of APIs?
- Before we are using V1.0.0 and now we are using V2.0.1 we observed time difference of execution ? What can be reason for the same ? Is new stack reducing timing functionality?
Code: Select all
// Sends a physical AccessTimingParameter message
printf("\n\nSends a physical AccessTimingParameter message: \n");
status = UDS_SvcAccessTimingParameter_2013(channel, config, &request, PUDS_SVC_PARAM_ATP_RCATP, request_record, record_size);
if (UDS_StatusIsOk_2013(status, PUDS_STATUS_OK, false))
status = UDS_WaitForService_2013(channel, &request, &response, &confirmation);
printf(" UDS_SvcAccessTimingParameter_2013: %d\n", (int)status);
if (UDS_StatusIsOk_2013(status, PUDS_STATUS_OK, false))
display_uds_msg(&confirmation, &response, false);
else
display_uds_msg(&request, NULL, false);
// Free messages
status = UDS_MsgFree_2013(&request);
printf(" Free request message: %d\n", (int)status);
status = UDS_MsgFree_2013(&response);
printf(" Free response message: %d\n", (int)status);
status = UDS_MsgFree_2013(&confirmation);
printf(" Free confirmation message: %i\n", status);
Thanks
Re: Memory allocation problem
Posted: Mon 4. Oct 2021, 08:55
by K.Wagner
Hello,
- If you mean with "status" the "response buffer" in the code, well yes. When issuing any UDS_Srv*** function, the buffer will be stored internally and used for filling it with the UDS response, when received. If you literally mean the variable "status", then it is normally declared there where it is needed (where the funcitons are called). This as it is just a DWORD value returned by calling each API function. You just need to pay attention where you define this, if you are using the same code in a multi-threading solution.
- After successful initialization, a channel is ready to communicate with the connected CAN bus. The functions starting with UDS_Svc* can be then used to transmit UDS requests and the utility functions starting with UDS_WaitFor* are used to retrieve the results of a previous request. In this way, you should have blocks of UDS_Svc* and UDS_WaitFor* functions. For each of those blocks you should maintain the response/confirmation buffer; the service function gives the API the place holder information for the response/confirmation that will be allocated by the API. Since the API allocates memory, this buffer must be deallocated by calling the function UDS_MsgFree_2013. For isntance:
Code: Select all
uds_msg request;
uds_msg response;
uds_msg confirmation;
memset(&request, 0, sizeof(uds_msg));
memset(&response, 0, sizeof(uds_msg));
memset(&confirmation, 0, sizeof(uds_msg));
uds_status status = UDS_SvcDiagnosticSessionControl_2013(channel, config, &request, PUDS_SVC_PARAM_DSC_DS);
if (UDS_StatusIsOk_2013(status, PUDS_STATUS_OK, false))
status = UDS_WaitForService_2013(channel, &request, &response, &confirmation);
...
status = UDS_MsgFree_2013(&request);
status = UDS_MsgFree_2013(&response);
status = UDS_MsgFree_2013(&confirmation);
...
status = UDS_SvcDiagnosticSessionControl_2013(channel, config, &request, PUDS_SVC_PARAM_DSC_ECUPS);
if (UDS_StatusIsOk_2013(status, PUDS_STATUS_OK, false))
status = UDS_WaitForService_2013(channel, &request, &response, &confirmation);
...
status = UDS_MsgFree_2013(&request);
status = UDS_MsgFree_2013(&response);
status = UDS_MsgFree_2013(&confirmation);
... And so on...
- What do you mean? are we talking on microseconds, or milliseconds? is this timing measured globally or per API function? Note that the API v2.x includes CAN-FD and implies allocating hugh amount of memory. This could lead to some loss of performance in comparation with version 1.x. But, the timing of the API itself (timeouts, time used by API calls like UDS_Srv*, etc) should not vary. The API is conform to all timeouts specificaitons within the ISO norm.
Re: Memory allocation problem
Posted: Mon 25. Oct 2021, 08:11
by Shail
Hello,
2) We need to use UDS service one after another based on status of API.
If status failed we are not proceeding for the next UDS service.
-> For Example :
Code: Select all
status = uds_execute_diganostic_service ()
if(status == PUDS_STATUS_OK)
{
state = SUCESS;
// move to next service;
service = uds_programming ;
}
else
{
status = FAILED;
}
==========================================
uds_execute_diganostic_service()
{
uds_msg request;
uds_msg response;
uds_msg confirmation;
memset(&request, 0, sizeof(uds_msg));
memset(&response, 0, sizeof(uds_msg));
memset(&confirmation, 0, sizeof(uds_msg));
uds_status status = UDS_SvcDiagnosticSessionControl_2013(channel, config, &request, PUDS_SVC_PARAM_DSC_DS);
if (UDS_StatusIsOk_2013(status, PUDS_STATUS_OK, false))
status = UDS_WaitForService_2013(channel, &request, &response, &confirmation);
...
status = UDS_MsgFree_2013(&request);
status = UDS_MsgFree_2013(&response);
status = UDS_MsgFree_2013(&confirmation);
/////////////////////////////////////////////////
IMPORTANT : HERE RETURN STATUS OF UDS_WaitForService_2013 or STATUS = cvo_uds_display_message(Message, &MessageResponse);
// OVERRIDDEN by status of (UDS_MsgFree_2013) UDS API BUFFER SO IT"S PROBLEM inspite UDS SERVICE FAILED WE ARE GETTING PUDS_STATUS_OK
// WHICH leads to PROBLEM.
}
3) HOW to handle such scenario ?
* code formated by ADMIN
Re: Memory allocation problem
Posted: Mon 25. Oct 2021, 14:18
by K.Wagner
Hello,
I think I don't understand your question. Are you asking which value you should return from your function? If yes, well, it depends on your needs! It is your code, it is your program.
No one knows better than you what is needed in your program as yourself. If you see that a variable is being overriden, and this is in your opinion wrong, well, just change this to not be overriden. If you want your function to return an error when a service fails, well, just return the information you require. If your problem is the check of the UDS_MsgFree_2013 calls, well you can just throw an exception when this happen (since it should always work), or you can have more status returned, if needed (for instance, using parameters per references), and checking all conditions in your main code.
Please understand, that we give support for the API usage, not for programming, or for teaching the UDS protocol. These things are not in the scope of support we give with the APIs. This question is also out of topic. It has nothing to do with memory allocation. I will close this thread at the end of the day. If you need more assistence related to the API use, please create another thread.
Note on the sample code: If you are modifying and using part of the sample code delivered with the API with your own program, please note that this is "just sample code". Functions in these samples are normally written as "void", and if an error happen inside a function, then a message is just sent to the console output. Please don't await this code to work for any situation. This code is intended to show how to use the API, not how to write a program.
Thanks for your understanding.