Hello, please can I have some clarification to check I am not doing something very stupid? It seems sensible with all that I have read, but I'm not confident. I suspect I have memory leaks and want to make sure some fundamentals are correct before I start looking deeper.

My code is very big so I can't reproduce it here. Instead, I have tried to illustrate with a simpler example.

In my code I have a class:

class MyClass {
...
      int Member;
    public:
      setMember(int value);
...
}

MyClass::setMember(int value)
{
   Member=value;
}

Originally, when I passed my instance of the class, I would pass it as a pointer:

char * myFunction(int testValue, MyClass *thisMyClass )
{
...
   
   thisMyClass->setMember(testValue);
...
}

void main(){
int aValue;
MyClass * thisMyClass;
aValue=20;
thisMyClass = new MyClass;
myFunction(aValue, thisMyClass);
delete thisMyClass;
}

However in this case, I believe that, thisMyClass was actually out of scope within myFunction() . I believe this because the memory occupied by thisMyClass kept getting overwritten.

Therefore, I started passing by reference:

char * myFunction(int testValue, MyClass &thisMyClass )
{
...
   
   thisMyClass.setMember(testValue);
...
}

void main(){
int aValue;
MyClass thisMyClass;
aValue=20;
myFunction(aValue, thisMyClass);
}

Questions:

  1. Was I right in believe that thisMyClass was out of scope in myFunction()?
  2. Am I right in passing by reference in this case where I am expecting thisMyClass to be changed by myFunction() ?
  3. Should I be using delete anywhere in the second example (I don't think I should as I have not instantiated with new )?

Recommended Answers

All 7 Replies

Hello,
firstly your function setMember has no returning type. Your example doesn't work either way? Is your compiler complaining about something?

Was I right in believe that thisMyClass was out of scope in myFunction()? NO, you sending a pointer to the function and then deleting it. so thr should notbe any problem or memory leak.
Am I right in passing by reference in this case where I am expecting thisMyClass to be changed by myFunction() ? it will not make a differnce here.
Should I be using delete anywhere in the second example (I don't think I should as I have not instantiated with new )? delete is only used when you are assiging memory from heap, i.e. dynamica memory allocation only.

Both example are similar, no you do not have a memory leak although there might be subtle problems with your first example. In general, use reference when possible instead of pointers. I'm sure mike below me will give you a more elaborate answer soon.

Thank you for swift responses.

I should have written the myFunction() definition in this example as

void myFunction(...

Both work and my compiler, Visual Studio 2008, does not complain. However, with the first example, parts of thisMyClass were being overwritten by variables being created in myFunction(). Its when I using classes from a DevKit from commercial software that the problems occur so I don't know what precisely is happening.

So I found that there was a difference between passing by pointer and by reference with by reference being safer. I'd like to understand why so that I can learn what is best to use when.

I think I am starting to understand heap and stack. I can't think of an occasion when dynamically assigning memory (i.e. to heap) occurs when I don't use new (or malloc() ).

Sorry to go over the obvious but just re-reading articles does not give me confidence that I have understood!

>>So I found that there was a difference between passing by pointer and by reference with by reference being safer. I'd like to understand why so that I can learn what is best to use when

Yes reference can be safer and most likely will be, simply because reference needs to be pointing to a valid object, where a pointer can point to an invalid object( i.e null object type )

Was I right in believe that thisMyClass was out of scope in myFunction()?

No, the object was created before the function call and gets deleted after the function returns. So, unless your myFunction() does something unholy, the passed object is in scope and the pointer is valid. Of course, if the myFunction() ends up saving that pointer somewhere else (like a global variable), then you might be in trouble and should avoid doing that.

Am I right in passing by reference in this case where I am expecting thisMyClass to be changed by myFunction() ?

In general yes, prefer passing by reference when possible (which is the vast majority of the time). References are safer because they cannot be reseated, meaning that you cannot accidentally invalidate the pointer by altering its address, or easily copy the reference to a persistent (global) variable and thus keeping a reference to the object beyond the scope of myFunction(). They are also preferred because they act as aliases for a variable and thus, the syntax is cleaner and more readable when using references, which is also less error-prone.

@firstPerson:

because reference needs to be pointing to a valid object
That is not strictly true. References can point to invalid objects (or deleted objects). But it is true that it is much harder to make that bug happen with references, while it is a lot easier to make that error with a pointer.

Should I be using delete anywhere in the second example (I don't think I should as I have not instantiated with new )?

No. Never use delete unless you are sure that the pointed-to memory was allocated using new. delete[] matches new some_type[N];, delete matches new some_type();, and free() matches malloc.

If you are really worried about memory leaks, try making use of reference counted smart-pointers like boost::shared_ptr (or std::tr1::shared_ptr).

>>@firstPerson: >> because reference needs to be pointing to a valid object
That is not strictly true. References can point to invalid objects (or deleted objects). But it is true that it is much harder to make that bug happen with references, while it is a lot easier to make that error with a pointer.

Yea yea yea, I glanced over that topic in my head but didn't write it down because I didn't think it was noteworthy for this thread, but thanks for mentioning it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.