473,385 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

new operator throws NullReferenceException

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
Nov 17 '05 #1
1 1618
> 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.


I saw this once when heap memory was corrupted. To catch this you better use
debug crt.
--
Vladimir Nesterovsky
e-mail: vl******@nesterovsky-bros.com
home: www.nesterovsky-bros.com
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sébastien Ros | last post by:
During the process of an XML document, I have to make a SelectNodes() call on an XmlDocument. It returns an isntance of XPathNodeList. I can call it several times but at one point, the result can't...
5
by: Niall | last post by:
I have an unmanaged C++ ray tracer which I am working to put a C# front end on. It runs fine as just the unmanaged code. I have made a MC++ wrapper DLL to expose the required types to the C#...
8
by: Pesso | last post by:
I'm having a difficulty compairing null to a class object whose "equal" operator is overridden.. Consider the following: class Foo { // ... public static bool operator==(Foo f1, Foo f2) {...
17
by: ma740988 | last post by:
Consider: # include <iostream> # include <algorithm> # include <vector> # include <string> using namespace std; class msg { std::string someStr;
2
by: Karen Hill | last post by:
I have a datagridview control on a form with a few of the columns being DataGridViewCheckBox. Whenever I click on the checkbox to create a new record I get a NullReference Exception thrown even...
3
by: Kush | last post by:
Hello All, Once again another question. :) This time I would like to understand why one needs to overload the operator to access objects in array format? Example code: class A {
7
by: Zytan | last post by:
I just got the strangest error. I have exception code that catches a null reference exception: catch (NullReferenceException ex) { ... } The handling code does nothing but print out data from...
12
by: Premal | last post by:
Hi, I tried to make delete operator private for my class. Strangely it is giving me error if I compile that code in VC++.NET. But it compiles successfully on VC++6.o. Can anybody give me inputs...
6
by: GiJeet | last post by:
Problem using AS operator to cast Tag property Hello, I’m using the Tag property of a menu item to hold an enum value of a PictureBoxSizeMode. Eg: this.menuImageStretch.Tag =...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.