CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2010
    Posts
    42

    Question How to catch error: The instruction at '' referenced memory at ''. The memory could n

    Hi all,

    I am experiencing this error: The instruction at '' referenced memory at ''. The memory could not be read. What I want to do is possibly Catch that error and execute a Go To Statement so that it will skip that part of the code altogether.

    I do not know what parameter to put in the catch(<?>) block. Can anyone help me with this? Is it also possible if I want the code part where I want to GoTo is located in a different class/file?


    Some additional information: This error occurs when I try to read an XML node attribute but is not present. Here is the part of the code where I think it causes this error.

    VARIANT GetAttribute(string attribute, IXMLDOMNamedNodeMap* pNodeMap)
    {
    VARIANT varAtt;

    IXMLDOMNode* pAtt = NULL;

    _bstr_t tempb = attribute.c_str();
    //Process varSource first to determine if to use WMI or TSVar
    BSTR bstrAtt = tempb.copy();
    pNodeMap->getNamedItem(bstrAtt,&pAtt);
    pAtt->get_nodeValue(&varAtt);

    SAFE_RELEASE(pAtt);

    return varAtt;
    }



    Thank you!

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: How to catch error: The instruction at '' referenced memory at ''. The memory cou

    I do not know what parameter to put in the catch(<?>) block.
    Try catch (...).

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to catch error: The instruction at '' referenced memory at ''. The memory cou

    Quote Originally Posted by LeanA View Post
    Hi all,

    I am experiencing this error: The instruction at '' referenced memory at ''. The memory could not be read. What I want to do is possibly Catch that error and execute a Go To Statement so that it will skip that part of the code altogether.
    What does your documentation say if that XML node attribute doesn't exist?

    I'm sure it has to say something, as no programmer can guarantee that every single XML call will be successful.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Mar 2010
    Posts
    42

    Re: How to catch error: The instruction at '' referenced memory at ''. The memory cou

    Hi all, thank you for replying.

    @Skizmo: Thank you for that, I'll try it.

    @Paul McKenzie: If there is an error (missing attribute), the whole XML node should be skipped and process the next node.


    Additional info: I call the GetAttribute function in another class. Something like this:

    VARIANT varSource = GetAttribute("Source", pNodeMap);

    Where do you think should I put the try-catch statement? Here when I call the function, or in the function itself?

    An additional question, is it possible to use GoTo but the place where it will jump is located in another class/file?

    Thank you very much!

  5. #5
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: How to catch error: The instruction at '' referenced memory at ''. The memory cou

    An additional question, is it possible to use GoTo but the place where it will jump is located in another class/file?
    No.

    btw... STOP USING GOTO. It's old and obsolete and it is asking for design trouble.

  6. #6
    Join Date
    Mar 2010
    Posts
    42

    Re: How to catch error: The instruction at '' referenced memory at ''. The memory cou

    Hi Skizmo,

    Oh I see, thank you. By any chance, do you know how I can check if a VARIANT has no value? I mean if it was initialized like this but did not assign any value.

    VARIANT varAtt;

    I am using this code now:

    VARIANT varAtt;

    IXMLDOMNode* pAtt = NULL;

    _bstr_t tempb = attribute.c_str();
    //Process varSource first to determine if to use WMI or TSVar
    BSTR bstrAtt = tempb.copy();

    if (pNodeMap != NULL)
    {
    pNodeMap->getNamedItem(bstrAtt,&pAtt);
    if (pAtt != NULL)
    {
    pAtt->get_nodeValue(&varAtt);
    SAFE_RELEASE(pAtt);
    }
    }

    return varAtt;

    So I can avoid getting an attribute if it does not exist. I want to check now if the returned VARIANT has been assigned to or not.

    Thank you very much!

  7. #7
    Join Date
    Mar 2010
    Posts
    42

    Re: How to catch error: The instruction at '' referenced memory at ''. The memory cou

    Hi all,

    Here is the solution on how to find if a VARIANT is empty. Just want to share:

    Hello Lean A,

    1. >> Do you know how to check if a VARIANT data type is NULL or has no value?

    1.1 One way to do this would be to check to see if the VARTYPE is VT_EMPTY.

    For example :

    pAtt->get_nodeValue(&varAtt);

    if (V_VT(&varAtt) == VT_EMPTY)
    {
    ...
    }

    1.2 But this assumes that the method will initialize the out parameter "varAtt" via VariantInit() before returning.

    1.3 To ensure that "varAtt" is set to a proper value, it is best to call VariantInit() on "varAtt" before calling the method. E.g. :

    VariantInit(&varAtt);
    pAtt->get_nodeValue(&varAtt);

    1.4 The return HRESULT may also be useful in determining success/failure.

    1.5 Consult the documentation to be sure.

    - Bio.


    Thank you all!

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to catch error: The instruction at '' referenced memory at ''. The memory cou

    Quote Originally Posted by LeanA View Post
    @Paul McKenzie: If there is an error (missing attribute), the whole XML node should be skipped and process the next node.
    That is what you want to do. That is not really what I was asking you.

    I asked you "what does the documentation say if that node doesn't exist". Is a return code sent back to you that the function failed? Is an exception thrown that on failure?

    If you expect a function to return an error code or throw an exception, you should read the documentation of that function you're calling to see what the possible return values or exceptions thrown are. You didn't do that initially, which is why I asked you to look at the documentation and see what is returned on failure.

    Regards,

    Paul McKenzie

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured