Output aendert sich mit dem Auruf ueber Valgrind



  • Hallo.

    Das Verhalten meines Programms aendert sich mit/ohne Aufruf ueber Valgrinds. Es handelt sich dabei eigentlich nur um ein reines Testprogramm (also es macht nichts Sinnvolles).

    class TestClass
    {
    private:
      static std::list<TestClass*> self_list_;
    public:
      TestClass()
      {
        self_list_.push_back(this);
      }
      ~TestClass()
      {
      }
    };
    
    void testClass()
    {
      TestClass *ptr = new TestClass;
      delete ptr;
      ptr = 0;
    }
    

    Ausgabe ohne Valgrind:

    Output (ohne valgrind):
    --------------------------------------------------
    ::new(size:640)
    Memory allocated.
    ret(block:0x804b008)
    --------------------------------------------------
    ::new(size:1)
    Memory allocated.
    ret(block:0x804b290)
    --------------------------------------------------
    ::delete(base_ptr:0x804b290)
    Memory freed.
    ret(void)
    

    Ausgabe mit Valgrind:

    --------------------------------------------------
    ::new(size:12)
    Memory allocated.
    ret(block:0x4779028)
    --------------------------------------------------
    ::new(size:1)
    Memory allocated.
    ret(block:0x4779068)
    --------------------------------------------------
    ::new(size:12)
    Memory allocated.
    ret(block:0x47790a0)
    --------------------------------------------------
    ::delete(base_ptr:0x4779068)
    Memory freed.
    ret(void)
    --------------------------------------------------
    ::delete(base_ptr:0x47790a0)
    Memory freed.
    ret(void)
    --------------------------------------------------
    ::delete(base_ptr:0x4779028)
    Memory freed.
    ret(void)
    

    Das sollte alles wesentliche gewesen sein.

    Eventuell will auch jmd das selbst ausprobieren, dafuer hab ich alles schnell mal in ein File kopiert.

    #include <iostream>
    #include <list>
    
    using std::cout;
    using std::endl;
    using std::bad_alloc;
    using std::list;
    
    namespace
    {
      size_t max_alloc_block_size_ = 1000;
    };
    
    void *operator new(size_t size) throw(std::bad_alloc)
    {
      cout << "--------------------------------------------------" << endl;
      cout << "::new(size:" << size << ")" << endl;
    
      if (size > max_alloc_block_size_)
      {
        throw bad_alloc();
      }
    
      void *block = malloc(size);
    
      if (!block)
      {
        throw bad_alloc();
      }
    
      cout << "Memory allocated." << endl;
      cout << "ret(block:" << block << ")" << endl;
    
      return (block);
    }
    
    void operator delete(void *base_ptr) throw()
    {
      cout << "--------------------------------------------------" << endl;
      cout << "::delete(base_ptr:" << base_ptr << ")" << endl;
    
      if (!base_ptr)
      {
        return;
      }
    
      free(base_ptr);
    
      cout << "Memory freed." << endl;
      cout << "ret(void)" << endl;
    }
    
    class TestClass
    {
    private:
      static list<TestClass*> self_list_;
    public:
      TestClass()
      {
        self_list_.push_back(this);
      }
      ~TestClass()
      {
      }
    };
    
    list<TestClass*> TestClass::self_list_;
    
    void testClass()
    {
      TestClass *ptr = new TestClass;
      delete ptr;
      ptr = 0;
    }
    
    int main(int argc, char *argv[])
    {
      testClass();
    
      return (0);
    }
    

    Ich hoffe mir kann das jmd erklaeren.

    Vielen Danke und
    mfg mythso_



  • Kann es sein, dass Du das Programm mit zwei unterschiedlichen Implementierungen von std::list übersetzt hast. Einmal mit Debug und einmal ohne könnte schon reichen, um diesen Effekt zu erzeugen.

    Gruß
    Werner


Anmelden zum Antworten