Memory Leak while trying to get Subkeys from registry

P

Prashant

Hello All,
I am new to win32 programming,I was trying to get all the Subkeys
under any Key. I have written a C program for achieving this, I am
using WinNT Server .
In this program

*networkCardKeyList is a Global array

The memory allocated to the pointers in this array are freed in the
main function.

The problem that I am facing is that when Query function is
executing there is memory loss, I am not able to trace why is this
happening. Can anyone please help me.

The function is as follows:-

void Query(LPCTSTR path)
{


HKEY hKey;
TCHAR *achKey;
DWORD cbName;
TCHAR achClass[MAX_PATH] = TEXT("");
DWORD cchClassName = MAX_PATH; // size of class
string
DWORD cSubKeys=0; // number of
subkeys
DWORD cbMaxSubKey; // longest
subkey size
DWORD cchMaxClass; // longest class
string
DWORD cValues; // number of values
for key
DWORD cchMaxValue; // longest value
name
DWORD cbMaxValueData; // longest value
data
DWORD cbSecurityDescriptor; // size of security
descriptor
FILETIME ftLastWriteTime; // last write time

DWORD i;
DWORD retCode;
DWORD cchValue = MAX_VALUE_NAME;



/*
Opening the Key by name to get the values
*/

if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,path,0,KEY_READ,&hKey)!=
ERROR_SUCCESS)
{
printf("Error in Connection");
return ;
}

// Get the class name and the value count.
retCode = RegQueryInfoKey(
hKey, // key handle
achClass, // buffer for class name
&cchClassName, // size of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey size
&cchMaxClass, // longest class string
&cValues, // number of values for this key
&cchMaxValue, // longest value name
&cbMaxValueData, // longest value data
&cbSecurityDescriptor, // security descriptor
&ftLastWriteTime); // last write time

// Enumerate the subkeys, until RegEnumKeyEx fails.


if (cSubKeys)
{
for (i=0; i<cSubKeys; i++)
{
achKey=(TCHAR *)malloc(MAX_VALUE_NAME * sizeof(TCHAR));

cbName = MAX_KEY_LENGTH;
retCode = RegEnumKeyEx(hKey, i,
achKey,
&cbName,
NULL,
NULL,
NULL,
&ftLastWriteTime);

if (retCode == ERROR_SUCCESS)
{
networkCardKeyList=(TCHAR*)malloc(MAX_VALUE_NAME *
sizeof(TCHAR));
strcpy(networkCardKeyList,achKey);

if(achKey!=NULL){
free(achKey);
achKey=NULL;
}

}
if(achKey!=NULL){
free(achKey);
achKey=NULL;
}
}
}


RegCloseKey(hKey);


}


Thanks
Prashant Gairola
 
R

Richard Bos

I am new to win32 programming,I was trying to get all the Subkeys
under any Key. I have written a C program for achieving this, I am
using WinNT Server .

Reading from the registry is, thank heavens, confined to a single
platform only, and therefore off-topic here. Please ask about this in a
Micro$oft programming newsgroup.

Richard
 
J

jacob navia

Why do you think there is a memory leak?

You *are* leaving in your array newly allocated strings that hold
the new data. Is this a "memory leak"?

Yes, if you never free them. No if they are used elsewhere
and later freed.

A possible source of the memory leak can be that you assign to the global
array
a new set of pointers without freeing the OLD contents, if any.

For instance you call your function two times. The first, the array is
filled
with information for (say) 3 strings. If it is called
a second time, the program will overwrite the pointer values stored in the
global
array WITHOUT freeing the allocated contents. The memory allocated the
first time becomes inaccessible (a memory leak)

By the way, in your code:
strcpy(networkCardKeyList,achKey);

if(achKey!=NULL){
free(achKey);
achKey=NULL;
}
If achKey would be NULL your program would crash in
the strcpy call. The test (achKey != NULL) is either redundant or
should be done right after the malloc call.

Proposed fix:

Test if the global array contains data already. If it does, free
the memory first, before overwriting it.

jacob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top