Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Memory Leak using new statement

Status
Not open for further replies.

flavioe

Programmer
Sep 3, 2006
4
IL
I'm realy don't know what is happening with my code, I'm doing a legal memory allocation on one variable and the VC++ 6.0 simply create another variable on the same memory space.

The program is very large and I will not post the whole program, I will try to post the relevant part of the code:

I have 2 classes, lets talk about Class1 and Class2

on the main I have such a thing

int main()
{
...
Class1 *pClass1 = new Class1();
Class2 *pClass2 = new Class2();
Class2->Init();
...
}

class Class1
{
...
Class1()
{
Initialize();
}
void Initialize();
{
InitializeCriticalSection(&m_CriticalSection);
\\ Take a look on this variable
ThreadParam * param = new ThreadParam();
if(param != NULL)
{
param->Done = FALSE;
param->pCriticalSection = &m_CriticalSection;
// speciic on this;
param->List = m_List;
m_hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunction),(LPVOID)param,&m_ThreadID);
}
}
...
};

class Class2
{
public:
...
void Init()
{
...
myFlag = FALSE;
...
}
...
private:
...
BOOL myFlag;
};

When I run the program:
1) The memory allocated to Class2::myFlag is the same of param->List, i.e.:
printf("Class2::myFlag %p",&Class2::myFlag )
Output = 00374[/color red]
printf("param->List %p", &param->List )
Output = 00374[/color red]
The same output.

I dont delete none of the classes and I have no delete on param at all.

Changing from new to malloc does the same behavior.

Can anyone tell me what's wrong? Is there any knwon issue about this strange behavior?

Thanks in advance.
Flavio.
 
Is param->List pointing to an instance of Class2 and is myFlag the first member to be declared in Class2? If so, then you'd expect them to be the same.
 
Thanks for the fast answer!

param->List is a pointer to a list (the std template list) that is a private member of Class1, and myFlag is the last member of the Class2.

 
I think I see it...

Code:
ThreadParam * param = new ThreadParam();

That variable is automatically freed after the Initialize routine finishes, so the space is free for Class2 and its variables.

You need to make param a class variable or a static variable if you want it to stick around.

You also want to output
Code:
printf("param->List %p", param->List  ) ;
printf("m_List %p", m_List ) ;
not &param->List!

 
Miros,
The first thing I have learned on C++ is that when you use the new Statement the memory allocated by it will be NEVER freed until a delete statement.
Even if in place of
ThreadParam * param = new ThreadParam();
I will put a Class1 variable with the name m_ThreadParam like:
ThreadParam * m_ThreadParam;
And just initialize it on the Initialize routine doing
m_ThreadParam = new ThreadParam();

I got the same strange behavoir.

My whole problem is when I do (myFlag = FALSE) the pointer
param->List will point to NULL because the memory space of myFlag is the same as param->List.
 
so if i do
printf("param->List %p", param->List ) ;
printf("m_List %p", m_List ) ;

befor the execution of Class2->Init();
I will receive
Output: 0034a
Output: 0034a

After execution of Class2->Init();
I will receive
Output: 00000
Output: 0034a

 
1. Please use the [code][/code] tags when posting code.

2. It would be a lot easier if you could post an actual compilable example which we can compile and run without any guessing. Code snippets full of ..., and random one-line statements out of context do not help.

--
 
How about making your class variable not allocated?

Code:
ThreadParam m_ThreadParam;



 
I've been writing Java lately, and if the variable goes out of scope, it gets de-allocated.
 
... then maybe you could consider .NET, where the garbage collected heap works in much the same way. There's no shame in using good tools!
 
You don't appear to have enough numbers in your output.
printf %p should display 8 digits. 4 indicating memory segment and 4 for the offset.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top