Very goofy pointer issue with cameras

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
DrAnonymous
Posts: 34
Joined: Thu Aug 04, 2005 9:37 pm

Very goofy pointer issue with cameras

Post by DrAnonymous »

I setup a development environment so that I could use irrLicht without changing any of its source. I want to be able to drop in any new updates by just overwriting the old source.

I wrote a new camera class, which supports zooming with the scroll wheel.

Since I didn't want to touch the original irrlicht source, I just manually did the same call the scenemanager.addCameraSceneNodeMaya(...)

The scenemanager code is -

Code: Select all


if(!parent)
   parent = this;

ICameraSceneNode* node = new CCameraMayaSceneNode(parent, this, id, rotate...);
node->drop();
setActiveCamera(node);
return node;
In my main.cpp I did the following -

Code: Select all

smgr = Device->getSceneManager();
ICameraSceneNode* node = new CCameraSimpleNode((ISceneNode*)smgr, smgr, id);
node->drop();
setActiveCamera(node);
I discovered 2 problems, if I do the drop I get a segmentation fault. Why?

The other one is if I print the parent pointer of the MayaCamera, it doesn't match the pointer to the scene manager. If you put a printf in the CSceneManager class and print the parent, its not NULL and its not the this pointer either. Since the method addCameraSceneNodeMaya defaults the parent pointer to 0, where is this other value coming from?

The only way I can get my camera to work is do this -

Code: Select all

ISceneNode* node = smgr->addCameraSceneNodeMaya();
node = new CCameraSimpleNode(node->getParent(), smgr, id);
node->drop();
setActiveCamera(node);
Can someone please explain whats going on?

Cheers,
Dr. A>
hearsedriver
Posts: 81
Joined: Fri Aug 22, 2003 12:06 pm
Location: Germany
Contact:

Re: Very goofy pointer issue with cameras

Post by hearsedriver »

Hi DrA,

there are some cases of multiple inheritance, where static C-casts can cause problems, and maybe you just discovered one. Try new CCameraSimpleNode(static_cast<ISceneNode*>(smgr), smgr, id) instead, and it should work.

Cheers
hearsedriver
matthias gall, lead programmer at sechsta sinn - email matthias@sechsta-sinn.de
MindGames
Posts: 32
Joined: Sat Jul 16, 2005 2:02 am

Post by MindGames »

I could be wrong here but I think your segmentation issue is actually due to the way the memory manager works. I have had this issue before with dlls where the memory is allocated either within the dll or within your application's memory space and then deleted from the other. Try overloading the drop function in your class and handle the deletion yourself by just copying Niko's method. If this causes the problem to go away then I am most likely correct and you will have to mess around with overloading delete operators etc.
DrAnonymous
Posts: 34
Joined: Thu Aug 04, 2005 9:37 pm

Post by DrAnonymous »

