locked
Exception error with public Vector

    Question

  • Hi everybody,

    I have a public IVector in my Class which is filled with ListViewItems. Everything compiles fine but in runtime I get an exception. This is my header:

    property Windows::Foundation::Collections::IVector<Windows::UI::Xaml::Controls::ListViewItem^>^ myList;
    		


    This is my .cpp

    ListViewItem^ item1 = ref new ListViewItem();
    	item1->Content = "item1";
    
    myList->Append(item1);

    "Exception error at 0x013E4F97 in HelloWorld.exe: 0xC000041D: 

    What I am trying to do is to pass a Vector full of items from the initialisation to my callback functions. The List is bound to a ListView UI Control.

    Any ideas?

    I thank u in advance... 

    Monday, December 09, 2013 6:12 PM

Answers

  • In your MainPage constructor, you have this line:

    	Windows::Foundation::Collections::IVector<Windows::UI::Xaml::Controls::ListViewItem^>^ myList = ref new Platform::Collections::Vector<Windows::UI::Xaml::Controls::ListViewItem^>;
    

    It should be this:

    	myList = ref new Platform::Collections::Vector<Windows::UI::Xaml::Controls::ListViewItem^>;
    

    What's happening is that you are creating a local variable myList in the constructor. The class member variable that is also named myList remains null because no value is ever assigned to it. So when you tap on an item, the program reaches your event handler and tries to use the null class member variable myList. Since myList is null, it throws an exception.

    You should be able to see in the Output window when you run the program (before fixing it) that it says something like:

    First-chance exception at 0x0103C85B in App5.exe: 0xC0000005: Access violation reading location 0x00000000.

    Which is the giveaway that it's trying to dereference a null pointer. Anytime you see 0xC0000005 you know that your program is trying to access memory that it shouldn't be trying. If the location fits the pattern 0x00000___ (i.e. only the last three numbers are possibly not 0) then it's a null pointer (sometimes you'll see things like 0x00000014 because it's trying to access an offset from the base address).

    If it were something with a non null address, then the pointer address was corrupted or uninitialized and so you have to figure out either where the pointer address was set to have a bad value or figure out why it was never initialized and thus has a random value. (With C++/CX classes that will never happen. You can only get uninitialized class member variables with native C++ classes (unless you initialize them, of course)).


    Visual C++ MVP | Website | Blog | @mikebmcl | Windows Store DirectX Game Template

    Friday, December 13, 2013 4:21 PM

All replies

  • Hi Peter,

    I'm not sure what exactly you're doing here, but it doesn't sounds like you have binding set up correctly.

    Typically you would bind to a list of model objects, not a list of ListViewItems. The data binding would then fill the ListView with generated items based on the contents of the list.

    Take a look at the XAML data binding sample for examples.

    --Rob

    Tuesday, December 10, 2013 2:16 AM
    Owner
  • hi, thank u for your reply.

    You are right, later I am gonna use it the way you mentioned. But it doesn't  matter what I fill the vector with, I always get that exception.

    I tried the same with ...<Platform::String^> without Databinding -> the same.

    --Pete

    Tuesday, December 10, 2013 6:41 PM
  • Hi Pete,

    You don't provide enough context here to know what causes that exception. If I just copy your code into an app I would get an exception since myList is never defined, but if I ref new up a list for it then I don't get an exception. The code won't do anything useful though, and it's not really on the right track to get to useful.

    --Rob

    Tuesday, December 10, 2013 8:51 PM
    Owner
  • ya thats true if I ref new it I dont get an exception when I fill the vektor, but I get one when I try to acces it in another function, like a tabbed callback or sth.

    What I want to do is: Fill the vektor, fill a ListView with it and then, in a tabbed_callback, work with the clicked item's content.

    --pete

    Tuesday, December 10, 2013 9:05 PM
  • Hi Pete,

    I'm still not sure what exactly you're doing. Can you share a minimal repro sample on your SkyDrive and exact steps to demonstrate the problem?

    --Rob

    Thursday, December 12, 2013 1:29 AM
    Owner
  • Hi, so I created an new app as example.

    https://skydrive.live.com/redir?resid=13F1B9B15DCCB06E!1154&authkey=!APjtTvyxNIF9-AE&ithint=folder%2c.opensdf

    There is nothing but a ListView and a Textblox as UserInterface. 

    1. I added the property mylist as an IVector in the MainPage.cpp.h

    2. filled mylist with ListViewItems in the .cpp and bound it to the ListView UI

    -- no problem so far--

    3. created a tapped callback to display the content of the selected Item

    -- Exception when I tap on an Item --

    So ... any ideas? I am thankful for every hint.

    --pete


    • Edited by Peter Carl Thursday, December 12, 2013 1:35 PM
    Thursday, December 12, 2013 1:34 PM
  • In your MainPage constructor, you have this line:

    	Windows::Foundation::Collections::IVector<Windows::UI::Xaml::Controls::ListViewItem^>^ myList = ref new Platform::Collections::Vector<Windows::UI::Xaml::Controls::ListViewItem^>;
    

    It should be this:

    	myList = ref new Platform::Collections::Vector<Windows::UI::Xaml::Controls::ListViewItem^>;
    

    What's happening is that you are creating a local variable myList in the constructor. The class member variable that is also named myList remains null because no value is ever assigned to it. So when you tap on an item, the program reaches your event handler and tries to use the null class member variable myList. Since myList is null, it throws an exception.

    You should be able to see in the Output window when you run the program (before fixing it) that it says something like:

    First-chance exception at 0x0103C85B in App5.exe: 0xC0000005: Access violation reading location 0x00000000.

    Which is the giveaway that it's trying to dereference a null pointer. Anytime you see 0xC0000005 you know that your program is trying to access memory that it shouldn't be trying. If the location fits the pattern 0x00000___ (i.e. only the last three numbers are possibly not 0) then it's a null pointer (sometimes you'll see things like 0x00000014 because it's trying to access an offset from the base address).

    If it were something with a non null address, then the pointer address was corrupted or uninitialized and so you have to figure out either where the pointer address was set to have a bad value or figure out why it was never initialized and thus has a random value. (With C++/CX classes that will never happen. You can only get uninitialized class member variables with native C++ classes (unless you initialize them, of course)).


    Visual C++ MVP | Website | Blog | @mikebmcl | Windows Store DirectX Game Template

    Friday, December 13, 2013 4:21 PM
  • Yes this works. Thank u very much Mike.

    --pete
    Wednesday, December 18, 2013 1:36 PM