470,815 Members | 2,026 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

[MC++] NullReferenceException with value structs?

I have been staring at this code and I cannot see what is wrong with it.
I am getting a NullReferenceException when trying to pass a value struct
to a method. This exception only happens in Release mode; Debug mode
runs fine. When I use the Debugger on Release mode, the debugger shows
valid values for "contour", but I still get this exception. If anybody
has any input at all, it would be much appreciated.

---------------------------
System.NullReferenceException: Object reference not set to an instance of an object.

at FSBSGui2.ControlPanel.write_contours_file(Contours & contours)

at FSBSGui2.ControlPanel.button_run_Click(Object sender, EventArgs e)
---------------------------

System::Void button_run_Click(System::Object * sender, System::EventArgs * e)
{
using System::Text::StringBuilder;
using System::Threading::Thread;
using System::Threading::ThreadStart;
using System::Windows::Forms::Button;
using System::Windows::Forms::DialogResult;
using System::Windows::Forms::MessageBox;
using System::Windows::Forms::MessageBoxButtons;
using System::Windows::Forms::MessageBoxIcon;

Button* button = __try_cast<Button*>(sender);
if (button->Text->Equals(S"&Cancel")) {
button_cancel_Click(sender, e);
return;
}

fill_FSBSParameters();
ContoursDialog::Contours contours; // HERE I INSTANTIATE
switch (run_type) {
case Xmtr_Avl:
contours = xmtr_avail_contours;
break;
case Fer:
contours = fer_contours;
break;
case Pcmr:
contours = pcmr_contours;
break;
default:
break;
}

VerificationDialog* vd = new VerificationDialog(*params, contours);
if (vd->ShowDialog() == DialogResult::OK) {
try {
write_contours_file(contours); // HERE I CALL
}
catch (System::NullReferenceException* e) {
System::Windows::Forms::MessageBox::Show(e->ToString());
}
}
}
// Here is the prototype for write_contours_file()
// I notice that the 'const' has been dropped in the stack trace above
System::Void write_contours_file(const ContoursDialog::Contours& contours);
// Here is the definition of ContoursDialog::Contours
__value struct Contours {
int num_contours;

__value struct ContourInfo {
double lower_bound;
int r;
int g;
int b;
};

ContourInfo contours[];

Contours()
: contours(new ContourInfo[max_contours])
{ }
};

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 12 '06 #1
3 1120
Marcus Kwok <ri******@gehennom.invalid> wrote:
I have been staring at this code and I cannot see what is wrong with it.
I am getting a NullReferenceException when trying to pass a value struct
to a method. This exception only happens in Release mode; Debug mode
runs fine.

ContoursDialog::Contours contours; // HERE I INSTANTIATE
write_contours_file(contours); // HERE I CALL

// Here is the prototype for write_contours_file()
// I notice that the 'const' has been dropped in the stack trace above
System::Void write_contours_file(const ContoursDialog::Contours& contours);


Well, I managed to work around it by changing it to

System::Void write_contours_file(const ContoursDialog::Contours* contours);
...
ContoursDialog::Contours contours;
write_contours_file(&contours);
I am just confused why I had to do this. I even made an intermediary
function:

System::Void print_contours(const ContoursDialog::Contours& contours);

which would print the contours and then call
write_contours_file(contours). The print_contours() function worked
perfectly fine, but then it still failed on the call to
write_contours_file(), even though both functions received their
parameters in exactly the same way.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 16 '06 #2
Marcus Kwok <ri******@gehennom.invalid> wrote:
Marcus Kwok <ri******@gehennom.invalid> wrote:
I have been staring at this code and I cannot see what is wrong with it.
I am getting a NullReferenceException when trying to pass a value struct
to a method. This exception only happens in Release mode; Debug mode
runs fine.

ContoursDialog::Contours contours; // HERE I INSTANTIATE
write_contours_file(contours); // HERE I CALL

// Here is the prototype for write_contours_file()
// I notice that the 'const' has been dropped in the stack trace above
System::Void write_contours_file(const ContoursDialog::Contours& contours);


Well, I managed to work around it by changing it to

System::Void write_contours_file(const ContoursDialog::Contours* contours);
...
ContoursDialog::Contours contours;
write_contours_file(&contours);


Nevermind, I still get the crash...

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 16 '06 #3
Marcus Kwok <ri******@gehennom.invalid> wrote:
Marcus Kwok <ri******@gehennom.invalid> wrote:
Marcus Kwok <ri******@gehennom.invalid> wrote:
I have been staring at this code and I cannot see what is wrong with it.
I am getting a NullReferenceException when trying to pass a value struct
to a method. This exception only happens in Release mode; Debug mode
runs fine.

ContoursDialog::Contours contours; // HERE I INSTANTIATE
write_contours_file(contours); // HERE I CALL

// Here is the prototype for write_contours_file()
// I notice that the 'const' has been dropped in the stack trace above
System::Void write_contours_file(const ContoursDialog::Contours& contours);


Well, I managed to work around it by changing it to

System::Void write_contours_file(const ContoursDialog::Contours* contours);
...
ContoursDialog::Contours contours;
write_contours_file(&contours);


Nevermind, I still get the crash...


OK, I think I solved it for real this time.

In my write_contours_file() function, I had the line:

// Here, line is a System::Text::StringBuilder*
line->Append(__box(std::log10(lower_bound))->ToString());

I changed it by adding a temporary variable:

double temp = std::log10(lower_bound);
line->Append(__box(temp)->ToString());
I still fail to see why the error would only occur in Release mode, and
why the debugger would crash where I called the function
write_contours_file(), instead of letting me step through the function
so I could have saved myself 3 days working on this.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Terrence | last post: by
1 post views Thread by Gurminder | last post: by
1 post views Thread by Shawn B. | last post: by
1 post views Thread by Marcus Kwok | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.