Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

What does the function (_mlock) do? I don't call the function manually. I found that many functions (new, _mbschr,_mbscmp,etc) will use it. I don't know why my program gives the error (runn time error).

I'm going to create an FTP where a client will open a file and read a date and send data toward to the server. The server opens a file and writes that data to the file but when the server goes to open the file it will get a run time error.

void reciveMsg(unsigned int av_sock);
void reciveFile(unsigned int av_sock);
void reciveMsg(unsigned int av_sock)
{
	int lv_ret;
	char			*lp_buff		= NULL;
		
	while(1)
	{
		lp_buff = (char *)malloc(sizeof(513));
		memset(lp_buff,0,512);
		lv_ret = recv(av_sock,lp_buff,512,0);
		printf("\n%s",lp_buff);
		delete(lp_buff);
		if(lv_ret == -1)
		{
			closesocket(av_sock);
			FD_CLR(av_sock, &master);
			printf("\nSocket closed");
			return;
		}
	}
}
void reciveFile(unsigned int av_sock)
{
	FILE		*fp		= NULL;
	int		lv_ret;
	char		*lp_buff	= NULL;
	char		lv_bufer[512]	= {""}; "//added for color correction
	int		lv_handle	= -1;
	
	
	lv_handle = _open("D:\\xyz\\test.txt", _O_RDWR);
	
	 if(lv_handle == -1)
	 {
		printf("\nError In Opening File");
		return;
	 }
	while(1)
	{
		lp_buff = (char *)malloc(sizeof(1025));
		memset(lp_buff,0,1024);
		lv_ret = recv(av_sock,lp_buff,1024,0);
		lv_ret = _write(lv_handle,lp_buff,sizeof(lp_buff));
		if(lv_ret != sizeof(lp_buff))
		{
			printf("\nTotal Bytes Write [%ld] ",lv_ret);
			return;
		}
		printf("\nWriting In File Please Wait");
	}
	_close(lv_handle); 

}


[Added a double-quote to color-correct the code]
Posted
Updated 30-Dec-10 6:14am
v7
Comments
Sandeep Mewara 30-Dec-10 1:39am    
What error?
Igor Kushnarev 30-Dec-10 2:42am    
It's the synchronization func. But is your question about this func or an exception? It's hard to answer the question without details. Any info about exception? Any code?
Slacker007 30-Dec-10 6:19am    
Edited for spelling and grammar.
Richard MacCutchan 30-Dec-10 8:05am    
Do NOT use delete() to release a buffer created by malloc(). Incidentally you are not allocating what you think; sizeof(513) returns 4 not 513.
Aescleal 30-Dec-10 11:37am    
Changed the tag to C rather than C++ - the only C++ thing was used incorrectly. If you actually used C++ then you wouldn't need to litter the code with sizeofs and get all of them wrong.

1 solution

lp_buff = (char *)malloc(sizeof(1025)); 
allocates 4 bytes of memory, sizeof(1025) is the same as sizeof(int). 

The next statement: memset(lp_buff,0,1024); sets 1024 bytes to zero, this is probably corrupting your heap.

Regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Slacker007 30-Dec-10 6:20am    
Edited for readability (code block).

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