Memory Leak Help
-
I'm not a C++ programmer, but I have some work to do with fixing a Memory Leak. I created a reference to an object called myObject: myObject = CreateObject(); right? So if I recreate that myObject by using: myObject = CreateObject(); Again and it creates a new object will it overwrite that memory space and destroy the old object or do I have to delete myObject first and then write to that reference again? Thanks for your help, I've been looking everywhere for this.
-
I'm not a C++ programmer, but I have some work to do with fixing a Memory Leak. I created a reference to an object called myObject: myObject = CreateObject(); right? So if I recreate that myObject by using: myObject = CreateObject(); Again and it creates a new object will it overwrite that memory space and destroy the old object or do I have to delete myObject first and then write to that reference again? Thanks for your help, I've been looking everywhere for this.
You have to delete the previously created object before creating a new object. If you have used new or new[] inside CreateObject you have to call delete or delete[] respectively. If malloc is used, you have to call free. If it is a COM object, you have to call its Release method.
«_Superman_» I love work. It gives me something to do between weekends.
-
I'm not a C++ programmer, but I have some work to do with fixing a Memory Leak. I created a reference to an object called myObject: myObject = CreateObject(); right? So if I recreate that myObject by using: myObject = CreateObject(); Again and it creates a new object will it overwrite that memory space and destroy the old object or do I have to delete myObject first and then write to that reference again? Thanks for your help, I've been looking everywhere for this.
It all depends on what CreateObject actually does... If it does something like this:
struct Object
{
whatever
};Object CreateObject() { return Object(); }
then you don't need to do anything to myObject before reassigning to it. If it uses a resource allocating function (e.g. new and malloc allocate memory, CreateFile, CreateEvent and CreateProcess allocate kernel handles, there are many more different examples of resource allocators) and passes control of that resource back to you, then you are responsible for deallocating that resource. To show a simple example:
Object* CreateObject() { return new Object(); }
To reassign to myObject, you'd need to deallocate the thing referenced by myObject using
delete myObject
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
I'm not a C++ programmer, but I have some work to do with fixing a Memory Leak. I created a reference to an object called myObject: myObject = CreateObject(); right? So if I recreate that myObject by using: myObject = CreateObject(); Again and it creates a new object will it overwrite that memory space and destroy the old object or do I have to delete myObject first and then write to that reference again? Thanks for your help, I've been looking everywhere for this.
just to clarify : does
CreateObject()
return a pointer to an object or a reference ?This signature was proudly tested on animals.
-
just to clarify : does
CreateObject()
return a pointer to an object or a reference ?This signature was proudly tested on animals.
-
In which case a
delete myObject;
is required to deallocate the object before assigning a different value to myObject - so long as the reference held by myObject hasn't been copied to some other variable.Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
I'm not a C++ programmer, but I have some work to do with fixing a Memory Leak. I created a reference to an object called myObject: myObject = CreateObject(); right? So if I recreate that myObject by using: myObject = CreateObject(); Again and it creates a new object will it overwrite that memory space and destroy the old object or do I have to delete myObject first and then write to that reference again? Thanks for your help, I've been looking everywhere for this.
No ,this will not overwrite the Memory Space. if you have created object and collected the Reference it's just a reference (i.e. constant pointer).If you will assign it a new address it 'll not delete old object.(As it should not, Bcoz that object may be used ant where else).So if you are sure it is not usable now.Then call delete on it first .Then create new and assign value to it. But if you are not assigning new value , set it to null ('ll save from Dangling pointer). So you can say if( myObject != NULL) delete myObject; myObject = CreateObject(); //or myObject = NULL;
It's not enough to be the best, when you have capability to be great....