查找内存分配错误

发布于 2024-08-18 17:51:52 字数 1299 浏览 9 评论 0原文

我在以下简化代码中遇到内存分配错误(以及随后的崩溃):

std::wstring myKey = L"str_not_actually_constant";

MyType obj;
Read( obj );

std::map<std::wstring, MyType> myMap;
myMap[myKey] = obj; // Sometimes allocation error (1)
...
Read( MyType& obj )
{
  obj.member1 = ReadFromFuncThatMayBeProblem();
  obj.member2 = ReadFromFuncThatMayBeProblem(); // Sometimes allocation error (2)
  /* more members */
}
...
void operator =( const MyType& source )
{
  if( this != &source )
  {
    member1 = source.member1; // std::wstring
    member2 = source.member2; // Usually (1) happen on the second member. // std::wstring
    /* more members */
  }
}

发生(1)或(2)。

现在,如果我只是继续,不管错误如何(使用调试器),该值确实会输入到映射中。

我不知道 ReadFromFuncThatMayBeProblem() 是否是罪魁祸首,但它是一个相当复杂的函数,我不能在这里透露。

此外,在将应用程序的其他部分移植到使用 OpenSSL 之前,该代码已经有效(或至少看起来有效)。不过,我不知道这是否会对这里产生任何影响。

那么,既然我假设上面的代码实际上不是问题所在,我该怎么做才能追踪这个分配错误呢?

编辑:更多信息: MyType 没有 dtor。

但是,MyType 有一个 SecondType 类型的成员,该成员有一个 void* 成员。这将在该类型的析构函数中被删除并置空。构造函数使用 m_pData = new std::wstring( ( (std::wstring )source.m_pData) );对于字符串。 (其他数据类型也类似)。这会是一个问题吗? (delete static_cast< std::wstring* >( m_pData );)

MyType 的其他成员类型有 std::wstring、unsigned long、bool、enum、structs(其中有 timeb)和 SecondType。

I'm getting memory allocation errors (and a subsequent crash) on the following simplified code:

std::wstring myKey = L"str_not_actually_constant";

MyType obj;
Read( obj );

std::map<std::wstring, MyType> myMap;
myMap[myKey] = obj; // Sometimes allocation error (1)
...
Read( MyType& obj )
{
  obj.member1 = ReadFromFuncThatMayBeProblem();
  obj.member2 = ReadFromFuncThatMayBeProblem(); // Sometimes allocation error (2)
  /* more members */
}
...
void operator =( const MyType& source )
{
  if( this != &source )
  {
    member1 = source.member1; // std::wstring
    member2 = source.member2; // Usually (1) happen on the second member. // std::wstring
    /* more members */
  }
}

Either (1) or (2) occur.

Now, if I simply continue on regardless of the error (with the debugger), the value is indeed entered in the map.

I don't know if ReadFromFuncThatMayBeProblem() is the culprit but it's a fairly complex function that I cannot devulge here.

Also, this is code that has worked (or at least appeared to work) before other sections of the application was ported to use OpenSSL. I don't know if that may have had any effect here, though.

So, what can I do to track down this allocation error, since I'm presuming that the above code is not in fact the problem?

Edit: More info:
There is no dtor for MyType.

However, MyType has a member of type SecondType that has a void* member. This is being deleted and null'd in that type's destructor. The constructor uses m_pData = new std::wstring( ( (std::wstring )source.m_pData) ); for strings. (And similar for other data types). Could that be an issue? (delete static_cast< std::wstring* >( m_pData );)

The other member types of MyType are std::wstring, unsigned long, bool, enum, structs (timeb among them) and SecondType.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

噩梦成真你也成魔 2024-08-25 17:51:52

终于找到了错误所在。

我们使用上述功能作为使用 OpenSSL 的更大套接字通信的一部分(因此上面的参考文献)。套接字按照上面的代码简化写入数据和读取数据。

读取套接字的方式是我们将内存从一个缓冲区重新分配到另一个缓冲区(动态更改大小)。在执行此操作时,我们使用缓冲区的输入以及我们应该扩展的大小。尺寸计算使用模数来计算调整尺寸的因子。这导致缓冲区太大或太小而无法适应后续操作。

经过两天的调试,将“%”更改为“/”。

不过还是感谢大家的支持。

Finally tracked down the error.

We are using the above functionality as part of a larger socket communication using OpenSSL (hence above reference). The socket was writing data and reading data as per above code simplification.

The way the socket was read was that we were re-allocating memory from one buffer to another (changing size dynamically). While doing this, we use the input of the buffer and the size we should expand with. The size calculation was using modulo to calculate the factor of the re-size. This caused the buffer to either be too large or too small to fit the following operations.

Two days of debugging to change a '%' to a '/'.

Thanks for all support, though.

—━☆沉默づ 2024-08-25 17:51:52

ReadFromFuncThatMayBeProblem() 返回什么类型?它返回一个(常量)引用吗?如果是这样,在离开 ReadFromFuncThatMayBeProblem() 的范围后该对象仍然有效吗?

what type does ReadFromFuncThatMayBeProblem() return? Does it return a (const) reference? if so, is the object still valid after leaving the scope of ReadFromFuncThatMayBeProblem()?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文