Hi,
I have a weird exception happening when calling the new operator. I'm
not sure if this is the intended reaction to the situation or is
actually a problem I've got. In any case a different error message
would be helpful. I would like to know your thoughts on this problem.
The situation:
I have an external Library of C Functions and Structures which are
implemented in a statical Library (.lib)
To use them in C# I've created a C++ DLL which creates a
object-oriented representation to interface with of the C functions.
These C++ objects own instances of handle structures, etc and make the
corresponding function calls. One should add, that these classes
represent data structures in the system to which we interface, so we
end up instantiating several thousand of these classes.
The Classes instantiate these external structures in the constructor
with new before calling a C function to fill it (good old C).
The problem:
This works fine for a whole while until a certain point the new
operator starts throwing NullReferenceExceptions.
The solutions I've tried:
I've tried by expanding the memory size of the heap in the compiler
and also adjust timing by sending the Thread to sleep a moment. Both
measures had no impact what soever on the time and point this problem
started appearing.
My explanaition and question:
The problem started vanishing after I started to redesign the code so
I didn't have to instantiate the structures. Though it wasn't elegant,
it solved the problem. On the other hand I didn't have any problems of
instatiating arrays of these structures at any time.
My guess is following: because I try to instantiate a structure which
is a value type, it is boxed somewhere inside the new operator before
being passed back. I believe that already the instantiation of the
structure failed and returned a null and when the new operator then
tries to box it, throws a NullReferenceException. What I don't
understand at this point is why all other instantiations still work
after that if it were a OutofMemory Situation.
Any thoughts?
Thanks,
Eduard Ralph