"Insufficient memory to continue the execution of the program" exception

All I'm trying to do is open a registry subkey and read a value. Oddly, the same application works on other windows systems. I'm just concerned about this - want to make sure its not something in my application.

Mine is a winforms application developed using managed C++. Is there something wrong with my app?

Here's the code that gets the registry key -

// Check for Registry Key to make sure iscsi iqn number is set.

RegistryKey ^LocalMachine;

RegistryKey ^iqnKey;

bool keyfound =false;

try

{

// Specify the HKEY_LOCAL_MACHINE hive

LocalMachine = Registry::LocalMachine;

iqnKey = LocalMachine->OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\XXX\\Default");

// Request all values from the "Default Folder" key

array <String^> ^valueNames = iqnKey->GetValueNames();

// Enumerate the values

for (int i = 0; i < valueNames->Length; i++)

{

// Each value name is now represented by valueNamesIdea. The actual value is returned via the subkey's GetValue

// method like this:

String ^value =static_cast<String^>(iqnKey->GetValue(valueNamesIdea));

if ( strstr((char*)Marshal::StringToHGlobalAnsi(value).ToPointer(),"iqn") )

{

keyfound =true;

}

}

if (!keyfound)

{

MessageBox::Show("Application cannot function correctly as the XXX Registry Key was not found.","XXX Key Error", MessageBoxButtons::OK, MessageBoxIcon::Error);

return -1;

}

}

catch(Exception ^ex)

{

MessageBox::Show(ex->Message,"Exception Occurred", MessageBoxButtons::OK, MessageBoxIcon::Error);

return -1;

}

__finally

{

if (iqnKey) iqnKey->Close();

if (LocalMachine) LocalMachine->Close();

}

[3620 byte] By [Boulderdude] at [2007-12-24]
# 1

Difficult to tell based on what's given. What I sometimes do in this case is to stub suspect parts of the code to see if the problem goes away.

e.g.

change

if ( strstr((char*)Marshal::StringToHGlobalAnsi(value).ToPointer(), "iqn") )

to

if( false )

and let it loop.

BrianKramer at 2007-8-31 > top of Msdn Tech,Visual C++,Visual C++ General...
# 2
I'm sorry, i dont think I follow your suggestion. Do you mean to say, instead of checking to see if "iqn" appears in any value, do something if iqn doesnt appear?
Boulderdude at 2007-8-31 > top of Msdn Tech,Visual C++,Visual C++ General...
# 3
Yes, that worked. Thank Brian.
Boulderdude at 2007-8-31 > top of Msdn Tech,Visual C++,Visual C++ General...
# 4
Perhaps you were leaking memory with the Marshall utility function. I would use one of the String functions to do your substring search instead.
BrianKramer at 2007-8-31 > top of Msdn Tech,Visual C++,Visual C++ General...