TFobear
August 4th, 2010, 01:00 PM
Hello all,
I am getting a System.OutOfMemoryException in my client program after I make a call to a managed dll wrapper (the dll is mixed mode c++/cli), the client is VB.Net)
The offending dll function is:
String^ Toolkit::Twain::NextSourceName()
{
return gcnew String(::TWAIN_NextSourceName());
}
TWAIN_NextSourceName() returns a LPCSTR
It is not throwing the exception after this function is called, rather at the end of Form1's constructor (sub New)
MSDN says OutOfMemoryException can occur when .newobj or .newarr (is generated by gcnew)
Task Managed shows memory usage for the client is fairly low, when NextSourceName is called there is no spike in memory usage..
commenting out Toolkit.Twain.NextSourceName allows the program to run fine.
One last thing, when there is no more TWAIN sources to be found TWAIN_NextSourceName returns an empty null terminated string, so when no more sources are found it calls gcnew String(""); I dont see that being a problem though
*Edit*
There is another function that might be problematic:
const char * SystemStringToCSTR(System::String^ sz)
{
IntPtr hglobal = Marshal::StringToHGlobalAnsi(sz);
const char * ret = (char*)hglobal.ToPointer();
Marshal::FreeHGlobal(hglobal);
return ret;
}
Boolean Toolkit::Twain::GetDefaultSourceName(System::String ^pzName)
{
return (0 == ::TWAIN_GetDefaultSourceName((LPSTR)SystemStringToCSTR(pzName)) ? false : true);
}
The overall goal for me is to be able to reference native global functions TWAIN_NextSourceName and TWAIN_GetDefaultSourceName in a managed client.
my questions are:
1.) Do I want to use char * pzName as a parameter for GetDefaultSourceName and pass it to native TWAIN_GetDefaultSourceName using pin_ptr<char>?
2.) if I use char * pzName as a parameter when I call this function from VB.Net will it map to System::String?
3.) if above case is true should I change the previous functions signature to char * NextSourceName();?
I am getting a System.OutOfMemoryException in my client program after I make a call to a managed dll wrapper (the dll is mixed mode c++/cli), the client is VB.Net)
The offending dll function is:
String^ Toolkit::Twain::NextSourceName()
{
return gcnew String(::TWAIN_NextSourceName());
}
TWAIN_NextSourceName() returns a LPCSTR
It is not throwing the exception after this function is called, rather at the end of Form1's constructor (sub New)
MSDN says OutOfMemoryException can occur when .newobj or .newarr (is generated by gcnew)
Task Managed shows memory usage for the client is fairly low, when NextSourceName is called there is no spike in memory usage..
commenting out Toolkit.Twain.NextSourceName allows the program to run fine.
One last thing, when there is no more TWAIN sources to be found TWAIN_NextSourceName returns an empty null terminated string, so when no more sources are found it calls gcnew String(""); I dont see that being a problem though
*Edit*
There is another function that might be problematic:
const char * SystemStringToCSTR(System::String^ sz)
{
IntPtr hglobal = Marshal::StringToHGlobalAnsi(sz);
const char * ret = (char*)hglobal.ToPointer();
Marshal::FreeHGlobal(hglobal);
return ret;
}
Boolean Toolkit::Twain::GetDefaultSourceName(System::String ^pzName)
{
return (0 == ::TWAIN_GetDefaultSourceName((LPSTR)SystemStringToCSTR(pzName)) ? false : true);
}
The overall goal for me is to be able to reference native global functions TWAIN_NextSourceName and TWAIN_GetDefaultSourceName in a managed client.
my questions are:
1.) Do I want to use char * pzName as a parameter for GetDefaultSourceName and pass it to native TWAIN_GetDefaultSourceName using pin_ptr<char>?
2.) if I use char * pzName as a parameter when I call this function from VB.Net will it map to System::String?
3.) if above case is true should I change the previous functions signature to char * NextSourceName();?