我是 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 的四分之一),但操作系统和缓存可能无法很好地处理这个问题。
我迫切希望找到解决问题的方法。我的谷歌搜索根本没有帮助我。我希望你能做到。非常感谢任何帮助。
谢谢
(对不起我的英语)
Edit1:忘了说,它还没有输出 -> 我无法验证它是否有效
编辑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 更好地了解您是否真的在泄漏。
关于尝试删除指针链表的第一个元素时发生 C++ 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8730002/