News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Unknown memory leak

Started by Stifado, October 30, 2007, 03:10:12 PM

Previous topic - Next topic

Tedd

Don't be rude to other members - particularly when they're trying to help. Understand that many members do not have English as their first language, so it's sometimes difficult to get their point across - don't mock them.

Unfortunately, six_L is getting mixed up with tcp socket details, while you're using udp.

As for your code - I can't seem to see anything wrong. The stack's balanced, there's nothing weird going on, resources appear to be cleared up.. Each time you receive a new message, a new thread is created which will increase the memory usage a little, but it's cleaned up again afterwards -- this even coming from program manager, which has a habit of over-estimating memory usage. Also keep in mind that reclamation is done lazily, so the usage won't necessarily drop immediately - try minimizing the window and watch the memory suddenly get reclaimed.
No snowflake in an avalanche feels responsible.

Mark Jones

Indeed Tedd, if you display a string in an edit control, windows uses a chunk of allocated memory outside of program space to hold the working copy of that string. If you then remove that edit control, the memory "used" is still marked by TaskMgr as "taken" but in reality, it is free to be overwritten at any time, like a GlobalAlloc chunk which is "freed." Perhaps something along these lines is what is happening here -- windows is allocating temporary memory which TaskMgr is assuming is all taken, but in reality it is just cached temporary memory.

I could try tracing through the calls to determine if this is the case, but it is a lot of work... and why should we help anyone who is not on their best behavior? It is interesting, how much more helpful people can be when you are nice to them.
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Stifado

Hey, I don't remember been rude to anyone here. My native language is not English too (as you should have guessed already). So, maybe the way I express my thoughts is kinda wierd, thus this misunderstadning. I always try to enrich my speech with the sense of humour, and that's why you may find some of my expressions grin... I'm not trying to project that image, of a smart ass. (Not sure about this sentence, syntactically :P)

Now,Tedd. You said that six_L has mixed TCP sockets in this discussion... I can't say that for sure, because I don't know much about sockets but, I beleive you.

Also, I beleive that he hasn't got any bad intesnsion by having a look at my code, and posting it back (changing it to the way he thought that it would be right). So, I've learned about WM_USER message...

While I was looking at his code, I found some things that I thought he might be interested in hearing. I don't think proposals are bad...

...while on the other hand posts like:

Quote
Posted by: evlncrn8
tried debugging your code then?
C' mon! You gotta be kiddin' me, right?

BUT, as far as I remember, I didn't hesitate to answer back, and of course, not in a bad way.

Quote
Each time you receive a new message, a new thread is created which will increase the memory usage a little, but it's cleaned up again afterwards -- this even coming from program manager, which has a habit of over-estimating memory usage. Also keep in mind that reclamation is done lazily, so the usage won't necessarily drop immediately - try minimizing the window and watch the memory suddenly get reclaimed.
That explains (fairly) everything. Though, I have tried minimizing the window, nothing happened. But I guess when the time comes (a need for memory), the "system" will "set it free" (those resources). Thanks for the information.

Hey there, Mark Jones. That was supposed to be braininess? :clap:

six_L

hey,Stifado
if udp_1.exe can't work, let me know.

server UDP port: 8889
msg limit: 1024 Bytes
...
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ServerRecvData proc hRecvSocket
local @dwRecv,@dwStructSize
local RecvSin:sockaddr_in

mov @dwStructSize,sizeof sockaddr_in
invoke RtlZeroMemory,addr RecvBuf,sizeof RecvBuf
invoke RtlZeroMemory,addr RecvSin,sizeof RecvSin
invoke recvfrom,hRecvSocket,addr RecvBuf,sizeof RecvBuf,0,addr RecvSin,addr @dwStructSize
.if eax == SOCKET_ERROR
invoke  MessageBeep,MB_ICONHAND
invoke SendMessage, hWndStat, SB_SETTEXT, 1, S("Error: server Recv failed!")
.else
invoke inet_ntoa,dword ptr [RecvSin.sin_addr]
invoke Add2Recv,addr RecvBuf,EAX
.endif
ret

ServerRecvData endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ClientSendData proc

invoke  RtlZeroMemory,addr SendBuf,sizeof SendBuf
invoke GetDlgItemText, hMain, ID_SENDMSG, offset SendBuf, sizeof SendBuf
        .if eax != 0
