473,396 Members | 1,799 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,396 software developers and data experts.

NullReferenceException on exit from managed/unmanaged code.

Hi,

I'm trying to put together some managed and unmanaged code. Everything
works fine until the program exits, and I suppose the garbage collector
runs to finalize things. Then I get:

Unhandled Exception: System.NullReferenceException: Object reference
not set to
an instance of an object.
at std.list<int,std::allocator<int>
.clear(list<int,std::allocator<int> >* )
at std.list<int,std::allocator<int>._Tidy(list<int,std::allocator<int> >* )


at Tab.MCell.Finalize()

I can't think why I would get a NullReferenceException in managed code.
Any ideas greatly appreciated.

The std::list is in unmanaged code:

#include <list>
#include <math.h>

using namespace std;

namespace Tab {

class __declspec(dllexport) Cell
{
private:
int _row;
int _col;
list<int> _vals;
...
};
}

The managed code looks like:

#using <mscorlib.dll>

#include "Cell.h"

using namespace std;
using namespace System::Collections;

namespace Tab {

public __gc class MCell
{
//
// DATA
//
private:
Cell __nogc *_cell;
//
// FUNCTIONS
//
public:
MCell(Cell *c)
{ _cell = c; }
MCell(int row, int col)
{ _cell = new Cell(row, col); }
~MCell()
{ _cell->~Cell(); }
....
};
}

The unmanaged code is built with the following which makes a DLL (the
8.3 directories are just where the stl stuff is on my machine):

cl -GX -I"C:\PROGRA~1\MIA4C6~1\include" Cell.cc /LD /link
/libpath:"C:\PROGRA~1\MIA4C6~1\lib"

The executable gets built with:

cl -GX -I"C:\PROGRA~1\MIA4C6~1\include" /clr CTest.cc /link
/libpath:"C:\PROGRA~1\MIA4C6~1\lib" Cell.lib

Thanks in advance!

-- Andrew Bell
an************@gmail.com

Nov 17 '05 #1
3 1416
an************@gmail.com wrote:
I'm trying to put together some managed and unmanaged code. Everything
works fine until the program exits, and I suppose the garbage collector
runs to finalize things. Then I get:

Unhandled Exception: System.NullReferenceException: Object reference
not set to
an instance of an object.
at std.list<int,std::allocator<int>
[...]
MCell(int row, int col)
{ _cell = new Cell(row, col); }
~MCell()
{ _cell->~Cell(); }


Is there a reason why you're calling the destructor explicitly? There's
only one case when you can (should) do it, and that's when the object is
created using the inplace new operator. So you either do

//A:
cell = new Cell(row, col);
[...]
delete cell;
cell = 0; // just in case; it helps debugging hard-to-trace bugs

--- OR ----

//B:
unsigned char buffer[sizeof(Cell)];
cell = new (buffer) (row, col);
[...]
cell->~Cell();
cell = 0; // just in case; it helps debugging hard-to-trace bugs

But you can't mix the two syntaxes, you can't construct with the
standard new operator and then call the destructor explicitly. I
recommend that you change your MCell destructor to

~MCell() { delete _cell; }

I also recommend that you stop using names beginning with an underscore,
as it may conflict with reserved words and other internal definitions,
although this is most likely not related to your problem.

Tamas
Nov 17 '05 #2
Thanks for your feedback. I did figure out the problem -- things were
getting deleted more than once. I just didn't understand the way that
things were manifesting themselves with the exception.

It is OK to explicitly call the dtor even if you have new'ed the
object. It just isn't usually necessary or helpful. Microsoft was
doing this in their example code that marries managed and unmanaged
code. Heck, I wasn't sure what they were thinking, so I thought I
would try it too. I did have a new/delete pair in my code originally.

Leading underscores certainly don't violate any language constraint.
Leading double underscores are reserved. Leading underscores are
reserved by C implementations, but since I use them to denote member
variables, it isn't an issue. Been doing it for many years on many
platforms without a single problem. As you said, this was not related
to my problem:)

Thanks again,

-- Andrew Bell

Nov 17 '05 #3
an************@gmail.com wrote:
It is OK to explicitly call the dtor even if you have new'ed the
object.
Except it's going to leak, because nobody deletes the object's memory.
The destructor certainly doesn't delete any memory.
Leading underscores certainly don't violate any language constraint.


There *is* a very real possibility that you get into a conflict, it's
not just a style issue.

http://www.informit.com/articles/art...&seqNum=3&rl=1
Herb Sutter writes: "Never, ever, ever create names that begin with an
underscore or contain a double underscore; they're reserved for your
compiler and standard library vendor's exclusive use so that they have
names they can use without tromping on your code."

Tom
Nov 17 '05 #4

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

Similar topics

0
by: Yoni Rabinovitch | last post by:
Hi, I am new to C#, so I apologise if this is a stupid question. I have some 3rd party C++ code, which gets built as static libraries (not DLLs). I want to create a C# form, which will call...
0
by: noe | last post by:
I have done a DLL in unmanaged code C++ and in that dll I have defined a function that use memcpy.This dll run ok.I use that dll in a aplication in C# managed code in Visual Studio .NET. I import...
3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
1
by: Klynt | last post by:
Project built using /CLR, but code is old and has not been converted specifically to "managed" (__gc or __value). Everything seemed to work great, until I got the following error. I have a...
2
by: ishekara | last post by:
Hi, Strange, I am having a .Net C++ managed dll project, where in i call a new on a non managed code. This however is giving a problem, I get an exception saying "An unhandled exception of type...
1
by: razilon | last post by:
Hi, I've written a managed class that makes use of stl vectors of a few unmanaged structs for data handling/manipulation, but I'm getting a few very strange errors. I get an "Unhandled...
0
by: razilon | last post by:
Hi, I've written a managed class that makes use of stl vectors of a few unmanaged structs for data handling/manipulation, but I'm getting a few very strange errors. I get an "Unhandled...
9
by: Amit Dedhia | last post by:
Hi All I have a VC++ 2005 MFC application with all classes defined as unmanaged classes. I want to write my application data in xml format. Since ADO.NET has buit in functions available for...
1
by: Bruce | last post by:
I am using VC .NET 2003. I created a simple unmanaged class in C++. The class is contained in a library. I then created a simple managed class wrapper that calls a function in the unmanaged...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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,...

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.