3

In Visual Studio, BuildVision gives me an Error, from which the Message is < Struct at Null>

Is it the same as "Struct is Null", or is it something different?

I'm asking because I couldn't find anything on the web with this message. All that I could find was pointing to "Struct is Null", but all I found didn't help me out! I was wondering if this is because of the at?

Edit:

I add a screenie of the debugger window with the < Struct at Null>-Stuff. I have a second question as well: I want to catch this exception with an if-else clause. How can I say

if(value == <struct at NULL>)
    this()
else
    that()

This is not working, although the debugger says the value would be .

enter image description here

In This example I try to check if components of the "distribution" are null to determine the state of the Distribution object. Here is the origianl code:

if (Distribution.distBrowserName() == NULL || Distribution.getShape() == NULL)
        return false;
    else 
        return true;
monamona
  • 1,195
  • 2
  • 17
  • 43
  • What is the error code? – Rotem Feb 19 '18 at 09:33
  • 3
    sorry, but i dont believe you that "< Struct at Null>" is the full error message – 463035818_is_not_an_ai Feb 19 '18 at 09:33
  • Post some code? *at* suggests you are mucking about with pointers. And maybe pointers to pointers – doctorlove Feb 19 '18 at 09:33
  • I am trying to call something in the form `strategy->plot()`, while strategy being a custom class pointer. THe Error message occuring is in an "Acces rading Violation..." Window. The Error in the BuildVision is just – monamona Feb 19 '18 at 09:37
  • And is `strategy` NULL? Where does it get initialised? – doctorlove Feb 19 '18 at 09:43
  • 1
    I think you need to edit the question to have a minimal, verifiable example – UKMonkey Feb 19 '18 at 09:48
  • The code snippet that you have posted is invalid, but you haven't posted the code that's actually creating the error. – David Hoelzer Feb 19 '18 at 10:22
  • Have you verified that `Distribution` isn't null? – David Hoelzer Feb 19 '18 at 10:22
  • Distribution is whatever that means – monamona Feb 19 '18 at 10:23
  • It is not a common diagnostic. My psychic debugger says that Distribution is a object& reference. References are pointers under the hood and should always be initialized. But with sufficient UB that can go wrong, a corrupted pointer will always make the program blow up with an access violation. If you are sure that it is initialized correctly and cannot dangle then set a data breakpoint to trap it getting modified. – Hans Passant Feb 19 '18 at 12:05

2 Answers2

4

I also encountered this problem recently. In my case, it was caused by a reference that had been initialized by dereferencing a null pointer. Basically, it was something like this:

Foo * fooPtr = nullptr;
// ...
Foo & fooRef = *fooPtr;
// ...
fooRef.Bar() // read access violation

Even if the exception is thrown at the third line, the problem actually lies in the second line because dereferencing a null pointer is undefined behavior. Here, it creates a reference to an invalid object which the Visual Studio debugger represents as <Struct at NULL>. Trying to access a member of this invalid object then results in a read access violation.

If you still want to verify whether a reference is <Struct at NULL>, you can do it by comparing its address with null:

if (&fooRef != nullptr)
{
    fooRef.Bar();
}

Here are some other interesting questions regarding invalid references:

Is null reference possible?

Why don't I need to check if references are invalid/null?

Félix Caron
  • 771
  • 6
  • 11
3

(Three months late, but this is the top search result for me. Hope I can help someone.)

To answer your first question: Yes, <struct at NULL> means that the reference to the object (struct or class) is null. This message can appear with a dangling reference, as appears to be your case. It can also appear with a dangling/null pointer.

So, to your second question, it's the reference to the Distribution object itself in your example that's causing the problem, so you're probably looking for a dangling reference somewhere.

A useful feature in MSVS that people often overlook is Data Breakpoints. If you're unfamiliar with them, you can read about them here. In this case, for instance, it could help you track down whether your Distribution variable is being affected someplace you didn't expect.

Dan
  • 333
  • 4
  • 12