试图删除指针的linkedlist的第一个元素时,C++内存泄漏
C++ Memory leak while trying to delete first element of linkedlist of pointers
我对C++还很陌生,两天来我遇到了一个大问题。我正试图用posix线程进行多线程渲染(光线投射、多采样、环境遮挡),每次运行该程序时,它都会消耗大约5GB的RAM(在线程启动后),直到终止。很明显我的记忆力有问题。我的工作线程是这样工作的:
struct Job
{
AOSampler sampler;
Ray ray;
bool abool;
int someint;
.
.
//no pointers here
};
//global
//use of C++ STL list
list<Job*> * jobs;
//Part of thread posix function starts here
list<Job*> tjobs;
// Mark 1
//Pushing and popping between "tjobs" the threadjobs just for this thread and the global jobpool "jobs". Of course threadsafe with mutex locking.
//The thread pops jobs from "jobs" and puts em into "tjobs"
while(!tjobs.empty())
{
//many calculations but all vars are on stack, besides creating new jobs an pushing them to some other queue, which will be pushed into "jobs" later
// !!!THE PROBLEM!!!
delete (tjobs.front());
tjobs.pop_front();
// The memory in htop always rises but never decreases!
}
// jumps to Mark 1
// end of multithread while
代码在许多核心上编译、运行和终止,但性能很差(4好,24坏,这是一台24核心机器)。我认为这可能是因为5GB的内存使用量(占所有phys.ram的四分之一),但操作系统和缓存可能无法很好地处理这一问题。
我迫切希望找到解决问题的办法。我的谷歌搜索对我一点帮助都没有。我希望你能帮上忙。非常感谢你的帮助。
谢谢
(对不起我的英语)
第1版:忘了提一下,它还没有输出->我无法验证它是否是有效的
第2版:一些标题:
class AOSampler
{
public:
AOSampler();
/// constructor that initializes the sampler, just calls init
AOSampler(vec3 const & normal, vec3 const & color);
/// initializes the sampler
void init(vec3 const & normal, vec3 const & color);
/// returns an importance sampled random direction and the associated weight
void sample(vec3 & sampledDirection, vec3 & sampleWeight) const;
private:
/// orthonormal basis
vec3 m_normal;
vec3 m_tangent;
vec3 m_bitangent;
/// diffuse color
vec3 m_color;
};
class Ray
{
public:
Ray() : tMin(0.001f), tMax(FLT_MAX){}
vec3 origin;
vec3 direction;
float tMin, tMax;
};
class vec3
{
public:
float x,y,z;
vec3();
vec3(float a, float b, float c);
/// assignment operator that assigns a single scalar value to all components
void operator=(float v);
/// unsafe element access
float operator[](unsigned int i) const
{
return (&x)[i];
}
/// length of the vector
float length() const;
///Returns a normalized version of the vector
vec3 normalize() const;
/// componentwise summation
vec3 add(const vec3& a) const;
/// componentwise subtraction
vec3 subtract(const vec3& a) const;
///compute the dot product which is cos(alpha) * this.Length * a.Length
///where alpha is the (smaller) angle between the vectors
float dot(const vec3& a) const;
float minComponent() const;
float maxComponent() const;
///computes a vector which is orthogonal to both of the input vectors
static vec3 cross(const vec3& a, const vec3& b);
static vec3 min(const vec3& a, const vec3& b);
static vec3 max(const vec3& a, const vec3& b);
/// add a vector to this vector
void operator+=( vec3 const & v );
/// subtract a vector from this vector
void operator-=( vec3 const & v );
};
while循环只在列表为空时循环,然后尝试删除不存在的第一个元素。当然,这会导致奇怪的行为。或者更有可能的是,您已经将项目放入队列,但从未将其拉出,这将导致它永远增长。
因此,让我们假设您并没有向我们展示您的真实代码,而是您重新键入并忘记了其中的!
字符,while循环是正确的。
在这种情况下,你确定它真的在泄漏吗?你的进程可能使用了更多的内存,但如果它释放了内存(暂时看起来是这样),即使操作系统在htop中看不到内存,进程也可以重用它。你可以使用valgrind让你更好地了解你是否真的在泄漏。
即使您可以访问源代码,也很难检测到内存泄漏。尝试使用泄漏检测工具,如Valgrind:http://www.cprogramming.com/debugging/valgrind.html这是一笔不错的投资。
相关文章:
- valgrind-hellgrind与泄漏检查的结果不同
- 从构造函数抛出异常时如何克服内存泄漏
- malloc() 可能出现内存泄漏
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 尽管遵循了规则,内存泄漏在哪里
- 为什么调用堆栈数组会导致内存泄漏
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 使用模板类的自定义列表类型中的内存泄漏
- 为什么以下C++代码中存在内存泄漏?
- Klocwork Inside的资源泄漏
- OpenCV 我应该使用智能指针来防止内存泄漏吗?
- 我是否生成线程并导致内存泄漏?
- 多线程程序中出现意外的内存泄漏
- 为什么此函数会导致内存泄漏?
- 在 C++ 库中使用cythonized python时内存泄漏
- 需要帮助查找内存泄漏
- C++功能泄漏内存,我是C++新手,不确定如何解决
- 瓦尔格林德的内存泄漏使用新的
- CPP 中的瓦尔格林德和记忆泄漏:"Conditional jump or move depends on uninitialised values"
- 无法找出我的代码中的内存泄漏