Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2005

Tip: Looking for answers? Try searching our database.

What's wrong with this

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Droopy - 11 Jul 2005 15:53 GMT
Hi,

I don't understand what's wrong with the following code.
It works in debug mode but generate a System.NullReferenceException in
release mode (see comment to locate the line where this exception
occurs).
I am using VS 2003, Win XP.
This class (SerialBuffer) is a managed C++ class developed to pass a char
buffer to unmanaged C++ legacy code in a mixed mode DLL.
This class is instantiated in C# code.
I made sample code here below to illustrate the problem (simplified as
much as I could).

// code.h

__gc public class SerialBuffer
{
private:
    unsigned char    __nogc *_buffer;

public:
    SerialBuffer (short bufferNr);
    ~SerialBuffer ();
}

// code.cpp

SerialBuffer::SerialBuffer (short bufferNr)
{
    _buffer = new unsigned char [16];        // this line fail in release
mode
}

SerialBuffer::~SerialBuffer ()
{
    delete _buffer;
}

// win app code.cs

private void Form1_Load(object sender, System.EventArgs e)
{
    SerialBuffer buf = new SerialBuffer (1);
}

Thanks in advance for your help,

Droopy.
Tamas Demjen - 11 Jul 2005 17:26 GMT
> SerialBuffer::SerialBuffer (short bufferNr)
> {
[quoted text clipped - 6 lines]
>     delete _buffer;
> }

If you use new[], you must use delete[]:
delete [] _buffer;

Tom
Droopy - 12 Jul 2005 09:29 GMT
>> SerialBuffer::SerialBuffer (short bufferNr)
>> {
[quoted text clipped - 12 lines]
>
> Tom

Hi Tom,

You are right but it was a typing error, I have it in my code.
Besides, the exception I got is in the constructor, at startup.

Thanks a lot for your answer.

Any other clue ?

Droopy.
Vladimir Nesterovsky - 12 Jul 2005 19:23 GMT
> >> SerialBuffer::SerialBuffer (short bufferNr)
> >> {
[quoted text clipped - 10 lines]
> > If you use new[], you must use delete[]:
> > delete [] _buffer;

You can get an exception in the operator new if you have heap corrupted.
E.g. check that you do not set _buffer[16] = 0; :-)
Signature

Vladimir Nesterovsky
e-mail: vladimir@nesterovsky-bros.com
home: http://www.nesterovsky-bros.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.