473,834 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

New operator returns null in some cases

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
pointer for the encapsulated unmanaged type, there is an exception.

This really confuses me, I've never seen new return null before. The machine
isn't out of memory, there's hundreds of megs left. Does anyone have any
ideas what could be causing this behaviour?

Niall
Nov 16 '05 #1
5 4473
Niall wrote:
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 pointer for the
encapsulated unmanaged type, there is an exception.

This really confuses me, I've never seen new return null before. The
machine isn't out of memory, there's hundreds of megs left. Does
anyone have any ideas what could be causing this behaviour?


VC++ has two different implementations of void* ::operator new(size_t) - one
that throws (C++ standard compliant) and one that returns null (VC6
compatible). Which one gets linked into your application can be hard to
determine sometimes. One thing that you could do is explicitly link
%VSDir%\vc7\lib \thrownew.obj into your program and see if your nulls turn
into exception throws. If so, then you may be fragmenting heap space to the
point that there's no contiguous free space large enough for some
allocation, hence the failure. If your nulls don't turn into exceptions
with thrownew.obj linked in, then something else is going on - most likely
heap corruption of some kind.

-cd
Nov 16 '05 #2
Thanks for your reply, Carl. I tried your suggestion of using the throw
version of new. I get a NullReference exception on the throw line, is this
the type of exception you would expect, or is there a specific exception for
failure of the new operator? It only happens after some time (after the
first run through the render).

I don't know too much about how the native C++ heap runs. The class it's
unable to instantiate is a pretty small class (just three floats and a few
methods). There would certainly be a lot of these objects instantiated and
destroyed during the process of running the program, the class is used to
represent colours. However, I would have thought that it would be possible
to put a new object in the same spot as one of the old ones, because the
objects are all of the same size. Also, there is still plenty of memory
free, and I haven't run into this problem when running the unmanaged code on
its own.

The heap corruption you mention, are there other specific causes you know
of? I'm not ruling out some bad memory handling by myself (though for the
most part, most of the app's variables are stack based), though I would
expect that would cause problems trying to use memory or delete it, not on
the new...

Any help you can give will be greatly appreciated

Niall

"Carl Daniel [VC++ MVP]" <cp******@nospa m.mvps.org> wrote in message
news:e$******** ******@TK2MSFTN GP11.phx.gbl...
Niall wrote:
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 pointer for the
encapsulated unmanaged type, there is an exception.

This really confuses me, I've never seen new return null before. The
machine isn't out of memory, there's hundreds of megs left. Does
anyone have any ideas what could be causing this behaviour?
VC++ has two different implementations of void* ::operator new(size_t) -

one that throws (C++ standard compliant) and one that returns null (VC6
compatible). Which one gets linked into your application can be hard to
determine sometimes. One thing that you could do is explicitly link
%VSDir%\vc7\lib \thrownew.obj into your program and see if your nulls turn
into exception throws. If so, then you may be fragmenting heap space to the point that there's no contiguous free space large enough for some
allocation, hence the failure. If your nulls don't turn into exceptions
with thrownew.obj linked in, then something else is going on - most likely
heap corruption of some kind.

-cd

Nov 16 '05 #3
Niall wrote:
Thanks for your reply, Carl. I tried your suggestion of using the
throw version of new. I get a NullReference exception on the throw
line, is this the type of exception you would expect, or is there a
specific exception for failure of the new operator? It only happens
after some time (after the first run through the render).
NullReference is a managed (CLR) exception, and not what I'd expect new to
be throwing (it should throw std::bad_alloc) . Are you sure this is
unmanaged code where you're seeing the error?

I don't know too much about how the native C++ heap runs. The class
it's unable to instantiate is a pretty small class (just three floats
and a few methods). There would certainly be a lot of these objects
instantiated and destroyed during the process of running the program,
the class is used to represent colours. However, I would have thought
that it would be possible to put a new object in the same spot as one
of the old ones, because the objects are all of the same size. Also,
there is still plenty of memory free, and I haven't run into this
problem when running the unmanaged code on its own.
Sounds very much like a heap corruption problem to me.

The heap corruption you mention, are there other specific causes you
know of? I'm not ruling out some bad memory handling by myself
(though for the most part, most of the app's variables are stack
based), though I would expect that would cause problems trying to use
memory or delete it, not on the new...


Unfortunately, heap corruption can be very hard to find. Examine all of
your access to heap-allocated objects carefully, looking for overindexed
pointers, acess of deleted objects, etc.

-cd
Nov 16 '05 #4
"Niall" <as**@me.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Thanks for your reply, Carl. I tried your suggestion of using the throw
version of new. I get a NullReference exception on the throw line, is this
the type of exception you would expect, or is there a specific exception for
failure of the new operator? It only happens after some time (after the
first run through the render).


The version of new that throws should throw a std::bad_alloc.
NullReferenceEx ception actually isn't a C++ exception at all, it's a managed
exception, and as far as I can tell it should never be thrown from the
standard C++ libraries if everything is kosher. The only reason I can think
that it would be thrown is in cases of undefined behavior (and, of course,
your code is compiled as managed).

My gut instict says heap corruption. Try to see if you are overrunning a
buffer somewhere.

Ken
Nov 16 '05 #5
Carl and Ken:

Hopefully you will see this, as I have let the post get old. The
NullReferenceEx ception is somewhat new. Sometimes, it seems to do new fine,
and I have watched it in the debugger, new has returned a null reference,
but with no exception. Then, of course, when the destructor comes around,
deleting the unmanaged object, which is null, causes an exception. However,
at other times, like is now happening, there is a NullReferenceEx ception
when calling new, which is, as you say a managed exception.

