2012-01-04 21 views
0

我對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); 
}; 
+0

和SomeObject是? – 2012-01-04 16:16:03

+1

看來你在你的文章中有錯別字。相反,**將您的調試測試用例**直接複製並粘貼到堆棧溢出。那麼我們不會浪費時間。 – 2012-01-04 16:20:59

+0

你是從多個線程訪問你的tjobs列表嗎?我沒有看到任何形式的線程安全訪問您的工作和列表本身的障礙我敢肯定不是線程安全的。 – 2012-01-04 16:22:48

回答

1

while while循環只在列表爲空時循環,然後嘗試刪除不存在的第一個元素。當然這會造成奇怪的行爲。或者更可能的是,您已經將物品放入隊列中,並且永遠不會將其拉出,這會導致它永遠增長。

因此,讓我們假設您沒有向我們展示您的真實代碼,但是您重新鍵入並忘記了!字符,並且while循環是正確的。

在這種情況下,你確定它實際上是泄漏?你的進程可能會使用更多的內存,但是如果它釋放它(暫時看起來是這樣),即使操作系統無法在htop中看到內存,進程也可以重用。您可以使用valgrind來更好地瞭解您是否真的在泄漏。

+0

是的,忘了「!」。不,我不確定它是否泄漏。不幸的是,我沒有在機器上安裝軟件包的權利......我會看到我可以使用valgrind在某處運行它。 – alex0ptr 2012-01-04 16:19:22

+0

@ nullpoint.er valgrind很容易從源代碼編譯,你不需要包管理器。 – 2012-01-04 16:25:53