Uggh no go. :(

I can't compile using static_cast. The compiler says invalid cast type of ISceneManger to ISceneNode. Ugggh. Why is there an interface, aren't you only going have 1 scenemanger ever?

I'm compiling on linux, so I'm not sure the dll would apply. My executable is staticly linked, so I think my memory allocs and free should all be in the same memory space.

Any other ideas?

Regards,
Dr. A>
MindGames
Posts: 32
Joined: Sat Jul 16, 2005 2:02 am

Post by MindGames »

Yeah you are right, the problem I mentioned was windows only with a dll.

Anyway, why are you calling drop after calling new. This will cause it to be deleted. Maybe I am misunderstanding this somehow, but AFAIK you only call drop when you are using something created with a createXXX function and only after you are finished with it. If you delete the object and then try to use it you are going to get problems.
DrAnonymous
Posts: 34
Joined: Thu Aug 04, 2005 9:37 pm

Post by DrAnonymous »

True, about dropping it. I'm still not keen on the whole drop/grab thing. I understand the intent of bookkeeping, but it doesn't seem intuitive enough.

Any way, the drop issue aside, the parent pointer is still not right. Basically if you copy the exact code out of the create method and put it into your main.cpp, it won't work. The cast of the scenemanager isn't right. The only work around I found was to use the getParent() method of another camera and pass that pointer to the constructor of my custom camera for a parent ie -

new CustomCamera(firstCamera->getParent(), sceneManager, 100)

Any other ideas?

Cheers,
Dr. A>
mohaps
Posts: 248
Joined: Tue Jun 08, 2004 1:54 pm
Location: Shrewsbury MA
Contact:

Post by mohaps »

wildgues...

your app is single-threaded and your irrlicht dll is multithreaded...

is that the case...

look in project-->Settings-->C/C++---> Code Generation...
---
Saurav Mohapatra
author, artist and bona fide geek

web: http://www.mohaps.com
email: mohaps AT gmail DOT com
MindGames
Posts: 32
Joined: Sat Jul 16, 2005 2:02 am

Post by MindGames »

I don't think it is a threading issue, he said he was statically linking it.

Right, here come some new theories. This time I have had a look at the Irrlicht code (as I am not at work).

smgr = Device->getSceneManager();
ICameraSceneNode* node = new CCameraSimpleNode((ISceneNode*)smgr, smgr, id);

This, as far as I can see is invalid. You cannot cast a ISceneManager into an ISceneNode, they are unrelated. As you are just using a c style cast you are not getting to see this as an error. Try doing a static_cast<ISceneNode>(smgr) or if you prefer a dynamic_cast<ISceneNode>(smgr). Either one of these would have thrown up a compiler error. When the compiler told you it was an invalid cast earlier, it was right. I imagine that this is the source of your problems so I have not looked further.
mohaps
Posts: 248
Joined: Tue Jun 08, 2004 1:54 pm
Location: Shrewsbury MA
Contact:

Post by mohaps »

I think it is a multithreaded dll / singlethreaded app issue... cause i have faced such symptoms before...
---
Saurav Mohapatra
author, artist and bona fide geek

web: http://www.mohaps.com
email: mohaps AT gmail DOT com
MindGames
Posts: 32
Joined: Sat Jul 16, 2005 2:02 am

Post by MindGames »

When you statically link you do not use a dll. Therefor there can only be one threading library in use. If it is a case of requiring specific threading related functions from the threading library then I would expect the compiler to complain, though I have not used GCC (I presume that is what he is using).

Of course, I could be misunderstanding what he meant exactly by statically linking.
mohaps
Posts: 248
Joined: Tue Jun 08, 2004 1:54 pm
Location: Shrewsbury MA
Contact:

Post by mohaps »

i think he does static linking on linux and links to the dll on win32..... is the problem occuring on both platforms...same symptom?? or just on windows??
---
Saurav Mohapatra
author, artist and bona fide geek

web: http://www.mohaps.com
email: mohaps AT gmail DOT com
MindGames
Posts: 32
Joined: Sat Jul 16, 2005 2:02 am

Post by MindGames »

Well I questioned the windows dll issue earlier and he only mentioned the linux statically linked system. I too have had the issues you mentioned though only when linking to a dll.

However, unless I am very much mistaken, he is attempting an invalid cast and, due to the fact that he uses c style casts, he is unaware of this (a very good arguement for never using them BTW). This error is almost certain to cause issues so should be resolved before worrying about more complex issues.

Unfortunately I have not delved very much into this area so am not really qualified to tell him what exactly he is meant to be doing to correct the issue.

I suspect however, that what he was meant to have written was something like;

Code: Select all


smgr = Device->getSceneManager();
ICameraSceneNode* node = new CCameraSimpleNode(smgr->getRootSceneNode(), smgr, id); 
setActiveCamera(node); 

But as I say, until I have a proper look at this area I will not really be certain.
Guest

Post by Guest »

Mindgames is right.

The "parent" of a scenenode is always supposed to be another scenenode, or none (0).

The scenemanager is set separately. There is absolutely no reason to pass it twice.

Setting the parent probably also modifies a list in the parent scenenode itself.
Since the scene manager is not a scenenode, that likely overwrites something that should not be overwritten.

If you don't want to set a parent for your new camera scenenode, just set it to 0.
Post Reply