Archived

This topic is now archived and is closed to further replies.

memory leaks .. ?

This topic is 5672 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey there, i originally programmed in VB, and have no idea how to _un-initialize_ values in C++. I heard that when i don''t do this, for many times, that my memory gets "eaten" which sounds logical to me... Any tips/urls/howto''s??

Share this post


Link to post
Share on other sites
The main point to remember when using C++ is that if you allocate something with malloc(), you must free it with free(), and if you allocate something with new, you must free it with delete.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Err... I don''t think that syntax will work exactly, it will create an array of pointers, not a string which is 255 characters long.

I don''t know if you understand it already, but a variable defined with a "*", ie. "char *item;", is not actually a variable, but a pointer to a variable. Whereas "char item" is actually a variable. Go buy a book, that''s the best way to learn.

The proper way to do things is as such-


char *var;
var = new char[255];

/* do stuff with variable here */

delete var;
var = new char[8192];

/* do more stuff, with newly allocated space */

delete var;


Or, you can do it more traditionally, not using pointers-


char var[255];

/* do stuff with variable */


And the above code will automatically clear your allocated space for you. It''s only that pointers are more dynamic (ie, you can create and delete them at will) and a lot faster that people use them. Also, the pointers themselves can be "copied"... ie, you can simply say;


char *var1;
char *var2;

var1 = new char[255];

/* set variable */

var2 = var1;

/* var1 and var2 now point to the same space, you can muck around with them as if they were essentially the same */

delete var2;


Hope that helps.

Share this post


Link to post
Share on other sites
Just a small remark to what AP said.
When you would like to delete an array that was dynamically allocated with ''new'', you should use delete[].
So th code could be:

  
char *var;
var = new char[255];
/* do something with var */
delete[] var;

But just as he said: it''s best to buy a good book and read carefully through it.
Success!

===========================
UNKNOWN caused an invalid page fault in module unknown at 0000:bff80eb6
: win98 with multiple sclerose

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
When you delete a pointer, it''s usually smart to set the
value to NULL, so you know it''s invalid.

Assuming you do that, note that in the previous example
where the pointer is copied, the memory pointed to is not!

Since both pointers point to the same block, doing
delete on one of them will also invalidate the other

/Kitt3n

Share this post


Link to post
Share on other sites
char *p_char

p_char = new char(''a'');

don''t deinitialize with delete []

but with delete p_char;

char *p_char_array

p_char_array = new char[50];

delete [] p_char_array

hope this is oke

Share this post


Link to post
Share on other sites
quote:
Original post by smarechal
Speaking of memory leaks here.... What would me a good way of detecting them? Is there a handy little func in VC++?


Sure there is. Put this before your code:

  
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>

And turn it on:

_CrtSetDbgFlag(_CrtSetDbgFlag(0) | _CRTDBG_LEAK_CHECK_DF);

If you have the leaks, you''ll see them in the debug output.
quote:

I hope it''s not in the debugger.


It''s not exactly in the debugger, but it writes to the debug output. It''s a debugging feature, after all.
quote:

For some reason the debugger and my program don''t work together.


You better make them work together, debugging with a debugger is much more efficient than doing so with printf statements (or log files, etc. etc.)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You might be getting errors while debugging, but not when actually running, because the errors are simply not appearing when you''re running the program normally - that is, the errors are occuring, but the program is still running anyway.

By the way - using a debugger to detect memory leaks is called "extremely slack coding". If you don''t want to learn it, then you''re better off staying away from pointers.

-- The First AP Guy To Reply

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
As an addition-

If you''re running in "Debug Mode", VC++ might be fixing up your code errors, on-the-fly. Try compiling it non-debug, and see if it crashes or encounters errors.

Share this post


Link to post
Share on other sites
Well, I do want to learn. I first tried to use the debugger when my texture manager screwed up big time and I couln''t find the error. I simply wrote a log function to do it manually. The engine got a little too big to find out why the debugger couln''t handle it.

I''ll try compiling it as a final. See what it does. And after that, I think i''ll throw everything out the window

I learned a lot since I started the engine and I''m going to do a total makeover. I just finished the basecode. Let see if I can make the booger debug-friendly

Sander Maréchal
[Lone Wolves Production][Articles][E-mail]

Share this post


Link to post
Share on other sites
quote:
Original post by Xces
To be honest that was the reason why i started this thread. I am kind new to C++, in VB all is done for you...


To an extent, VB does it for you. But you will find that there are cases where VB does have memory leaks, even with perfect code. Other times, Terminators never fire when you would assume they do.

I would suggest getting into the habit of explicitly setting all references to Nothing in VB. You can''t explicitly delete an object, but setting references to nothing will help ensure that VB doesn''t "forget" to deallocate memory for you.

And since VB uses reference counting for automatically deleting variables, it is easy to write objects that VB never cleans up for you. Take circular references, for example. If A holds a reference to B, and B holds a reference to A, neither will be freed.

Automatic deleting of objects is nice, but it helps to understand what is really going on.

--TheMuuj

Share this post


Link to post
Share on other sites