Jump to content
  • Advertisement

Archived

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

JohanK

Memoryleak

This topic is 5960 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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!