S
SteveZ
First off, I am not sure if this belongs in this group or the C# group. It
seems more like a C++ problem to me. Anyways...
I have a C# project which links in an unmanaged C++ DLL, both built with
VS2005. I am having a problem where the C# program is making a call into the
C++ DLL which new's a large class. For a class of a certain size the new is
failing. Here's somewhat of an example of what I am doing:
// Start C# program code
public enum MyTypes
{
TYPE1 = 0,
TYPE2 = 1
}
[DllImport("MyDll.dll")]
public static extern void UnmanagedFunc(MyTypes PassedType);
MyTypes funcArg = MyTypes.TYPE1;
UnmanagedFunc(funcArg);
// End C# program code
// Start unmanaged C++ DLL code
// This next line is from the class declaration and initialized to NULL in
the
// class constructor, I just haven't shown that here.
ParentClass *ClassPtr_;
void UnmanagedClass::UnmanagedFunc(MyTypes PassedType)
{
if(ClassPtr_)
{
delete ClassPtr_;
}
switch(PassedType)
{
case TYPE1:
ClassPtr_ = new Class1();
break;
case TYPE2:
default:
ClassPtr_ = new Class2();
break;
}
}
// End unmanaged C++ DLL code
A few things to note. I left out the C++ wrapper code which wraps the
unmanaged call to this function to the managed call--there is no issue with
that code. Class1 and Class2 are inherited from ParentClass. Class1's size
is around 150 MB, while Class2 is around 200 MB. Both classes have large
static arrays in them, but Class2 has more arrays, which makes up the 50 MB
difference.
When UnmanagedFunc is called with the parameter MyTypes::TYPE1, there is no
problem. However when MyTypes::TYPE2 is used, then the new call throws
std::bad_alloc. I have plenty of physical memory and virtual memory
available, so that should not be a problem. My program size (according to
Task Manager and Performance Monitor) is about 750 MB at the point before
attempting the new. This brings up a few questions in my mind (other than
the obvious, "why doesn't this work?"):
Are unmanaged and managed heaps shared within the context of a process? If
not, could one heap (in this case the managed heap) reserve more space than
it needs such that the other heap (in this case the unmanaged heap) doesn't
have enough heap to new the larger class?
Is there a way to specify the size of the heap in C# (since there is no way
to specify heap size for a DLL that I am aware of)?
If this is a problem with fragmentation, is there any way to "defragment"
the heap space so I am able to allocate the contiguous block that I need?
Many thanks in advance.
-SteveZ
seems more like a C++ problem to me. Anyways...
I have a C# project which links in an unmanaged C++ DLL, both built with
VS2005. I am having a problem where the C# program is making a call into the
C++ DLL which new's a large class. For a class of a certain size the new is
failing. Here's somewhat of an example of what I am doing:
// Start C# program code
public enum MyTypes
{
TYPE1 = 0,
TYPE2 = 1
}
[DllImport("MyDll.dll")]
public static extern void UnmanagedFunc(MyTypes PassedType);
MyTypes funcArg = MyTypes.TYPE1;
UnmanagedFunc(funcArg);
// End C# program code
// Start unmanaged C++ DLL code
// This next line is from the class declaration and initialized to NULL in
the
// class constructor, I just haven't shown that here.
ParentClass *ClassPtr_;
void UnmanagedClass::UnmanagedFunc(MyTypes PassedType)
{
if(ClassPtr_)
{
delete ClassPtr_;
}
switch(PassedType)
{
case TYPE1:
ClassPtr_ = new Class1();
break;
case TYPE2:
default:
ClassPtr_ = new Class2();
break;
}
}
// End unmanaged C++ DLL code
A few things to note. I left out the C++ wrapper code which wraps the
unmanaged call to this function to the managed call--there is no issue with
that code. Class1 and Class2 are inherited from ParentClass. Class1's size
is around 150 MB, while Class2 is around 200 MB. Both classes have large
static arrays in them, but Class2 has more arrays, which makes up the 50 MB
difference.
When UnmanagedFunc is called with the parameter MyTypes::TYPE1, there is no
problem. However when MyTypes::TYPE2 is used, then the new call throws
std::bad_alloc. I have plenty of physical memory and virtual memory
available, so that should not be a problem. My program size (according to
Task Manager and Performance Monitor) is about 750 MB at the point before
attempting the new. This brings up a few questions in my mind (other than
the obvious, "why doesn't this work?"):
Are unmanaged and managed heaps shared within the context of a process? If
not, could one heap (in this case the managed heap) reserve more space than
it needs such that the other heap (in this case the unmanaged heap) doesn't
have enough heap to new the larger class?
Is there a way to specify the size of the heap in C# (since there is no way
to specify heap size for a DLL that I am aware of)?
If this is a problem with fragmentation, is there any way to "defragment"
the heap space so I am able to allocate the contiguous block that I need?
Many thanks in advance.
-SteveZ