修复windows dll中的内存泄漏 继承了一些看起来像C++或.NET的代码?这不在我的驾驶室里。无论如何,它会通过使用堆内存,然后反复重新分配本地内存而导致内存泄漏: CPTRecord* pRec = static_cast<CPTRecor

修复windows dll中的内存泄漏 继承了一些看起来像C++或.NET的代码?这不在我的驾驶室里。无论如何,它会通过使用堆内存,然后反复重新分配本地内存而导致内存泄漏: CPTRecord* pRec = static_cast<CPTRecor,c++,string,dll,mfc,C++,String,Dll,Mfc,修复windows dll中的内存泄漏 继承了一些看起来像C++或.NET的代码?这不在我的驾驶室里。无论如何,它会通过使用堆内存,然后反复重新分配本地内存而导致内存泄漏: CPTRecord* pRec = static_cast<CPTRecord*>(pData); // Remove channel # from end of test name CString testName = pRec->m_stdf.test_txt; int strIndex = tes

修复windows dll中的内存泄漏 <>继承了一些看起来像C++或.NET的代码?这不在我的驾驶室里。无论如何,它会通过使用堆内存,然后反复重新分配本地内存而导致内存泄漏:

CPTRecord* pRec = static_cast<CPTRecord*>(pData);

// Remove channel # from end of test name
CString testName = pRec->m_stdf.test_txt;

int strIndex = testName.ReverseFind(' ');
if (strIndex != -1)
{
    testName.Delete(strIndex,9);
    pRec->m_stdf.test_txt = (char *)LPCTSTR(testName);
    pRec->m_stdf.test_txt_size = testName.GetLength();
}
CPTRecord*pRec=static_cast(pData);
//从测试名称末尾删除通道#
CString testName=pRec->m_stdf.test_txt;
int strIndex=testName.ReverseFind(“”);
如果(strIndex!=-1)
{
testName.Delete(strIndex,9);
pRec->m_stdf.test_txt=(char*)LPCTSTR(testName);
pRec->m_stdf.test_txt_size=testName.GetLength();
}

我不确定如何使用指针,或者使用<代码> MeMCPY < /C>,而不是使用< C++ >(char)LPCTSTR < /Calp><:你认为这里哪里有漏洞?如果动态分配了

test\u text
,则只需在分配新值之前删除它即可。更好的是;仅在需要时使用
std::string
(或
std::wstring
)并创建
LPCTSTR
。此外,这是C++,所以没有太多的理由使用<代码> Malc 和 Fuff LPCTSTR实际上是Windows API构造。它大致代表“指向常量T字符串的长指针”。请注意,T字符串可以由char或wchar\u T构建,具体取决于编译器标志(请参阅Windows和Unicode支持)。因此,从一开始就对char*进行强制转换是非常不安全的,因为它可能是另一个构建中的wchar\t。出于同样的原因,从CString构造LPCTSTR也是可疑的。可能会有帮助。这是因为上面写满了各种各样的错误。讽刺的是,这段代码中没有内存泄漏。任何类型的唯一动态分配都发生在CString内部,并且它受范围保护。但这段代码所做的有很多错误。我想从这个开始。WhozCraig-那真的没有帮助。你会改变什么?为什么?