locked
access violation error calling AppendQuerySuggestion

    Question

  • I get the following error when calling

    AppendQuerySuggestion

    Unhandled exception at 0x0134706B in s.exe: 0xC0000005: Access violation reading location 0xFEFEFF02.

    the code looks like this.

    void App::SearchResultsWanted(Windows::ApplicationModel::Search::SearchPane^ sender,Windows::ApplicationModel::Search::SearchPaneSuggestionsRequestedEventArgs^ e)
    {
    
    
    	StockViewerCP::DataModel::DataManager^ dataManager = ref new StockViewerCP::DataModel::DataManager();
    	int stockNumber = _wtoi(e->QueryText->Data());
    
    	auto searchRecommendationsTask =	create_task( dataManager->SearchStocks(stockNumber));
    
    
    	auto deferral =	e->Request->GetDeferral();
    	auto searchSuggestionCollection = e->Request->SearchSuggestionCollection;
    
    	searchRecommendationsTask.then([deferral, &searchSuggestionCollection](task<IVector<StockViewerCP::DataModel::AStock^>^> task2)
    	{
    		
    			auto resp = task2.get();
    	
    			for(auto stock = begin(resp); stock != end(resp); ++stock)
    			{
    				char16 dest[20];
    				errno_t res =	_itow_s((*stock)->StockNumber,dest, sizeof(dest), 10);
    
    				Platform::String^ sn = ref new Platform::String(dest);
    
    				searchSuggestionCollection->AppendQuerySuggestion(sn);
    
    			}	
    			deferral->Complete();
    		
    	}, task_continuation_context::use_current());
    }
    

    Tuesday, October 02, 2012 5:30 PM

Answers

  • think I can answer this myself with more investigation.

    I haven't done much native stuff in years so bear with me.

    sizeof is the incorrect thing to use for the length to pass into _itow_s

    sizeof of a wchar_t aka char16 is double what the array size is so _itow_s is writing directly to memory and destroying the stock's m_v value or something else inside the stock variable that effects m_v.

    • Proposed as answer by Chris Guzak Thursday, October 04, 2012 5:14 AM
    • Marked as answer by Jesse Jiang Thursday, October 04, 2012 7:31 AM
    Wednesday, October 03, 2012 2:59 PM

All replies

  • Hi,

    Did you forget to add Search contract in Declarations which double click package.appxmanifest file in your project.
    Here is document for declarations
    http://msdn.microsoft.com/en-us/library/windows/apps/hh464906.aspx
    Here is Search contract sample
    http://code.msdn.microsoft.com/windowsapps/Search-app-contract-sample-118a92f5

    Hope them can help you.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, October 03, 2012 2:47 AM
  • You cannot capture a local variable by reference when creating a lambda for a task continuation. The continuation won't be called until the current function has returned, so the local variable (searchSuggestionCollection) won't be valid by the time you call it. Since searchSuggestionCollection is already a reference you don't want its address anyway. If you replace &searchSuggestionCollection with searchSuggestionCollection you should get the correct behavior.

    --Rob

    Wednesday, October 03, 2012 2:55 AM
    Owner
  • even with just searchSuggestionCollection  it still has the same exception.

    right on this line

    searchSuggestionCollection->AppendQuerySuggestion(sn);

    Wednesday, October 03, 2012 2:19 PM
  • I got it working but visual studio needs to do a better job saying where the error is.

    it was always erring on the searchSuggestionCollection but the error is in the stock iterator and the fact that for some reason _itow_s is destroying the vectoriterator memory address.

    if I take _itow_s out and use the normal ToString method it works.

    the _itow_s is making the m_v variable inside the stock iterator variable turn into address FEFEFEFE

    any ideas on why the _itow_s would be doing that?

    Wednesday, October 03, 2012 2:40 PM
  • think I can answer this myself with more investigation.

    I haven't done much native stuff in years so bear with me.

    sizeof is the incorrect thing to use for the length to pass into _itow_s

    sizeof of a wchar_t aka char16 is double what the array size is so _itow_s is writing directly to memory and destroying the stock's m_v value or something else inside the stock variable that effects m_v.

    • Proposed as answer by Chris Guzak Thursday, October 04, 2012 5:14 AM
    • Marked as answer by Jesse Jiang Thursday, October 04, 2012 7:31 AM
    Wednesday, October 03, 2012 2:59 PM