我對C++很陌生,現在我遇到了一個大問題。 我想用posix線程來執行多線程渲染(raycasting,multisampling,ambientocclusion),並且每次運行程序時,它都會消耗大約5GB的RAM(啓動線程後)直到終止。所以顯然我有一個內存泄漏。我的工作線程的工作是這樣的:C++內存泄露,同時嘗試刪除指針鏈表的第一個元素
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內存使用率(所有內存使用率的四分之一),但是操作系統和緩存可能無法很好地處理這個問題。
我很想找到解決我的問題。我的谷歌搜索根本沒有幫助我。我跳你可以做。任何幫助,高度讚賞。
謝謝
(對不起,我的英語)
EDIT1:忘了提,它沒有輸出尚未 - >我無法驗證它是否有效
EDIT2: 一些標題:
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);
};
和SomeObject是? – 2012-01-04 16:16:03
看來你在你的文章中有錯別字。相反,**將您的調試測試用例**直接複製並粘貼到堆棧溢出。那麼我們不會浪費時間。 – 2012-01-04 16:20:59
你是從多個線程訪問你的tjobs列表嗎?我沒有看到任何形式的線程安全訪問您的工作和列表本身的障礙我敢肯定不是線程安全的。 – 2012-01-04 16:22:48