The NullReferenceEx ception is occurring on the line that is creating a new
instance of the underlying unmanaged C++ object from another DLL that is
being wrapped by the MC++ classes. The constructor being called in the
unmanaged DLL is trivial, there is nothing in it except setting up a few
floating point variables, so nothing that should cause an exception.

The strange thing is that these managed wrapper objects can work fine for a
while of the run, then cause the exception. This kind of unpredicatable
behaviour leads me to believe, as you suggest, that there is some dodgy
memory access occurring. Unfortunately, the unmanaged code runs fine on its
own with no memory issues. Because of its highly functional nature, almost
all memory is stack based, which I found much easier than trying to work out
when I was really finished with an object and deleting it. So there isn't
much opportunity for heap corruption, I think.

The one area I do suspect is the integration between the MC++ dll and the
native C++ dll. This is something I haven't done before, so I've had to
learn a fair bit. One thing I'm not sure of is the whole static
initialisation issue with the DLLs. I went through the steps to make the
MC++ dll use /NOENTRY, found on one of the MSDN pages. One of the steps is
the manual initialisation of the C ruintime. Do I have to call some method
in my native C++ dll which will manually setup values in the few static
variables I'm using? This is about the only guess I have...

Once again, any help would be greatly appreciated

Niall

"Carl Daniel [VC++ MVP]" <cp******@nospa m.mvps.org> wrote in message
news:OW******** ******@TK2MSFTN GP12.phx.gbl...
Niall wrote:
Thanks for your reply, Carl. I tried your suggestion of using the
throw version of new. I get a NullReference exception on the throw
line, is this the type of exception you would expect, or is there a
specific exception for failure of the new operator? It only happens
after some time (after the first run through the render).


NullReference is a managed (CLR) exception, and not what I'd expect new to
be throwing (it should throw std::bad_alloc) . Are you sure this is
unmanaged code where you're seeing the error?

I don't know too much about how the native C++ heap runs. The class
it's unable to instantiate is a pretty small class (just three floats
and a few methods). There would certainly be a lot of these objects
instantiated and destroyed during the process of running the program,
the class is used to represent colours. However, I would have thought
that it would be possible to put a new object in the same spot as one
of the old ones, because the objects are all of the same size. Also,
there is still plenty of memory free, and I haven't run into this
problem when running the unmanaged code on its own.


Sounds very much like a heap corruption problem to me.

The heap corruption you mention, are there other specific causes you
know of? I'm not ruling out some bad memory handling by myself
(though for the most part, most of the app's variables are stack
based), though I would expect that would cause problems trying to use
memory or delete it, not on the new...


Unfortunately, heap corruption can be very hard to find. Examine all of
your access to heap-allocated objects carefully, looking for overindexed
pointers, acess of deleted objects, etc.

-cd

Nov 16 '05 #6

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

Similar topics

34
6480
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment operator. Here's what I'm trying to do. I've defined class Complex as class Complex { friend ostream &operator<<( ostream &, Complex & ); public: Complex( float = 0.0, float = 0.0 );
80
35165
by: Christopher Benson-Manica | last post by:
Of course one can get the effect with appropriate use of existing operators, but a ^^ operator would make for nice symmetry (as well as useful to me in something I'm working on). Am I the only one who would find it useful? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
3
1989
by: Jon Shemitz | last post by:
Why would you ever want to define both "public static bool operator true" and "public static bool operator false" when you can just define "public static implicit operator bool"? Is there syntax where the two are not equivalent? Or are there cases where you might legitimately want both false and true to return the same value? Fwiw, I'm having a hard time coming up with any C# 1.1 code that calls true or false operators. I'm inclined...
6
2991
by: Robert Zurer | last post by:
In his paper on Coding Standard found on http://www.idesign.net/idesign/DesktopDefault.aspx Juval Lowy discourages the use of the ternary conditional operator with no specific reason given. I can understand that complicated code would be much less readable using nested operators, but aside from that is there another reason? -- something going on under the hood perhaps which make it inadvisable to use?
9
2391
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I attempt to check to see if the object is null: "The call is ambiguous between the following methods or properties: 'TestObject.operator ==(TestObject, string)' and 'TestObject.operator ==(TestObject, TestObject)" Does anyone have any idea how to...
12
2218
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid overloading of == and != but instead translate each call of objA==objB automatically in System.Object.Equals(objA, objB). This would remove inconsistencies like myString1==myString2
5
2285
by: sam_cit | last post by:
Hi Everyone, I was just wondering, about the overloaded assignment operator for user defined objects. It is used to make sure that the following works properly, obj1 = obj; so the overloaded operator function performs the necessary copy of obj's member in to obj1. Can't the same be done using copy
9
3830
by: George2 | last post by:
Hello everyone, I am wondering the default implementation of assignment operator (e.g. when we do not implement assignment operator in user defined class, what will be returned? temporary object? reference or const reference? deep copy or shallow copy is used in default assignment operator?)? I have the C++ Programming Book at hand, but can not find it from Index page.
19
3533
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I need to create a map of my string (say a class called MyString) and an integer i.e. std::map<MyString, int>. Whenever I insert the elements in the map using the subscript operator, I noticed that the copy constructor for MyString is invoked more number of times than if I do it using the insert()...
0
9646
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
10795
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...
1
10550
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
10220
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
9332
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6957
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
5627
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
5796
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3981
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.