Archived

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

Memoryleak

This topic is 5744 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

Hi, Lowlevel c++ question... Consider the following piece of code: struct a { int somevar; }; a* SomeFunction() { a* objA; objA = new objA(); return objA; } MainFunction() { a* pA; pA = SomeFunction(); } Does the memory stay allocated for pA forever so you have to clean it up yourself?

Share this post


Link to post
Share on other sites
Not "forever", but for the duration of your program''s execution. In fact, once you leave MainFunction, the pointer pA no longer exists and you lose the pointer to that memory, with no hopes of deleting it.

-------------------
"Pointer?????"
-Anonymous

-=Xelius=-

Share this post


Link to post
Share on other sites
Does this clear up something for you?

  
#include <iostream.h>

struct a { int var; };

a * function()
{
a * ret;
ret = new a;
ret->var = 10;
cout << "ret->var = " << ret->var << "\n";
cout << "&ret = " << &ret << "\n";
return ret;
}

int main()
{
a * myA;

cout << "&myA = " << &myA << "\n";

myA = function();

cout << "&myA = " << &myA << "\n";

cout << "myA->var = " << myA->var << endl;

delete myA;

return 0;
}


/G

Share this post


Link to post
Share on other sites
Hi,

It''s clear what it does, i was just wondering over the allocation.

The reason why I was wondering is when you use the D3DXxxx functions you often get pointers to structures in return. What I was wondering over is if I have to delete them myself... (And I guess I have to then).

Thanks,
/Johan

Share this post


Link to post
Share on other sites
If you get a pointer from a DirectX, you''re supposed to call the release method. So, if lpD3D is your Direct3D object, when you''re done with it, you call

lpD3D->Release();

This is the proper way to free the memory you''re using with the object.


-------------------
"Pointer?????"
-Anonymous

-=Xelius=-

Share this post


Link to post
Share on other sites
But I don't want it to consume all the memory while running.
I ran a little test, letting my app run for about an hour. When closing it, it takes around 20 seconds to return to windows.

Could this be due to a memory leak?
When running for short periods of time the return to windows is instant...

[edited by - JohanK on March 24, 2002 12:26:02 PM]

Share this post


Link to post
Share on other sites
It sounds like that could be the case if you're using MFC. This is only true if you are running in Debug mode because it keeps track of and reports all the memory you do not free in the debug output window. I have a feeling that this may be what's slowing you down especially if you have lots of memory leaks.
I tried a test program where I allocated 30 megs of memory in 30 chunks and then exited my program and there was no delay.
However, like IDispath said, if you new it you have to delete it. If the memory is allocated for you, there's a way for you to de-allocate it, whether it be by calling a function or by passing it to some other method or something.

What does: "But I don't want it to consume all the memory while running." mean?


-------------------
"Pointer?????"
-Anonymous

-=Xelius=-

[edited by - xelius on March 24, 2002 12:42:48 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Xelius

What does: "But I don''t want it to consume all the memory while running." mean?




Allocating to much memory, eating it up, therefore "consume"... (Should have said allocating directly but my english is not that good, sorry)

Its not a problem right now, but if you add 200 models, each eating away a bit for every matrixtransformation, it''s gonna be alot of memory wasted...

Thanks everyone for you time by the way!

Share this post


Link to post
Share on other sites
It''s not wasted...if you need it, you need it. You just need to be smart about how you use it. There are certain things you can''t avoid allocating memory for...you know? :-)

-------------------
"Pointer?????"
-Anonymous

-=Xelius=-

Share this post


Link to post
Share on other sites
I know I need to allocate memory, the question is wheter (or however you spell that) or not it was deallocated by some magic ferry.

It would suck if my app allocated a MB each minute and not deallocating it. (that could easily be the case)

But as I learned here, when you new it you have to delete it. Thats fine as long as I know it...

Share this post


Link to post
Share on other sites
Gee, about that example you gave, I think it would have been clearer if you did:

(Main)
cout << "myA = " << myA << "\n";
(Function)
cout << "ret = " << ret << "\n";
(Main)
cout << "myA = " << myA << "\n";

This would demonstrate that Function() returns a pointer
to the newly allocated memory. The addresses of the pointers
will never change, but what they point to will. Was this
your intention?

I also had a question to just clear things up in my mind. If memory is allocated (via New) in a function (like Function()), the pointer to the new memory can be returned. However, when is that memory gauranteed to be safe and secure? I am trying to remember the cases when the memory myA newly points to might get overridden, and when it will not. Does this question make sense?

Share this post


Link to post
Share on other sites
Yeah, I think I understand what you mean. Basically, any memory you allocate within your program via new is (in most cases) safe to pass around within your program. However, if you have code like this:


GameObject *DoStuff(ControlStruct *s)
{
GameObject g;

// .... perform actions on g

return &g;
}


This is not valid because when the function exits, any local variable is considered non-existant. And it may be possible that you can access the contents of the returned address properly for a little while because it''s location in memory has not been over-written. However, this behaviour is NOT guaranteed and therefore shouldn''t be used! :-)

Does this clear things up? Or would you like some further explaination?

Share this post


Link to post
Share on other sites