2015-10-17 145 views
-1

我一直在努力處理內存泄漏,我正在編寫一個程序來讀取某些圖像的顏色。內存泄漏問題C++程序

代碼:http://ideone.com/dcU5Su

問題:我無法找出內存泄露/泄漏的根源。

我試過的是什麼:我通過Valgrind運行程序。當中很多unprocessible信息,下面是錯誤,我可以讓出:

  • Invalid write of size 4 [有這些3]
  • Conditional jump or move depends on uninitialised value(s)
  • Syscall param write(buf) points to uninitialised byte(s)

綜觀上述錯誤,我認爲這個問題與不正確的初始化有關。但我無法看到在哪裏。

+0

請解釋原因之前,向下投票這個問題,我在這裏是新的,所以請體諒,並幫助我改進 – Harry

+0

這是所有這些有缺陷的代碼,它需要一個更大的文章來告訴什麼。 –

+1

請在這裏詢問之前使用調試器,或抱怨反對票。 –

回答

1

你不應該這樣做手動內存管理。使用向量:

#include <iostream> 
#include <sstream> 
#include <vector> 

struct Image { 
    unsigned int l; 
    unsigned int b; 
    unsigned int h; 
}; 

int main() 
{ 
    using namespace std; 

    std::vector<Image> image; 

    std::string line; 
    while(getline(cin,line)) 
    { 
     if(rand()%2) 
     { 
      istringstream iss(line); 

      Image img; 
      while (iss >> img.l >> img.b >> img.h) 
      { 
       image.push_back(img); 
      } 
     } 
    } 
} 

更新

既然你沒有提供反饋(爲什麼delete[]似乎是你的樣品中環內),我所能做的最好是張貼重構的建議包括修復/改進,我會做:

Live On Coliru

#include <iostream> 
#include <sstream> 

#include <cstring> 
#include <cassert> 

struct Image { 
    unsigned int l; 
    unsigned int b; 
    unsigned int h; 
}; 

class Images { 
    private: 
    size_t capacity; 
    size_t size; 

    Image *images; 

    Images& operator=(Images const&); // not supported 
    Images(Images const&);   // not supported 

    void autogrow() { 
     if (size >= capacity) { 
      int newCapacity = capacity * 2; 
      Image* newImage = new Image[newCapacity]; 
      std::cout << "growing " << capacity << " -> " << newCapacity << "\n"; 

      //only available in c++11: 
      static_assert(std::is_pod<Image>::value, "you're screwed"); 
      memcpy(newImage, images, size * sizeof(Image)); 

      capacity = newCapacity; 
      delete[] images; 
      images = newImage; 

     } 
    } 

    public: 
    Images() : capacity(1), size(0), images(new Image[capacity]) { 
     assert(images); 
    }; 
    ~Images() { 
     delete[] images; 
    } 

    Image& insert(Image const& img) { 
     autogrow(); 
     assert(size<capacity); 
     return images[size++] = img; 
    } 

}; 

int main() 
{ 
    using namespace std; 

    Images collection; 

    std::string line; 
    while(getline(cin,line)) 
    { 
     if(true) { 
      istringstream iss(line); 

      Image cur; 
      while (iss >> cur.l >> cur.b >> cur.h) { 
       collection.insert(cur); 
      } 
     } 
    } 
} 

打印,例如

od /dev/urandom -Anone -t u4 -w36 | head -1000 | ./a.out 
growing 1 -> 2 
growing 2 -> 4 
growing 4 -> 8 
growing 8 -> 16 
growing 16 -> 32 
growing 32 -> 64 
growing 64 -> 128 
growing 128 -> 256 
growing 256 -> 512 
growing 512 -> 1024 
growing 1024 -> 2048 
growing 2048 -> 4096 
+0

仍然看着你的代碼給出一個更具體的提示什麼是錯的(https://www.livecoding.tv/sehe/,你的SSCCE壞了) – sehe

+0

我不想使用一個向量,你能否建議我最新的問題是什麼實現 – Harry

+2

@Harry爲什麼你不想使用'std :: vector'? –

0

我想,當while (iss >> image[j].l >> image[j].b >> image[j].h)通過的第二次迭代路過的時候while循環中,image指針是無效的,因爲你在上一次迭代中刪除它被執行。

所以,delete[]第二次(內部while循環後),您應該重置變量荷蘭國際集團image(就像你重新entring外while環路後的第一次。

// reset 
delete[] (image); 
capacity = 1; 
size = 0; 
j = 0; 
image = new Image[capacity]; 

或者,你應該把這個「重置」塊放在外圍的while的開頭(並且在它之前擺脫初始化)

我不知道整個程序的邏輯,但是我想假設這是所需的行爲...

編輯:所以這個問題可以通過移動size++;略高於if (size >= capacity)來解決(榮譽給@sehe尋找記憶違反:)的由來)

size++; 
if (size >= capacity) 
{ 
+0

我認爲這個樣本太破碎了,沒有得出實際的結論,但我在我的回答中將它重構爲一個工作混亂:) – sehe

+0

請看看這裏:http://ideone.com/dcU5Su – Harry