473,699 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

new operator throws NullReferenceEx ception

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 NullReferenceEx ceptions.

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 NullReferenceEx ception. 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 1631
> 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 NullReferenceEx ceptions.

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 NullReferenceEx ception. 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******@nester ovsky-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
3580
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 be iterated. Here is the code: XmlNodeList nodes = _Document.SelectNodes(myXpathQuery); foreach(XmlNode n in nodes) { ... }
5
4468
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# project. Things seem to be getting through from the unmaged side to the managed side - the image is being rendered correctly. However, at random points, calls to new are returning null. Hence, when the managed type gets destroyed and calls delete on its...
8
2117
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) { return (f1.n == f2.n); } Now the following throws:
17
2037
by: ma740988 | last post by:
Consider: # include <iostream> # include <algorithm> # include <vector> # include <string> using namespace std; class msg { std::string someStr;
2
4881
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 though I check for NULL using System.Convert.IsDbNull function. What gives? I'm catching the NullReferenceException, although I know it is not correct to do for normal program flow. What is the solution?
3
1248
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
1977
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 within the object, ex. So, nothing is changed. And I've found that the exception object, ex, ITSELF is null! So, by accessing it to print its innards, it throws another NullReferenceException (which is, of course, unhandled)! Printing...
12
2561
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 about it. I wanted that on my class delete should not work. Object pointer should be deleted using my function only which is taking care of reference count for particular class. Thanx in advance for your inputs.
6
8441
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 = System.Windows.Forms.PictureBoxSizeMode.StretchImage; now in the ProcessImageClick method I check to make sure the Tag propety is not null and cast from type Object to PictureBoxSizeMode
0
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8613
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9172
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.