Concerning Garbage Collection in Managed C++

  • Thread starter Thread starter Tim Murray
  • Start date Start date
T

Tim Murray

Hello,
I am developing a console application in Managed C++
with Visual Studio .Net. I am trying to learn how to
correctly destroy variables. When I run my program, with
every added action, the memory usage increases but the
only memory being allocated in each function is for
temporary local variables, and should not persist.
Calling the garbage collector does not seem to do anything
to clean up the memory usage. I am currently setting each
variable back to NULL upon leaving the functions, but it
does not seem to be working. Are variables in Managed C++
not cleaned up when they go out of scope? Am I supposed
to take extra precautions to get them to be released? Is
there any way for me to find out what is leaking?

Thank you for your help,
Tim
 
Hi again,
I got some help tracking the problem down and now know
that the variable that is leaking memory is an
OleDBDataReader - I close it, but all of the memory that
was allocated for it remains. Any ideas on how to clear
this up? I need to keep the database connection open.

Thanks,
Tim
 
Hi again,
I got some help tracking the problem down and now know
that the variable that is leaking memory is an
OleDBDataReader - I close it, but all of the memory that
was allocated for it remains. Any ideas on how to clear
this up? I need to keep the database connection open.

Are you referring to .Close()ing it or to setting the variable to null and
it being garbage collected?

If you mean .Close(), it is possible that the memory used is held onto in
case of another .Open(). If you mean you're setting the variable to null
(or it goes out of scope, or you .Dispose() it), the garbage collector runs
in a non-deterministic manner, meaning it will deallocate the memory
whenever it feels like it and you needn't be concerned.
 
Back
Top