- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
亲爱的大家,这会很难:我创建了一个游戏对象工厂来生成我想要的对象。但是,我遇到无法修复的内存泄漏。
内存泄漏是由代码示例底部的return new Object();产生的。
static BaseObject * CreateObjectFunc()
{
return new Object();
}
如何以及在哪里删除指针?我写了 bool ReleaseClassType()。尽管工厂运作良好,但 ReleaseClassType() 并未修复内存泄漏。
bool ReleaseClassTypes()
{
unsigned int nRecordCount = vFactories.size();
for (unsigned int nLoop = 0; nLoop < nRecordCount; nLoop++ )
{
// if the object exists in the container and is valid, then render it
if( vFactories[nLoop] != NULL)
delete vFactories[nLoop]();
}
return true;
}
在查看下面的代码之前,让我帮助您了解我的 CGameObjectFactory 创建指向函数的指针 创建特定对象类型。指针存储在 vFactories vector 容器中。
我之所以选择这种方式,是因为我解析了一个对象映射文件。我有对象类型 ID(整数值),我需要将它们转换为真实对象。因为我有超过 100 种不同的对象数据类型,所以我希望避免连续遍历很长的 Switch() 语句。
因此,为了创建对象,我通过 CGameObjectFactory::create() 调用 vFactories'['nEnumObjectTypeID']'() 来调用生成所需对象的存储函数。
vFactories 中相应函数的位置与 nObjectTypeID 相同,因此我可以使用索引来访问该函数。
所以问题仍然存在,如何进行垃圾回收并避免报告的内存泄漏?
#ifndef GAMEOBJECTFACTORY_H_UNIPIXELS
#define GAMEOBJECTFACTORY_H_UNIPIXELS
//#include "MemoryManager.h"
#include <vector>
template <typename BaseObject>
class CGameObjectFactory
{
public:
// cleanup and release registered object data types
bool ReleaseClassTypes()
{
unsigned int nRecordCount = vFactories.size();
for (unsigned int nLoop = 0; nLoop < nRecordCount; nLoop++ )
{
// if the object exists in the container and is valid, then render it
if( vFactories[nLoop] != NULL)
delete vFactories[nLoop]();
}
return true;
}
// register new object data type
template <typename Object>
bool RegisterClassType(unsigned int nObjectIDParam )
{
if(vFactories.size() < nObjectIDParam) vFactories.resize(nObjectIDParam);
vFactories[nObjectIDParam] = &CreateObjectFunc<Object>;
return true;
}
// create new object by calling the pointer to the appropriate type function
BaseObject* create(unsigned int nObjectIDParam) const
{
return vFactories[nObjectIDParam]();
}
// resize the vector array containing pointers to function calls
bool resize(unsigned int nSizeParam)
{
vFactories.resize(nSizeParam);
return true;
}
private:
//DECLARE_HEAP;
template <typename Object>
static BaseObject * CreateObjectFunc()
{
return new Object();
}
typedef BaseObject*(*factory)();
std::vector<factory> vFactories;
};
//DEFINE_HEAP_T(CGameObjectFactory, "Game Object Factory");
#endif // GAMEOBJECTFACTORY_H_UNIPIXELS
最佳答案
So the question remains, how to proceed with garbage collection and avoid reported memory leaks?
考虑使用 std::shared_ptr
或 boost::shared_ptr管理您的 BaseObject
指针所有权。
关于c++ - 游戏对象工厂 : Fixing Memory Leaks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5189049/
我的应用有时会抛出奇怪的日志,例如: :通知名称“com.apple.iapd.notify.extendedMode”已被注册 40 次 - 这可能是泄漏:通知名称“com.apple.iapd.n
我在 dotcloud 上使用 redis 内存存储,但尽管 key 已过期,但它的 used_memory 再也不会下降。从 redis-cli 使用 flushdb 或 flushall 不会导致
所以我一直在寻找这个问题的解决方案有一段时间了。我编写了一个程序来从两个单独的文本文件中获取数据,对其进行解析,然后输出到另一个文本文件和一个 ARFF 文件以供 Weka 分析。我遇到的问题是我编写
随着我继续学习 Rust,我正在从事一个涉及广泛使用谓词函数的项目。我决定用 Rust 闭包实现这些谓词,例如: type Predicate = Box bool> . 我的程序使用应用于这些谓词的
我的应用程序中有一个非常简单的 CUDA 组件。 Valgrind 报告了很多泄漏和仍然可以访问的问题,所有这些都与 cudaMalloc 调用有关。 这些泄漏是真的吗?我打电话cudaFree每 c
在 chrome latest 和其他浏览器中测试。此页面启动一个 timer() 每 60 秒刷新一次。在 init() 和每个 refresh() 上,它从服务器获取数据并在页面中显示相同的数据。
我正在尝试使用 pprof 来验证内存泄漏。 能否解释一下如何阅读您在以下位置找到的堆配置文件:http://localhost:6060/debug/pprof/heap?debug=1 此外,在启
我们遇到了由 GroovyShell/Groovy 脚本引起的内存泄漏(请参阅最后的 GroovyEvaluator 代码)。主要问题是(从 MAT 分析器复制粘贴): The class "java
我在 Ubuntu 中使用带有延迟长度字符变量的 gfortran 8.4,如下例所示: PROGRAM test IMPLICIT NONE CHARACTER(LEN=:),ALLOC
我正在阅读 Efficient Android Threading,上面写着 Instead of using nested classes with outer class references,
我们最近将我们的应用程序从 Java 7 迁移到 Java 8。从切换之日起,我们开始看到内存不足的元空间问题。我们尝试增加元空间空间大小,但没有帮助。 Jvisual VM(和 Jconsole)显
使用最新版本的 FFmpeg 读取图像文件时我遇到了内存泄漏,我无法追踪。 好像填完AVFrame与 avcodec_send_packet和 avcodec_receive_frame ,我调用av
我们有某种泄漏,我不明白其性质。 Gen0/1/2 堆的大小不会增加,但是工作集会增加,直到我们 OOM。 DebugDiag 告诉我 CLR.DLL 拥有不断增加的内存,并告诉我我们有一个不断增长的
一位系统管理员告诉 med,内存泄漏是不可见的。即,如果应用程序正在泄漏内存,则应用程序使用的所有内存可能在任务管理器或等效工具中不可见。 他似乎对此非常肯定。我一直认为在任务管理器(或进程资源管理器
我们正在自动化大多数可以自动化的已知问题(用于回归测试)。但是,我们确实通过第三方软件发现了内存泄漏。但是,我不知道如何自动执行我们发现并修复它们的内存泄漏测试。 有什么建议吗? 最佳答案 通常最好的
如果我有一个垃圾收集器来跟踪分配的每个对象并在它们不再具有对它们的可用引用时立即释放它们,您是否仍然存在内存泄漏? 考虑到内存泄漏是没有任何引用的分配不是不可能的还是我错过了什么? 编辑:所以我算作内
我有一个托管在 IIS 中的 WCF 应用程序(用 C#/.Net 4 编写)。随着时间的推移,进程的句柄计数或多或少以线性方式增加(在进程回收之前增加到 30,000)。根据 SysInternal
我从LeakSanitizer工具获得以下输出。正如工具所了解的那样,直接泄漏和间接泄漏有什么区别? 13: ==29107==ERROR: LeakSanitizer: detected memor
首先我是这样跑的: valgrind --leak-check=full --show-leak-kinds=all test_proc 和日志: ==30349== For counts of de
我知道每个进程都有操作系统分配的自己的地址空间。所以当程序终止时,整个地址空间都被标记为无效(或者可以再次自由重用)。现在,如果上述进程正在泄漏内存,那么在程序终止后会产生什么影响吗? 也就是说,如果
我是一名优秀的程序员,十分优秀!