Jump to content
  • Advertisement
Sign in to follow this  
korvax

C++ Pointer clean up question

This topic is 2116 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.

Question about pointer clean up.

void funcA(int id)
{
    Object* pkTemp = NULL;
   for (Object* pkObj :  forwardList)
   {
              if (pkObj ->id = id)
              {
                 pkTemp = pkObj;
              }

    }

 DoSomething(pkTemp);
 return;
}

So my question is, will it cause an memoryleak not deleting pkTemp? I see it is that pkTemp referees so pkObj so I cant delete it?

Or should pkTemp be a copy of pkObj so i can delete it?

 

Is there any way I can track memory leaks in visual studio?

Share this post


Link to post
Share on other sites
Advertisement
In general, you don't need to delete a pointer if it's just a copy of another pointer, only if it's pointing to an object that needs to be cleaned up. In this case presumably your forwardList still contains a copy of the pointer to an object that you want to stay valid. You might want to consider using smart pointers which will simplify your thinking about when you need to delete objects.

In any case, you can use crtdbg.h or Visual Leak Detector to track down leaks in your program with MSVC.

Share this post


Link to post
Share on other sites

Thx for you help.  A half related question for my case.

 

if you know, can you explain to me what this does?

Pointer** pointer = new Pointer*[100]; 

 

will automatically fill the array with Pointer objects and not just empty spaces?

Share this post


Link to post
Share on other sites
That will create an array of 100 uninitialized Pointer *s. It will not create any objects; you need to create those manually.

Share this post


Link to post
Share on other sites

Sorry this is a bit puzzling for me :)

 

Pointer* matched = new Pointer[100]; 

is it the same or will it (the array above) create 100 *pointer, and the other one above will create 100 **pointer?

 

all help much appreciated.

Share this post


Link to post
Share on other sites
Let's chose a type name that isn't so confusing to talk about. new Object[100] will create 100 consecutive Objects. new Object *[100] will give you 100 uninitialized Object pointers.

Share this post


Link to post
Share on other sites

...
if (pkObj ->id = id)
...

Did you perhaps mean to use == rather than =? (If this was just a quick demonstration example then nevermind ;))

 

Also, unless you know of a specific reason not to, you should look into using something like std::vector to create arrays in C++. It will help you against most (if not all) memory issues of this sort.

 

You might also want to consider using a std::vector of std::shared_ptr or std::unique_ptr if you don't want to worry about manual memory cleanup (almost critical if you use exceptions).

If you are new to smart pointers, start with std::shared_ptr so you dont need to worry about exclusive ownership and using std::move to push_back into a vector.

Edited by Karsten_

Share this post


Link to post
Share on other sites

You need to very clearly separate the idea of a pointer from the idea of an allocation. A pointer, no matter what you do to it, or with it, is variable that holds a memory address.

 

Allocations are created and need to be destroyed later to avoid leaks.

 

Their only relation is that the value in a pointer can point to an allocation.

 

It sounds simple, but getting really, really clear on that will solve all your pointer problems. Never let yourself think of a pointer as the thing it points to. Force yourself to think in terms of referencing and dereferencing. When that becomes second nature, you'll be able to intuit processes much more easily.

 

Also, in that example code, it's going to continue iterating the pointers in forwardlist, so DoSomething() will only happen to the last pointer found. You may want to throw a break in there after the assignment.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!