一些奇怪的腐败问题

时间:2015-03-29 22:16:19

标签: c++ corruption

以下是我通过串口输出可视数据的winamp插件的一些片段

char * overrideCom = NULL;
char * cPortName;

void config(struct winampVisModule *this_mod)
{
    MessageBox(this_mod->hwndParent, cPortName, "Serial Port", MB_OK); // Tell us what the value is
}

// This function will convert a System^ string into a std::string
std::string makeStd(String ^in){
    array<Byte, 1> ^chars = System::Text::Encoding::ASCII->GetBytes(in);
    pin_ptr<Byte> charsPointer = &(chars[0]);
    char *nativeCharsPointer = reinterpret_cast<char *>(static_cast<unsigned char *>(charsPointer));
    std::string native(nativeCharsPointer, chars->Length);
    return nativeCharsPointer;
}

// This function will grab the second indexed com port and return it in an easily convertable std::string instead of String^
std::string getComPort(){
    array<String^, 1> ^serialPorts = nullptr;
    LPTSTR out;
    try {
        serialPorts = SerialPort::GetPortNames();
    }
    catch (Win32Exception^ ex)
    {
        MessageBox(NULL, "Failed to find COM port!","Initialize Failed!", MB_OK);
    }

    std::string portb = makeStd(serialPorts[1]);
    out = const_cast<char *>(portb.c_str());
    // Make sure I'm not crazy:
    MessageBox(NULL, out, "Com port!", MB_OK);
    return portb;
}

int init(struct winampVisModule *this_mod)
{
    config_read(this_mod);

    //std::string port = getComPort();
    //cPortName = const_cast<char *>(port.c_str());

    // This would normally work but for some reason causes corruption of the string and doesn't connect properly:

    if (overrideCom == NULL || overrideCom[0] == 0){
        std::string portx = getComPort();
        cPortName = const_cast<char *>(portx.c_str());
    }
    else
    {
        cPortName = overrideCom;
    }

return 0;
}

void config_getinifn(struct winampVisModule *this_mod, char *ini_file)
{   // makes a .ini file in the winamp directory named "plugin.ini"
    char *p;
    GetModuleFileName(this_mod->hDllInstance,ini_file,MAX_PATH);
    p=ini_file+strlen(ini_file);
    while (p >= ini_file && *p != '\\') p--;
    if (++p >= ini_file) *p = 0;
    strcat(ini_file,"plugin.ini");
}


void config_read(struct winampVisModule *this_mod)
{
    char ini_file[MAX_PATH];
    char* tResult = new char[255];
    config_getinifn(this_mod,ini_file);
    config_x = GetPrivateProfileInt(this_mod->description,"Screen_x",config_x,ini_file);
    config_y = GetPrivateProfileInt(this_mod->description,"Screen_y",config_y,ini_file);
    // Grab the overrideCom= param.
    GetPrivateProfileString(this_mod->description, "overrideCom", NULL, tResult, 255, ini_file);
    overrideCom = tResult;
}

当我在没有overrideCom参数的情况下运行代码时,它应该通过抓住它可以找到的第二个索引的com端口进行补偿,单独它可以正常工作:

std::string port = getComPort();
cPortName = const_cast<char *>(port.c_str());

但这不起作用:

if (overrideCom == NULL || overrideCom[0] == 0){
    std::string portx = getComPort();
    cPortName = const_cast<char *>(portx.c_str());
}
else
{
    cPortName = overrideCom;
}

端口没有连接,当我调用config()时,我收到一个消息框说:

Èêtw

当我省略上面的if语句时,config()输出:

COM3

我不明白!

另外, 当在INI文件中指定了overrideCom参数时,它可以很好地工作。

我的目标是不依赖于INI文件并允许它自动检测第二个COM端口,但如果需要,允许在INI中覆盖。


DOH!解决方案是将portx声明为全局变量。

1 个答案:

答案 0 :(得分:0)

std::string portx = getComPort();
cPortName = const_cast<char *>(portx.c_str());

在下一行portx被破坏,其数据也被破坏。所以cPortName只是一个悬而未决的指针。