0

以下代码在 Marmalade 模拟器中工作(我在 OSX 上使用 x-code)

bool PictureDictionary::OnTableSelect(CTable* table, int tab){
    //if something is selected, look up the item, and display it
    //also change the search to the selected item
    if(-1 < tab){

        // if a term is selected, set the search text field to the term
        CString term = m_SearchResults.GetString(tab);

        if(m_currentWord != (char*)term.Get()){
            m_currentWord = (char *)term.Get();
            m_searchTextField->SetAttribute("text", term);

            char* normalizedTerm = (char *)term.Get();
            char* imagePath;
            sprintf(imagePath,"images/%s.jpg", normalizedTerm);

            if(m_ImageAttached){
                m_Image->SetAttribute("image", (const char*)imagePath);
            } else {
                m_Image = CreateImage(CAttributes()
                                      .Set("name",    "picture")
                                      .Set("x1",      "0")
                                      .Set("x2", "0")
                                      .Set("y1",      "50%")
                                      .Set("image", (const char*)imagePath)
                                      );
                m_SearchView->AddChild(m_Image);
                m_ImageAttached = true;
            }
        }
    }
    return true;
}

当我运行模拟器并从表中选择一个项目时,图像会出现,并且当我选择不同的项目时会发生变化。当我去重构时,我得到一个 EXC_BAD_ACCESS (code=1…..) 错误

bool PictureDictionary::OnTableSelect(CTable* table, int tab){
    //if something is selected, look up the item, and display it
    //also change the search to the selected item
    if(-1 < tab){

        // if a term is selected, set the search text field to the term
        CString term = m_SearchResults.GetString(tab);

        if(m_currentWord != (char*)term.Get()){
            m_currentWord = (char *)term.Get();
            m_searchTextField->SetAttribute("text", term);        

            char* normalizedTerm = (char *)term.Get();
            char* imagePath;
            sprintf(imagePath,"images/%s.jpg", normalizedTerm);

            UpdatePictureView(imagePath);
        }
    }
    return true;
}

void PictureDictionary::UpdatePictureView(char* imagePath){
    if(m_ImageAttached){
        m_Image->SetAttribute("image", (const char*)imagePath);
    } else {
        m_Image = CreateImage(CAttributes()
                              .Set("name",    "picture")
                              .Set("x1",      "0")
                              .Set("x2", "0")
                              .Set("y1",      "50%")
                              .Set("image", (const char*)imagePath)
                              );
        m_SearchView->AddChild(m_Image);
        m_ImageAttached = true;
    }
}

关于如何在不出现这些问题的情况下清理代码的任何建议?

编辑关于未初始化变量的 RE 评论: m_ImageAttached 在构造函数中被初始化为 false,除非我做错了什么。此外,更改条件以检查 m_Image!=NULL 是否也会引发相同的错误。

主.cpp:

PictureDictionary pictDict(myApp, &dictionary);

图片字典的构造函数:

PictureDictionary::PictureDictionary(CAppPtr app,Dictionary::Dictionary* dictionary){
    m_App = app;
    m_Dictionary = dictionary;
    m_currentWord = "";
    m_ImageAttached = false;
}
4

1 回答 1

1

imagePath是一个统一的指针,在两个片段中。任何取消引用的尝试都是未定义的行为。它似乎在第一个片段中起作用。使用数组或填充 astd::string代替:

std::string imagePath(std::string("images/") + normalizedTerm + ".jpg");

如果std::string::c_str()需要访问底层const char*,则使用。

于 2013-03-15T16:38:44.667 回答