[Solved] Delete object and reset pointer

Get answers to all your basic programming questions. No Ogre questions, please!
User avatar
affentanz
Kobold
Posts: 30
Joined: Tue Sep 03, 2013 10:34 pm
x 2

[Solved] Delete object and reset pointer

Post by affentanz »

I am still a beginner in C++ (normally, I code Java or C#), so I have some trouble with pointers and references. I'm trying to set a pointer to a new target object. So, I think I have to delete the old object to prevent a memory leak. Here's my code:

Code: Select all

#define SAFE_DELETE(X) {if (X != NULL) {delete (X); X=NULL;}}

AnObject* currentPointer = new AnObject();
SAFE_DELETE(currentPointer);
currentPointer = new AnObject();
This doesn't work. The app just crashes without an error. What's wrong?
If I do it without the SAFE_DELETE, it works fine.

Code: Select all

AnObject* currentPointer = new AnObject();
currentPointer = new AnObject();
But I think this will produce a memory leak. Right?
User avatar
spacegaier
OGRE Team Member
OGRE Team Member
Posts: 4304
Joined: Mon Feb 04, 2008 2:02 pm
Location: Germany
x 136

Re: Delete object and reset pointer

Post by spacegaier »

Yes, the second will provide a memory leak since you allocated memory in the first line, that never got released.

I assume that your NULL is defined as '0', right?

The only thing that looks a bit weird to me are the parentheses here:

Code: Select all

delete (X)
What happens if you remove those?
Ogre Admin [Admin, Dev, PR, Finance, Wiki, etc.] | BasicOgreFramework | AdvancedOgreFramework
Don't know what to do in your spare time? Help the Ogre wiki grow! Or squash a bug...
User avatar
affentanz
Kobold
Posts: 30
Joined: Tue Sep 03, 2013 10:34 pm
x 2

Re: Delete object and reset pointer

Post by affentanz »

I've got the SAFE_DELETE macro from the (german) book "C++ für Spieleprogrammierer". It's the same to write:

Code: Select all

AnObject* currentPointer = new AnObject();
if (currentPointer != NULL)
{
    delete (currentPointer);
    currentPointer=NULL;
}
currentPointer = new AnObject();
User avatar
spacegaier
OGRE Team Member
OGRE Team Member
Posts: 4304
Joined: Mon Feb 04, 2008 2:02 pm
Location: Germany
x 136

Re: Delete object and reset pointer

Post by spacegaier »

Yes, I understand what the macro is supposed to do, I just never saw the syntax "delete (x)" with the parentheses.

Where does it crash? What does the debugger say?
Ogre Admin [Admin, Dev, PR, Finance, Wiki, etc.] | BasicOgreFramework | AdvancedOgreFramework
Don't know what to do in your spare time? Help the Ogre wiki grow! Or squash a bug...
User avatar
affentanz
Kobold
Posts: 30
Joined: Tue Sep 03, 2013 10:34 pm
x 2

Re: Delete object and reset pointer

Post by affentanz »

Ok, I understand your point. I think it's the same like "delete X".

I don't know where exactly it crashes, because the app just shuts down, when I click a button that leads to that piece of code. There's no error in the console or logfile. And if I start the app in debug mode the whole system freezes and I have to restart my computer.

I just suggest that it's somewhere around this delete, because it works fine when I write it without the delete.

So, generally this code is right?
User avatar
spacegaier
OGRE Team Member
OGRE Team Member
Posts: 4304
Joined: Mon Feb 04, 2008 2:02 pm
Location: Germany
x 136

Re: Delete object and reset pointer

Post by spacegaier »

There must be something seriously wrong, if you cannot start your app in debug mode! Did you try to set a breakpoint at the earliest possible code line to try to see if it starts at all and then make your way to be cause. If that is not possible at all, you could write a log message directly before the delete and directly afterwards to check whether this is actually the problematic code line.

Yes, the code looks okay to me, but the debugger will tell you more.

PS: System freezes might indicate a endless loop in case you only have a CPU core that gets completely taken up by your application. Otherwise, the other cores should still allow you to call the TaskManager and kill the application process.
Ogre Admin [Admin, Dev, PR, Finance, Wiki, etc.] | BasicOgreFramework | AdvancedOgreFramework
Don't know what to do in your spare time? Help the Ogre wiki grow! Or squash a bug...
User avatar
affentanz
Kobold
Posts: 30
Joined: Tue Sep 03, 2013 10:34 pm
x 2

Re: Delete object and reset pointer

Post by affentanz »

Ok, I should explain that. The debug thing is a different problem and I think it's because of my dev environment: Eclipse-CDT/gcc on Ubuntu 12.04 64 Bit. I can start the app in debug mode, but in the moment when there's a bug and the focus is going from the app to the ide - I can't control mouse and keyboard anymore. All open programs are working fine (for example the chat message popups in the right corner are still working), but I can't do anything. Alt-Tab, Alt+F4, Str+Alt+Del. Nothing works. So I have to reboot.
But I think this is a different problem, not related to the problem of this thread.

The only thing I can see in the debug window after the "crash" is, that there's an error in OgreDistanceLodStrategy.cpp Line 61

Also, I tried some log messages before and after the codeblock I posted. And all of them were printed! So it seems that the delete is not the problematic code line. But if I do it without the delete, it works. That's crazy....

Maybe another hint: When the app crashes, these lines are missing in the ogre log:

Code: Select all

DefaultWorkQueue('Root') shutting down on thread 0x1e91b50.
DefaultWorkQueue('Root')::WorkerFunc - thread 0x21c07b0 stopped.
DefaultWorkQueue('Root')::WorkerFunc - thread 0x21c6450 stopped.
DefaultWorkQueue('Root')::WorkerFunc - thread 0x1f0ac50 stopped.
DefaultWorkQueue('Root')::WorkerFunc - thread 0x21c79b0 stopped.
*-*-* OGRE Shutdown
Unregistering ResourceManager for type Compositor
Unregistering ResourceManager for type Font
Unregistering ResourceManager for type Skeleton
Unregistering ResourceManager for type Mesh
Unregistering ResourceManager for type HighLevelGpuProgram
Uninstalling plugin: Octree Scene Manager
Plugin successfully uninstalled
Unloading library /usr/local/lib/OGRE/Plugin_OctreeSceneManager
Uninstalling plugin: Cg Program Manager
Plugin successfully uninstalled
Unloading library /usr/local/lib/OGRE/Plugin_CgProgramManager
Uninstalling plugin: GL RenderSystem
Unregistering ResourceManager for type GpuProgram
******************************
*** Stopping GLX Subsystem ***
******************************
Unregistering ResourceManager for type Texture
Plugin successfully uninstalled
Unloading library /usr/local/lib/OGRE/RenderSystem_GL
Unregistering ResourceManager for type Material
Btw: I user the NULL definition from stddef.h
User avatar
spacegaier
OGRE Team Member
OGRE Team Member
Posts: 4304
Joined: Mon Feb 04, 2008 2:02 pm
Location: Germany
x 136

Re: Delete object and reset pointer

Post by spacegaier »

#1 What are those pointers actually (the generic AnObject from your example)? Ogre objects?

#2 Can you show the complete log / console output, regrading the OgreDistanceLodStrategy.cpp thing you mentioned?

#3 Regarding the mssing log lines: If the application crashes, it of course cannot properly shut down Ogre, hence those lines won't appear in the Ogre.log.
Ogre Admin [Admin, Dev, PR, Finance, Wiki, etc.] | BasicOgreFramework | AdvancedOgreFramework
Don't know what to do in your spare time? Help the Ogre wiki grow! Or squash a bug...
User avatar
affentanz
Kobold
Posts: 30
Joined: Tue Sep 03, 2013 10:34 pm
x 2

Re: Delete object and reset pointer

Post by affentanz »

"AnObject" is a class I implemented myself. Actually, it's an abstract class and I have two child classes. I wanted to keep things simple with my sample code, but it should be something like this:

Code: Select all

AnObject* currentPointer = new AnChildObject();
SAFE_DELETE(currentPointer);
currentPointer = new AnotherChildObject();
And now I get the solution myself! I had some weird code in the destructor of the abstract class and I didn't think of that. I just looked at the child classes all the time.

Ok, newbie mistake. Thank you for your help!