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])
{ }
};

Signature
Marcus Kwok
Replace 'invalid' with 'net' to reply
Marcus Kwok - 16 May 2006 14:53 GMT
> 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
[quoted text clipped - 7 lines]
> // 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.

Signature
Marcus Kwok
Replace 'invalid' with 'net' to reply
Marcus Kwok - 16 May 2006 15:33 GMT
>> 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
[quoted text clipped - 14 lines]
> ContoursDialog::Contours contours;
> write_contours_file(&contours);
Nevermind, I still get the crash...

Signature
Marcus Kwok
Replace 'invalid' with 'net' to reply
Marcus Kwok - 16 May 2006 20:18 GMT
>>> 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
[quoted text clipped - 16 lines]
>
> 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.

Signature
Marcus Kwok
Replace 'invalid' with 'net' to reply