I have been experiencing a memory leak in WSOCK32.DLL using
gethostbyname(). The same problem does not happen in the 16bit
version, just the 32bit version (WIN32).
Below is example code which, on my machine and others, reproduces the
problem every time in Windows95.
My system is:
P200
64 meg ram
6 gig hard disk
Windows 95 (4.00.950a) All updates installed.
Borland C++ (5.0a)
I have reported this to Microsoft, but have had no responce as of yet
(It's only been a week). Can someone else compile and test this out
for me, as I do not know if this is a Borland or Microsoft problem.
Compile as a WIN32 app.
Any comments or solutions are welcome. Thanks.
// -------------- cut ----------------------
/****************************************************************************
test.rc
produced by Borland Resource Workshop
*****************************************************************************/
#include "test.h"
IDD_DIALOG1 DIALOG 0, 0, 240, 83
STYLE DS_MODALFRAME | DS_3DLOOK | DS_FIXEDSYS | DS_CONTEXTHELP |
WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CLASS "MyDlgClass"
CAPTION "Memory Chomper!"
FONT 8, "Fixedsys"
{
CONTROL "OK", IDOK, "BUTTON", BS_PUSHBUTTON | BS_CENTER | WS_CHILD |
WS_VISIBLE | WS_TABSTOP, 186, 44, 50, 14
CONTROL "Cancel", IDCANCEL, "BUTTON", BS_PUSHBUTTON | BS_CENTER |
WS_CHILD | WS_VISIBLE | WS_TABSTOP, 186, 64, 50, 14
CONTROL "Memory Load:", -1, "static", SS_RIGHT | WS_CHILD |
WS_VISIBLE, 3, 9, 80, 8
CONTROL "Total Phys:", -1, "static", SS_RIGHT | WS_CHILD |
WS_VISIBLE, 3, 19, 80, 8
CONTROL "Avail Phys:", -1, "static", SS_RIGHT | WS_CHILD |
WS_VISIBLE, 3, 29, 80, 8
CONTROL "Total Page File:", -1, "static", SS_RIGHT | WS_CHILD |
WS_VISIBLE, 3, 39, 80, 8
CONTROL "Avail Page File:", -1, "static", SS_RIGHT | WS_CHILD |
WS_VISIBLE, 3, 49, 80, 8
CONTROL "Total Virtual:", -1, "static", SS_RIGHT | WS_CHILD |
WS_VISIBLE, 3, 59, 80, 8
CONTROL "Avail Virtual:", -1, "static", SS_RIGHT | WS_CHILD |
WS_VISIBLE, 3, 69, 80, 8
CONTROL "", MEMORYLOAD, "static", SS_LEFT | WS_CHILD | WS_VISIBLE,
85, 9, 40, 8
CONTROL "", AVAILPHYS, "static", SS_LEFT | WS_CHILD | WS_VISIBLE, 85,
29, 40, 8
CONTROL "", TOTALPHYS, "static", SS_LEFT | WS_CHILD | WS_VISIBLE, 85,
19, 40, 8
CONTROL "", TOTALPAGEFILE, "static", SS_LEFT | WS_CHILD | WS_VISIBLE,
85, 39, 40, 8
CONTROL "", TOTALVIRTUAL, "static", SS_LEFT | WS_CHILD | WS_VISIBLE,
85, 59, 40, 8
CONTROL "", AVAILPAGEFILE, "static", SS_LEFT | WS_CHILD | WS_VISIBLE,
85, 49, 40, 8
CONTROL "", AVAILVIRTUAL, "static", SS_LEFT | WS_CHILD | WS_VISIBLE,
85, 69, 40, 8
CONTROL "&On", IDC_ON, "button", BS_PUSHBUTTON | BS_CENTER | WS_CHILD
| WS_VISIBLE | WS_TABSTOP, 192, 18, 18, 14
CONTROL "O&ff", IDC_OFF, "button", BS_PUSHBUTTON | BS_CENTER |
WS_CHILD | WS_VISIBLE | WS_TABSTOP, 213, 18, 18, 14
CONTROL "GetHostByName", -1, "static", SS_CENTER | WS_CHILD |
WS_VISIBLE, 183, 8, 57, 8
}
// -------------- cut ----------------------
// test.h
#define IDD_DIALOG1 1
#define IDC_ON 101
#define IDC_OFF 102
#define MEMORYLOAD 200
#define TOTALPHYS 201
#define AVAILPHYS 202
#define TOTALPAGEFILE 203
#define AVAILPAGEFILE 204
#define TOTALVIRTUAL 205
#define AVAILVIRTUAL 206
// -------------- cut ----------------------
// test.c
// Mark Williston's gethostbyaddress memory chomper
// Copyright (c) 2001, John Doe
#include <windows.h>
#include <winsock.h>
#include <mem.h>
#include <stdio.h>
#include <string.h>
#include "test.h"
#define MY_TIMER 1
#define COMNAMLEN 32
HINSTANCE hInst;
HWND hDlgMain;
char computerName[COMNAMLEN];
struct hostent *pHostEnt;
MEMORYSTATUS MemStatus;
WSADATA wsaData;
int Switch=FALSE;
#pragma argsused
BOOL CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM
lParam)
{
int nRet;
switch(msg)
{
case WM_INITDIALOG:
hDlgMain = hDlg;
nRet = WSAStartup(MAKEWORD(1, 1), &wsaData);
if (nRet != 0)
{
MessageBox(0, "WSAStartup Error","WSOCK32", MB_OK);
SendMessage(hDlg, WM_COMMAND, IDCANCEL, 0);
return TRUE;
}
nRet=gethostname(computerName,COMNAMLEN);
if(nRet == SOCKET_ERROR)
{
MessageBox(0, "No Local Name","WSOCK32", MB_OK);
SendMessage(hDlg, WM_COMMAND, IDCANCEL, 0);
return TRUE;
}
// Decrease timer value to eat memory faster
SetTimer(hDlg, MY_TIMER, 10, NULL);
MemStatus.dwLength=sizeof(MEMORYSTATUS);
return TRUE;
case WM_TIMER:
switch(wParam)
{
case MY_TIMER:
if(Switch==TRUE)
// This is the trouble maker!
pHostEnt = gethostbyname(computerName);
GlobalMemoryStatus(&MemStatus);
SetDlgItemInt(hDlg,MEMORYLOAD,MemStatus.dwMemoryLoad,FALSE);
SetDlgItemInt(hDlg,TOTALPHYS,MemStatus.dwTotalPhys,FALSE);
SetDlgItemInt(hDlg,AVAILPHYS,MemStatus.dwAvailPhys,FALSE);
SetDlgItemInt(hDlg,TOTALPAGEFILE,MemStatus.dwTotalPageFile,FALSE);
SetDlgItemInt(hDlg,AVAILPAGEFILE,MemStatus.dwAvailPageFile,FALSE);
SetDlgItemInt(hDlg,TOTALVIRTUAL,MemStatus.dwTotalVirtual,FALSE);
SetDlgItemInt(hDlg,AVAILVIRTUAL,MemStatus.dwAvailVirtual,FALSE);
break;
}
break;
case WM_DESTROY:
return FALSE;
case WM_COMMAND:
switch(wParam)
{
case IDC_ON:
Switch=TRUE;
break;
case IDC_OFF:
Switch=FALSE;
break;
case IDOK:
case IDCANCEL:
nRet = WSACleanup();
if (nRet == SOCKET_ERROR)
MessageBox(0, "WSACleanup Error","WSOCK32", MB_OK);
EndDialog(hDlg,wParam);
return TRUE;
}
break;
}
return FALSE;
}
#pragma argsused
int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow )
{
WNDCLASS wc;
hInst = hInstance;
if(!hPrevInstance)
{
memset(&wc,0,sizeof(wc));
wc.hInstance = hInst;
wc.cbWndExtra = DLGWINDOWEXTRA;
wc.lpfnWndProc = DefDlgProc;
wc.hIcon = LoadIcon(hInst,IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "MyDlgClass";
RegisterClass(&wc);
}
DialogBox(hInst,MAKEINTRESOURCE(IDD_DIALOG1),NULL,MainDlgProc);
return 0;
}
// -------------- cut ----------------------
>I have been experiencing a memory leak in WSOCK32.DLL using
>gethostbyname(). The same problem does not happen in the 16bit
>version, just the 32bit version (WIN32).
Mark,
I compiled your program with Borland 5.02. One change had to
be made: DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), NULL,
(DLGPROC)MainDlgProc); IE: the cast to DLGPROC, and I don't understand why
that was needed. Anyway here's some data I collected in the order they
appear in you dialogbox:
Clean Boot, TaskBar w/sound icon and clock showing. Program started from
run dialog on TaskBar. Record Readings, Start GetHostByName.
20, 49762304, 30392320, 106201088, 106201088, 2143289344, 2136473600
Stop GetHostByName. Record Readings.
52, 49762304, 4096, 106201088, 104173568, 2143289344, 2102788096
Exit Program, Pause, Restart, Record Readings, Start GetHostByName.
20, 49762304, 30990336, 106196992, 106196992, 2143289344, 2136473600
Stop GetHostByName, Record Readings.
33, 49762304, 18575360, 106196992, 106196992, 2143289344, 2123759616
Exit Program, Pause, Restart Program, Record Readings.
20, 49762304, 30990336, 106196992, 106196992, 2143289344, 2136473600
End Test.
WSOCK32.DLL data: 66560 bytes, created: unknown, modified: tue july 11
1995, accessed: thur sep 25 1997, version 4.00.950.
Windows 95, one of the older production builds I think. Other software
that might pertain, AOL 32bit Hope That Helps, Br5an