invoke socket, AF_INET, SOCK_DGRAM, IPPROTO_UDP
.if eax != INVALID_SOCKET
mov hClientSocket, eax
invoke WSAAsyncSelect,hClientSocket,hMain,WM_SOCKET,FD_WRITE
.else
invoke  MessageBeep,MB_ICONHAND
invoke SendMessage, hWndStat, SB_SETTEXT, 1, S("Error: Client socket failed!")
.endif
        .else
invoke  MessageBeep,MB_ICONHAND
invoke SendMessage, hWndStat, SB_SETTEXT, 1, S("Error: Message is empty!")
        .endif
ret

ClientSendData endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ClientThread proc

invoke GetDlgItemText, hMain, ID_SENDIP, offset szTargetIP, sizeof szTargetIP
.if eax!=0
invoke GetDlgItemText, hMain, ID_SENDPORT, offset szPort, sizeof szPort
.if eax!=0
invoke atodw,offset szPort
invoke htons, eax
mov UdpSendAddr.sin_port, ax
mov UdpSendAddr.sin_family, AF_INET

invoke inet_addr, offset szTargetIP

.if (eax != INADDR_NONE) && (eax != 0)
mov UdpSendAddr.sin_addr, eax
invoke ClientSendData
.else
invoke gethostbyname, offset szTargetIP
.if eax != 0
mov eax, dword ptr [eax+12]
mov eax, dword ptr [eax]
mov eax, dword ptr [eax]
mov UdpSendAddr.sin_addr, eax
invoke ClientSendData
.else
invoke  MessageBeep,MB_ICONHAND
invoke SendMessage, hWndStat, SB_SETTEXT, 1, S("Error: Invalid IPv4 address!")
.endif
.endif
.else
invoke  MessageBeep,MB_ICONHAND
invoke SendMessage, hWndStat, SB_SETTEXT, 1, S("Error: No Port specified!")
.endif
.else
invoke  MessageBeep,MB_ICONHAND
invoke SendMessage, hWndStat, SB_SETTEXT, 1, S("Error: No IPv4 address specified!")
.endif

ret
ClientThread endp

...
.if uMsg == WM_CREATE
...
invoke socket,AF_INET,SOCK_DGRAM,0
.if eax!=0
mov hServerSocket,eax
invoke WSAAsyncSelect,hServerSocket,hWnd,WM_SOCKET,FD_READ

invoke RtlZeroMemory,addr UdpRecvAddr,sizeof UdpRecvAddr
invoke htons,UDP_PORT
mov UdpRecvAddr.sin_port,ax
mov UdpRecvAddr.sin_family,AF_INET
mov UdpRecvAddr.sin_addr,INADDR_ANY
invoke bind,hServerSocket,addr UdpRecvAddr,sizeof UdpRecvAddr
.if eax == SOCKET_ERROR
invoke  MessageBeep,MB_ICONHAND
invoke SendMessage, hWndStat, SB_SETTEXT, 1, S("Error: Failed on bind !")
jmp @Exit
.endif
.else
invoke  MessageBeep,MB_ICONHAND
invoke SendMessage, hWndStat, SB_SETTEXT, 1, S("Error: Failed on socket!!")
.endif

...
.elseif uMsg==WM_COMMAND
mov eax,wParam
.if eax==ID_SEND
invoke CreateThread, 0, 0, addr ClientThread, 0, 0, 0
invoke CloseHandle, eax

...
.elseif uMsg == WM_SOCKET
mov eax,lParam
.if ax == FD_READ ; for server
invoke ServerRecvData,wParam
.elseif ax == FD_WRITE ; for client

invoke lstrlen,addr SendBuf
invoke sendto, wParam, addr SendBuf, eax, 0, addr UdpSendAddr, sizeof sockaddr_in
.if eax == SOCKET_ERROR
invoke  MessageBeep,MB_ICONHAND
invoke WSAGetLastError
.if eax == WSAEHOSTUNREACH
invoke SendMessage, hWndStat, SB_SETTEXT, 1, S("Error: Destination unreachable!")
.elseif eax == WSAEADDRNOTAVAIL
invoke SendMessage, hWndStat, SB_SETTEXT, 1, S("Error: Address not available!")
.else
invoke SendMessage, hWndStat, SB_SETTEXT, 1, S("Error: Unknown!")
.endif
.else
invoke inet_ntoa,dword ptr [UdpSendAddr.sin_addr]
invoke Add2Send,addr SendBuf,EAX
invoke SendMessage, hWndStat, SB_SETTEXT, 1, S("OK: Message sent!")
.endif
invoke closesocket,wParam
.endif

the attachment is exe




[attachment deleted by admin]
regards