Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
test1.c

struct mode {
unsigned int param1;
unsigned int param2;
char param3[255];
unsigned int param3;
}

void function(int param1,int param2 , char* param3, int paramn) {

  
    struct mode* mode;
    /* Allocate the memory */
    mode = (struct mode*)calloc(1, sizeof(struct mode));

   if (mode == NULL)
        exit(0);

    mode->param1 = param1;
    mode->param2 = param2;
    strcpy(mode->param3, param3);
    mode->paramn = paramn;

   sendtheData(mode)
 }

After sending the data i want to free the allocated memory.In order to avaoid Memory leak

test2.h
struct test2 {
unsigned int param1;
unsigned int param2;
char param3[255];
unsigned int param3;
}


test2.c

static struct test2 *test2 = null;
sendtheData(void *arg)
{
 test2 = (struct test2*)aArg;

}


What I have tried:

sendtheData(mode)
     free(mode) 


After free memory it gives me heap error in visual studio
Posted
Updated 21-Jul-21 1:54am
v9

Quote:
After free memory it gives me heap error in visual studio
This could also mean that you're writing into the memory after you successfully free it. It's impossible to tell without seeing all your code, and even then it would probably have to be debugged, which isn't something that anyone here is likely to do for you. In Visual Studio, you can set a "memory watch" on mode after you allocate it, and then see if it changes after you free it. This is described here[^].
 
Share this answer
 
v3
Comments
Greg Utas 19-Jul-21 13:39pm    
Despite some idiot's downvote, this is a possible explanation.
Richard MacCutchan 20-Jul-21 4:18am    
Countered.
Greg Utas 20-Jul-21 6:38am    
Thanks!
You can free the data only if the sendtheData function internally makes a copy of it.
Since you didn't post the sendtheData definition I'm sorry we cannot help further.
 
Share this answer
 
Comments
Member 14603643 19-Jul-21 5:44am    
void sendtheData (void * aArg)
{
hs = (struct resphs*)aArg;
}

I have the same structure like mode and this code is in another source file
CPallini 19-Jul-21 5:49am    
You see, the function doesn't make a copy, so you are not allowed to free it.
Do you have any documentation of the function?
Member 14603643 19-Jul-21 5:59am    
no , these is developed by me so dont have any docs . Can you advice me for the same
CPallini 19-Jul-21 6:06am    
Well, it would probably better if you show us the complete code, or, at least, you tell us what are you trying to accomplish.
Use free when you no longer access that memory. A better place would be AFTER sending it.

As CPallini hinted: make a copy in the sendtheData() function, when it sending is asyncron. Than delete that also after sending it as the original memory.
 
Share this answer
 
Comments
Member 14603643 19-Jul-21 7:57am    
What you said i understood but implmentation of sendtheData is in another source file where struct mode is not visible or accessible
KarstenK 19-Jul-21 13:46pm    
Then you need to make it visible by including the declaration header. I dont understand how than the sendtheData is working when it doenst know WHAT to send.
Member 14603643 20-Jul-21 2:16am    
sendtheData has function parameter as void parameter, and same structure is created in that file so the void pointer is assigned to struct resphs
Where are the declaration for
param3
?
 
Share this answer
 
Comments
Member 14603643 20-Jul-21 2:14am    
char buff[255];
steveb 20-Jul-21 7:19am    
It is not declared in your code. Also if it is char[] then you cannot use assignment operator. U need to use memcpy on param3
Member 14603643 20-Jul-21 12:03pm    
yes i have used strcpy(mode->param3, param3) as these are character array .
If the value you are passing into your function for the parameter
char* param3
is a string that is longer than 255 characters or it is not null terminated then this could be your problem.

I would suggest replacing the line
strcpy(mode->param3, param3);
with
strncpy(mode->param3, param3, 255);
mode->param3[254]='\0'

This will guard against this situation.
 
Share this answer
 
Comments
Member 14603643 21-Jul-21 10:26am    
will surely check and give the feedback thanks for the valuable feedback. One More Question: logic for test2.c sendtheData(void *Arg) is good or any advice to do it more better

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900