indirectly lost and definitely lost memory leak

Can someone please point to me where is the momory leak in the below code.

thread.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// includes...
9  struct DataStruct
10  {
11      int v1;
12      int v2;
13      string v3;
14      void *thisclass;
15  };
16
17  class MyClass
18  {
19      public:
20            void entry();
21      private:
22            static void *Thread_test(void *arg);
23            void func(int v1, int v2, string v3);
24  };

thread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
1   #include "threadtest.h"
2   void *MyClass::Thread_test(void *arg)
3   {
4         DataStruct *p = (DataStruct*) arg;
5         MyClass *pt = (MyClass*) p->thisclass;
6         pt->func(p->v1, p->v2, p->v3);
7         pthread_detach(pthread_self());
8         return NULL;
9   }
10  void MyClass::func(int v1, int v2, string v3) { cout << ". "; }
11  void MyClass::entry() {
12        pthread_t ThreadID[10];
13        int i = 0;
14        while (1) {
15             if (i < 9) {
16                  DataStruct *inDATA = new DataStruct();
17                  inDATA->v1 = 1;
18                  inDATA->v2 = 2;
19                  inDATA->v3 = "3";
20                  inDATA->thisclass = this; 
21                  if (pthread_create(&ThreadID[i], NULL, Thread_test, (void*) inDATA) != 0)
22                        cout << "Thread error" << endl;
24                  inDATA = NULL;
25                  delete inDATA;
26                  i++;
27            }
28            else {
29                   for (int th=0;th<i;th++)
30                          if (ThreadID[th])
31                                 pthread_join(ThreadID[th], NULL);
32                  break;
33            }
34        }
35  }
36  int main()
37  {
38          MyClass *mc = new MyClass();
39          mc->entry();
40          mc = NULL;
41          delete mc;
42          return 0;
43  }


The following is the output of valgrind:


==23501== 1 bytes in 1 blocks are indirectly lost in loss record 1 of 3
==23501==    at 0x4A06D5C: operator new(unsigned long) (vg_replace_malloc.c:230)
==23501==    by 0x4010DB: main (threadtest.cpp:38)
==23501== 
==23501== 
==23501== 451 (216 direct, 235 indirect) bytes in 9 blocks are definitely lost
 in loss record 2 of 3
==23501==    at 0x4A06D5C: operator new(unsigned long) (vg_replace_malloc.c:230)
==23501==    by 0x400F75: MyClass::entry() (threadtest.cpp:16)
==23501==    by 0x4010E8: main (threadtest.cpp:39)
==23501== 
==23501== 
==23501== 234 bytes in 9 blocks are indirectly lost in loss record 3 of 3
==23501==    at 0x4A06D5C: operator new(unsigned long) (vg_replace_malloc.c:230)
==23501==    by 0x3B33CA1650: std::string::_Rep::_S_create(unsigned long,
 unsigned long, std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.10)
==23501==    by 0x3B33CA32ED: std::string::_M_mutate(unsigned long, unsigned
 long, unsigned long) (in /usr/lib64/libstdc++.so.6.0.10)
==23501==    by 0x3B33CA34AB: std::string::_M_replace_safe(unsigned long, 
unsigned long, char const*, unsigned long) (in /usr/lib64/libstdc++.so.6.0.10)
==23501==    by 0x400FBA: MyClass::entry() (threadtest.cpp:19)
==23501==    by 0x4010E8: main (threadtest.cpp:39)


Indirectly lost could be false, but definitely lost is a memory leak! Why?

Thanks in advance
Last edited on
On line 24/25 of the second file, you NULL the pointer before you delete it, losing what it points to.
Oh yeah! Thanks
I removed lines 7, 24, 40

Works fine now..
Topic archived. No new replies allowed.