0

基本的に、Managedクラス(UWP)の変数を変更しようとすると、クラッシュします。また、アプリケーションの名前空間で作成された変数を変更しようとするとクラッシュするだけです。言い換えれば、新しい名前空間とマネージクラスを作成すると、変数をうまく修正できます。管理されたC++クラスがcreate_taskでクラッシュする

crash

void ASynTaskCategories(void) 
    { 
     concurrency::task_completion_event<Platform::String^> taskDone; 
     MainPage^ rootPage = this; 
     UberSnip::HELPER::ASYNC_RESPONSE^ responseItem = ref new UberSnip::HELPER::ASYNC_RESPONSE(); 
     create_task([taskDone, responseItem, rootPage]() -> Platform::String^ 
     { 

      //UBERSNIP API 0.4 
      UberSnip::UBERSNIP_CLIENT* UberSnipAPI = new UberSnip::UBERSNIP_CLIENT(); 

      UberSnipAPI->Http->RequestURL = "http://api.ubersnip.com/categories.php"; 
      UberSnipAPI->Http->request(); 
      taskDone.set(UberSnipAPI->Client->BodyResponse); 
      responseItem->Body = UberSnipAPI->Client->BodyResponse; 
      return "(done)"; 

     }).then([taskDone, responseItem, rootPage](Platform::String^ BodyResponse) { 
      Platform::String^ BR = responseItem->Body; 


      cJSON* cats = cJSON_Parse(_string(BR)); 
      cats = cats->child; 
      int *cat_count = new int(cJSON_GetArraySize(cats)); 
      rootPage->Categories->Clear(); 

      GENERIC_ITEM^ all_item = ref new GENERIC_ITEM(); 
      all_item->Title = "All"; 

      rootPage->Categories->Append(all_item); 
      for (int i = 0; i < *cat_count; i++) { 
       cJSON* cat = new cJSON(); 
       cat = cJSON_GetArrayItem(cats, i); 

       string *track_title = new string(cJSON_GetObjectItem(cat, "name")->valuestring); 

       GENERIC_ITEM gitem; 
       gitem.Title = "Hi"; 

       GENERIC_ITEM^ ubersnipCategory = ref new GENERIC_ITEM(); 
       ubersnipCategory->Title = _String(*track_title); 

       rootPage->Categories->Append(ubersnipCategory); 
      } 
     }); 

これは

UberSnipAPI->Http->RequestURL = "http://api.ubersnip.com/categories.php"; 

をクラッシュしませんが、この1は

all_item->Title = "All"; 

私はそれはそれはアプリのデフォルトであることとは何かを持って、ほぼ正午前を行います名前空間とそれはメインスレッドの外でアクセスされている...少なくともそれはそれが何であるかそれは実際のクラス以外の唯一の違いです。

これはGENERIC_ITEMの外観です。

[Windows::UI::Xaml::Data::Bindable] 
public ref class GENERIC_ITEM sealed { 
private: 
    Platform::String^ _title = ""; 
    Platform::String^ _description; 
    Windows::UI::Xaml::Media::ImageSource^ _Image; 

    event PropertyChangedEventHandler^ _PropertyChanged; 

    void OnPropertyChanged(Platform::String^ propertyName) 
    { 
     PropertyChangedEventArgs^ pcea = ref new PropertyChangedEventArgs(propertyName); 
     _PropertyChanged(this, pcea); 
    } 
public: 
    GENERIC_ITEM() { 

    }; 

    property Platform::String^ Title { 
     Platform::String^ get() { 
      return this->_title; 
     } 

     void set(Platform::String^ val) { 
      this->_title = val; 
      OnPropertyChanged("Title"); 
     } 
    } 

    property Platform::String^ Description { 
     Platform::String^ get() { 
      return this->_description; 
     } 

     void set(Platform::String^ val) { 
      this->_description = val; 
      OnPropertyChanged("Description"); 
     } 
    } 

    void SetImage(Platform::String^ path) 
    { 
     Windows::Foundation::Uri^ uri = ref new Windows::Foundation::Uri(path); 
     _Image = ref new Windows::UI::Xaml::Media::Imaging::BitmapImage(uri); 
    } 

}; 

私は最初のスレッドでこの全く同じコードを実行すると完全に正常に動作するため、クラスに問題はないことを知っています。

提案がありますか?ありがとう! :D

答えて

0

数時間後にそれを見つけてください!他の誰かがこの問題を抱えている場合は、ディスパッチャを使用してUIスレッドの外部で利用できないコードをUI上で実行するだけです。

void loadCats(UberSnip::HELPER::ASYNC_RESPONSE^ responseItem, MainPage^ rootPage) { 
    Platform::String^ BR = responseItem->Body; 


    cJSON* cats = cJSON_Parse(_string(BR)); 
    cats = cats->child; 
    int *cat_count = new int(cJSON_GetArraySize(cats)); 
    rootPage->Categories->Clear(); 

    GENERIC_ITEM^ all_item = ref new GENERIC_ITEM(); 
    all_item->Title = "All"; 

    rootPage->Categories->Append(all_item); 
    for (int i = 0; i < *cat_count; i++) { 
     cJSON* cat = new cJSON(); 
     cat = cJSON_GetArrayItem(cats, i); 

     string *track_title = new string(cJSON_GetObjectItem(cat, "name")->valuestring); 

     GENERIC_ITEM gitem; 
     gitem.Title = "Hi"; 

     GENERIC_ITEM^ ubersnipCategory = ref new GENERIC_ITEM(); 
     ubersnipCategory->Title = _String(*track_title); 

     rootPage->Categories->Append(ubersnipCategory); 
    } 
} 

void ASyncTaskCategories(void) 
{ 
    concurrency::task_completion_event<Platform::String^> taskDone; 
    MainPage^ rootPage = this; 
    UberSnip::HELPER::ASYNC_RESPONSE^ responseItem = ref new UberSnip::HELPER::ASYNC_RESPONSE(); 

    auto dispatch = CoreWindow::GetForCurrentThread()->Dispatcher; 
    auto op2 = create_async([taskDone, responseItem, rootPage, dispatch] { 
     return create_task([taskDone, responseItem, rootPage, dispatch]() -> Platform::String^ 
     { 

      //UBERSNIP API 0.4 
      UberSnip::UBERSNIP_CLIENT* UberSnipAPI = new UberSnip::UBERSNIP_CLIENT(); 

      UberSnipAPI->Http->RequestURL = "http://api.ubersnip.com/categories.php"; 

      try{ 
       UberSnipAPI->Http->request(); 
      } 
      catch (...) { 
       printf("Error"); 
      } 

      int err = UberSnip::UTILS::STRING::StringToAscIIChars(UberSnipAPI->Client->BodyResponse).find("__api_err"); 

      if (err < 0) { 
       if (UberSnip::UTILS::STRING::StringToAscIIChars(UberSnipAPI->Client->BodyResponse).length() < 3) { 
        return; 
       } 
      } 
      taskDone.set(UberSnipAPI->Client->BodyResponse); 
      responseItem->Body = UberSnipAPI->Client->BodyResponse; 
      dispatch->RunAsync(Windows::UI::Core::CoreDispatcherPriority::High, ref new Windows::UI::Core::DispatchedHandler([=]() 
      { 
       rootPage->loadCats(responseItem, rootPage); 
      })); 

      for (int i = 0; i < 100;) { 

      } 


      return "(done)"; 

     }).then([taskDone, responseItem, rootPage](Platform::String^ BodyResponse) { 
     }); 

    }); 

     //rootPage->loadCats(responseItem, rootPage); 
} 
関連する問題