使用新方法分配的free(..)删除内存 [英] Deleting memory using free(..) allocated by new method

查看:81
本文介绍了使用新方法分配的free(..)删除内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Deleting memory using free(..) allocated by new method





我有什么试过:



使用free()方法删除新方法分配的内存。

我的VS2012没有崩溃mthread x64环境。

但它在客户端VS2012 mthread x64环境中崩溃。

据我所知,这是错误的。但在目前的情况下,我要重新创建崩溃。

在我的环境中重建崩溃所需的任何特定视觉工作室设置?



任何帮助都表示赞赏。



What I have tried:

Memory allocated by new method is deleted using free() method.
There is no crash in my VS2012 mthread x64 environment.
But it crashed in Clients VS2012 mthread x64 environment.
As i know, it is wrong.But in current situation, I've to recreate the crash.
Any specific visual studio setting needed to recreate the crash in my environment?

Any help is appreciated.

推荐答案

这是基本的C知识:



alloc =>免费

new =>删除



他们使用不同的内存模型作为内存,所以将它们混合是一个自新发明以来的错误



在多线程用例中,分配的同一个线程也必须释放内存。



请学习通过阅读文档来学习基础知识。
The is basic C-knowledge:

alloc => free
new => delete

they are using different memory models for the memory, so mixing them is a bug since the invention of new.

In multi-threading use case it is necessary that the same thread which allocates also frees the memory.

Please learn to learn the basics by reading the documentation.


否 - 没有崩溃我的应用程序设置。

崩溃是你编码的组合用户做了什么导致崩溃:这将特定于您的应用程序(可能还有该用户)。它不适用于任何其他应用程序(如果他以不同的顺序执行操作,则可能不适用于同一用户)。



和内存分配不是崩溃的应用程序的唯一原因,而不是长粉笔!



所以你需要收集关于用户做了什么的信息,尝试将其复制到看看你是否可能导致问题,如果没有开始查看他可能做了什么,或者你的代码中存在一些弱点,如果事情以错误的顺序或错误的时间完成,那么这些弱点可能会变得明显。 br />
我们不能为你做任何事情 - 你必须自己调试这个。
No - there is no "crash my app" setting.
The crash is a combination of what you coded with what the user did to cause the crash: and that will be specific to your application (and possibly to that user). It will not be applicable to any other application (and possibly not to the same user on a different day if he does things in a different order).

And memory allocation isn't the only cause for a crashed app, not by a long chalk!

So you need to gather information as to what the user did, try duplicating it to see if you can cause the problem, and if not start looking at what else he might have done, or if there is some weakness in your code that might become visible if things are done in the wrong order, or at the wrong time.
We can't do any of that for you - you will have to debug this one on your own.


好吧,你可以编写自己的分配器,这样 new malloc 不会共享实现,通过一些跟踪,您可能会发现不匹配。



这种功能甚至可能已存在于某个地方......可能找到内存泄漏的工具会检测到这些问题。



但实际上,如果你不编写自定义代码,崩溃的原因可能在解决方案2中指出的其他地方。如果你遇到崩溃,崩溃的可能性更大双重删除或缓冲区溢出,而不是混合免费



此外,如果内存分配 new [] ,则应使用 delete [] 进行释放如果在某处使用自定义内存分配,则应使用匹配的解除分配函数(总是成对)。



您还需要注意可能存在一些问题,如果您的应用程序使用多个DLL并且它们不使用相同的运行时库,并且在分配的同一DLL中没有进行解除分配。



在COM(Active / X)中),这样的亲通过实现 IUnknown 接口来解决问题,并正确使用它。因此可以通过不同的运行时间进行分配。
Well, you can write your own allocator such that new and malloc would not share the implementation and with some tracking you might be able to find mismatches.

That kind of functionality might even already exist somewhere... Probably that tools that find memory leaks would detect such problems.

But in practice, if you don't write custom code the cause of the crash is probably elsewhere as indicated in solution 2. And if you have a crash, there a more chances that the crash is caused by a double delete or a buffer overflow than a mix of free and new.

Also if memory is allocated with new[], it should be released with delete[] and if custom memory allocation is used somewhere then matching desallocation function should be used (always in pair).

You also need to be aware that there might be some problem if your application uses multiple DLLs and they don't use the same runtime libraries and desallocation is not done in the same DLL as the allocation.

In COM (Active/X), such problems where resolved by implementing IUnknown interface and use it properly. So it was possible to have allocation made by different run-times.


这篇关于使用新方法分配的free(..)删除内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