L
Lcubed
I found a memory leak in my application and fixed a few minor things
after reading all the helpful posts and information on memory leaks.
However, there's still one left. Every time I p/invoke code from my
dll, tiny bits of memory (under 100 bytes) are not reclaimed. While
this does not seem like much, it would build up after a while.
Is there a chance it could be fragmentation? Does anyone have any
ideas? Any help or ideas appreciated.
Here's some typical problem area code boiled down :
[DllImport("MyDLL.dll", EntryPoint = "GetMyData", CharSet =
CharSet.Auto)]
unsafe static extern int GetMyData(ref DATASTRUCT data);
[DllImport("MyDLL.dll", EntryPoint = "SetMyData", CharSet =
CharSet.Auto)]
unsafe static extern bool SetMyData(ref DATASTRUCT data);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DATASTRUCT
{
public Int16 MyInt;
public bool MyBool;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 25)] public string
MyString;
}
unsafe static public bool UseGetMyData()
{
int sz = 0;
try
{
DATASTRUCT p = new DATASTRUCT();
if ((sz = GetMyData(ref p)) == CORRECT_DATA_SIZE )
{
// Data from DATASTRUCT p used or
// assigned to other variables here
}
else return false;
}
catch (Exception ex)
{
// error handling here
return false;
}
}
unsafe static public bool UseSetMyData()
{
try
{
DATASTRUCT p = new DATASTRUCT();
// DATASTRUCT p variables assigned here
return SetMyData(ref p);
}
catch (Exception ex)
{
// error handling here
return false;
}
}
// some C++ code for MyDLL
typedef struct
{
__int16 MyInt;
bool MyBool;
wchar_t MyString[25];
} DATASTRUCT;
static _declspec(dllexport) int GetMyData(DATASTRUCT * data);
static _declspec(dllexport) bool SetMyData(DATASTRUCT * data);
bool SetMyData(DATASTRUCT * data)
{
// data used to set other variables here
return true;
}
int GetMyData(DATASTRUCT * data)
{
// data populated here
return sizeof(data);
}
after reading all the helpful posts and information on memory leaks.
However, there's still one left. Every time I p/invoke code from my
dll, tiny bits of memory (under 100 bytes) are not reclaimed. While
this does not seem like much, it would build up after a while.
Is there a chance it could be fragmentation? Does anyone have any
ideas? Any help or ideas appreciated.
Here's some typical problem area code boiled down :
[DllImport("MyDLL.dll", EntryPoint = "GetMyData", CharSet =
CharSet.Auto)]
unsafe static extern int GetMyData(ref DATASTRUCT data);
[DllImport("MyDLL.dll", EntryPoint = "SetMyData", CharSet =
CharSet.Auto)]
unsafe static extern bool SetMyData(ref DATASTRUCT data);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DATASTRUCT
{
public Int16 MyInt;
public bool MyBool;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 25)] public string
MyString;
}
unsafe static public bool UseGetMyData()
{
int sz = 0;
try
{
DATASTRUCT p = new DATASTRUCT();
if ((sz = GetMyData(ref p)) == CORRECT_DATA_SIZE )
{
// Data from DATASTRUCT p used or
// assigned to other variables here
}
else return false;
}
catch (Exception ex)
{
// error handling here
return false;
}
}
unsafe static public bool UseSetMyData()
{
try
{
DATASTRUCT p = new DATASTRUCT();
// DATASTRUCT p variables assigned here
return SetMyData(ref p);
}
catch (Exception ex)
{
// error handling here
return false;
}
}
// some C++ code for MyDLL
typedef struct
{
__int16 MyInt;
bool MyBool;
wchar_t MyString[25];
} DATASTRUCT;
static _declspec(dllexport) int GetMyData(DATASTRUCT * data);
static _declspec(dllexport) bool SetMyData(DATASTRUCT * data);
bool SetMyData(DATASTRUCT * data)
{
// data used to set other variables here
return true;
}
int GetMyData(DATASTRUCT * data)
{
// data populated here
return sizeof(data);
}