重构后的EXC_BAD_ACCESS问题

时间:2013-03-15 10:47:37

标签: c++ marmalade

以下代码适用于Marmalade模拟器(我使用x-code在OSX上)

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(代码= 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是否也会引发相同的错误。

main.cpp中:

PictureDictionary pictDict(myApp, &dictionary);

PictureDictionary的构造函数:

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

1 个答案:

答案 0 :(得分:2)

imagePath是两个片段中的一个整体指针。任何取消引用的尝试都是未定义的行为。它只是在第一个片段中出现 work 。使用数组或填充std::string代替:

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

如果需要访问基础std::string::c_str(),请使用const char*