User avatar
Erio
Topic Author
Posts: 81
Joined: 06 Feb 2013, 17:30
Location: Orléans/France

Ptr<> System question

14 Feb 2013, 23:58

Hi there. As there's not a lot of activity there, and i got a question (yes.. Another one :p), i hope that it's ok if i ask it there.

As i'm actually trying to use NoesisGUI and make it to work with Ogre and Qt (and i'm almost done with it, it can compile, i juste did a wrong initialization of Noesis), i get a bit lost in the Ptr<> System with all these dynamic cast.

I was wondering why did you choose (or maybe and more probably had to) use this system of Ptr which gets a bit confusing to me (and even more to QtCreator ^^)?

If you don't mind to explain it to me, i would be pleased to hear more about it and get the real meaning of this system, why and why not using "standard" pointers as
UIElement *element = new UIElement(); 
PS: I saw a few mistakes in the documentation during my search phasis through the tutorials :)
On the "building an application" one, you open the windows tag but at the end close with a </Grid> which i guess is a mistake :) (I might be wrong as well, but i tried it either, and it didn't work ;))
 
User avatar
jsantos
Site Admin
Posts: 2922
Joined: 20 Jan 2012, 17:18
Contact:

Re: Ptr<> System question

18 Feb 2013, 13:57

Hi,

Returning a Ptr<> instead of a raw pointer allow us to do automatic garbage collection for the objects that are returned. This is used everywhere in Noesis. What problems are you having? Ptr<UIElement> behaves like a UIElement*. The main advantage is that when all the references to that element disappear the instance is freed from the heap.

Thanks for reporting us the documentation issues. Will be fixed in the next release.
 
User avatar
sfernandez
Site Admin
Posts: 1935
Joined: 22 Dec 2011, 19:20

Re: Ptr<> System question

18 Feb 2013, 14:26

To clarify a bit more the use of Ptr and castings follow this example.

We will define 2 interfaces (virtual pure classes without data and deriving from Interface):
//////////////////////////////////////////////////////////////////////////
NS_INTERFACE IVisible: public Interface
{    
    virtual void Hide() = 0;
    virtual void Show() = 0;

    NS_IMPLEMENT_INLINE_REFLECTION_(IVisible, Interface)
};
//////////////////////////////////////////////////////////////////////////
NS_INTERFACE IFloat: public Interface
{
    virtual void SetFloat(NsFloat32 value) = 0;
    virtual NsFloat32 GetFloat() = 0;

    NS_IMPLEMENT_INLINE_REFLECTION_(IFloat, Interface)
};
And a component that implements those interfaces:
//////////////////////////////////////////////////////////////////////////
class Sphere: public BaseComponent, public IVisible, public IFloat
{
public:
    Sphere() { numSpheres++; }
   ~Sphere() { numSpheres--; }
        
    /// From IVisible
    //@{
    void Hide() {}
    void Show() {}
    //@}
        
    /// From IFloat
    //@{
    void SetFloat(NsFloat32 value) { mFloat = value; }
    NsFloat32 GetFloat() { return mFloat; }
    //@}
    
private:
    NsFloat32 mFloat;
        
    NS_IMPLEMENT_INLINE_REFLECTION(Sphere, BaseComponent)
    {
        NsImpl<IVisible>();
        NsImpl<IFloat>();
    }
};
And another component not related with the previous classes:
//////////////////////////////////////////////////////////////////////////
class Box: public BaseComponent
{
public:
    Box() {}
    ~Box() {}
    
    NS_IMPLEMENT_INLINE_REFLECTION_(Box, BaseComponent)
};
Now, let's create a sphere component. Component can be created like normal classes with the operator new.
// Create the component. Reference count is equal to 1
Ptr<Sphere> sphere = *new Sphere;

// Cast to the IVisible interface (reference count incremented to 2)
Ptr<IVisible> visible = NsStaticCast<Ptr<IVisible> >(sphere);
visible->Hide();
visible->Show();
    
// Cast to the IFloat interface (reference count is incremented to 3)
Ptr<IFloat> flt = NsDynamicCast<Ptr<IFloat> >(visible); // static cast is not possible
flt->SetFloat(1.0f);
flt->GetFloat();

// Here, we try to cast to Box. But Sphere does not implement the Box class. A null pointer
// is returned here and the reference count is left intact
Ptr<Box> box = NsDynamicCast<Ptr<Box> >(sphere); // box == 0

// Calling Reset is the way to release the reference managed by the Ptr. If you do not do this,
// the reference is automatically decremented when sphere goes out of scope.
// NEVER EVER do delete sphere!
sphere.Reset();

// Here, the component sphere created at the beginning is automaticaly destroyed
 
User avatar
Erio
Topic Author
Posts: 81
Joined: 06 Feb 2013, 17:30
Location: Orléans/France

Re: Ptr<> System question

19 Feb 2013, 16:01

Thanks a lot for the explanation and all
I guess i get it in a way, it's a way to use the Interface system and garbage collection :)

Well actually, the problem comes from the CreateRenderer(guiResource->GetPtr());

I'll show you my code:
bool NoesisGUI::initNoesis(int width, int height)
{
    NsConfigValue("Render.RenderSystem", "Render", "GL");

    NsGetKernel()->Init();

    HDC hdc = wglGetCurrentDC();
    Noesis::Render::IGLRenderSystem::SetContext(hdc, wglCreateContext(hdc));

    NsGetKernel()->InitSystems();

    Ptr<IUIResource> guiResource = NsDynamicCast<Ptr<IUIResource>>(NsGetSystem<IResourceSystem>()->Load("MyApps/RssReader/RssReader.xaml"));

    Ptr<IRenderTarget2D> rt = NsGetSystem<IGLRenderSystem>()->WrapRenderTarget(GLuint(0), width, height);
    Ptr<IVGLSurface> surface = NsGetSystem<IVGLSystem>()->CreateSurface(rt.GetPtr());
    m_root.Reset(NsStaticCast<UIElement*>(guiResource->GetRoot()));

    m_renderer = CreateRenderer(m_root.GetPtr()); //That's the line where the program crash from what i've seen using debug tools
    m_renderer->SetSurface(surface.GetPtr());
    m_renderer->SetSurfaceClearMode(SurfaceClearMode_Manual);
    m_renderer->SetAntialiasingMode(AntialiasingMode_MSAA);

    return true;
}
i do declare m_root and m_renderer in my class header:
private:
    Ptr<IRenderer> m_renderer;
    Ptr<UIElement> m_root;
There i get the next error when i run it (at least at debug, else it just crash):
"Exception Triggered:
The inferior stopped because it triggered an exception.

Stopped in thread 0 by: Exception at 0xc1027c220, code: 0xc0000005: read access violation at: 0x0, flags=0x0."

Then i looked up to the variables and found out this.

m_renderer.mPtr = 0x0 it's a Noesis::Gui::IRenderer type, i guess that's right. But how to initialise it to something else than 0x0 ?
Did i miss something a special initialization needed previously?

Thanks for helping me out with this! If i get it to work, then it'll be working with Qt and OGRE which would be very cool !

and by the way, while talking a bit about the doc, for the tutorial and examples, you should put up some code that are more complete than the one present in it, actually, it's missing all the includes you need to do for example, which gave me some really hard time (i got them looking at the spinning cube and shadows examples)
and maybe explain what are the variables that are declared l through the tutorial :)
I got a bit lost trying to use it as i didn't get the header side, or at least the include/declaration as Noesis.h doesn't include all the other part itself.

And thanks again for the time taken to answer :)
When i get it to work i'll post the minimum code here that people can use it to compile a minimum application using Qt, Ogre and Noesis :)
 
User avatar
jsantos
Site Admin
Posts: 2922
Joined: 20 Jan 2012, 17:18
Contact:

Re: Ptr<> System question

21 Feb 2013, 17:25

Hmmm... strange

could you confirm that after this
m_root.Reset(NsStaticCast<UIElement*>(guiResource->GetRoot()));
m_root is valid? (not null)
 
User avatar
Erio
Topic Author
Posts: 81
Joined: 06 Feb 2013, 17:30
Location: Orléans/France

Re: Ptr<> System question

21 Feb 2013, 23:59

It is, i tried all the way i found in examples and else.

m_root isn't null (it have a value of m_root.mPtr = "@0x7ef3ae80", but i guess it'll change at next debug session)

I can't upload the screenshot of the debug session right now.. (dunno why..)
but all there is is that all elements under the mPtr are... unavailable?!

mPtr.AllowDropProperty = <Value unavailable error> for example.. I can't tell if it does matter or if the debugger just can't read them.

Hope we'll be able to find out the problem :)
 
User avatar
Erio
Topic Author
Posts: 81
Joined: 06 Feb 2013, 17:30
Location: Orléans/France

Re: Ptr<> System question

23 Feb 2013, 23:00

Well. I just made a clean install of all my system, we'll see if i might have broke something while trying some other things.

I'll keep you informed of the state of it.
 
User avatar
jsantos
Site Admin
Posts: 2922
Joined: 20 Jan 2012, 17:18
Contact:

Re: Ptr<> System question

25 Feb 2013, 17:27

Yes, please, keep us informed.

By the way, what debugger are you using?

Remember that a Ptr<> behaves as a pointer. You must access members using -> not .
 
User avatar
Erio
Topic Author
Posts: 81
Joined: 06 Feb 2013, 17:30
Location: Orléans/France

Re: Ptr<> System question

25 Feb 2013, 17:45

I'm using CDB i guess on it's latest version

Yeah i did use them correctly, that's the debugger that can't access the members it seems ^^

I'm on reinstalling all my systems and set them up to be reading for school, so i'll try again as soon as all is set up !

(and maybe that by a miracle it'll work this time... I'm not joking, this kind of things already happened on my other laptop.. So. Who knows?)
 
User avatar
Erio
Topic Author
Posts: 81
Joined: 06 Feb 2013, 17:30
Location: Orléans/France

Re: Ptr<> System question

28 Feb 2013, 00:56

I might have found where could be the error.

Which version of MSVC did you use to compile the lib? 2008?

Because after making a virtual machine to check if the problem could come from win 8, i found out the debugging errors are coming from xutility and after from xstring, looks to be some things from MSVC2010.. a dealloc part, which i can barely understand due to it's complexity and from where it comes from.

I might try again with MinGW... If i can compile OGRE without Boost using it.

Else, i'm curious, but i guess that the problem is really MSVC itself ! It's the best option i found out actually as the errors aren't coming from Noesis file according to native debugging.

If you've got any solution, i'll try it out.

thanks for the time you accord to solve this. (and it it's really MSVC2010 itself the problem, then.. I don't know, but it'll show some problem, now i'll have to find where!)

Who is online

Users browsing this forum: Google [Bot] and 1 guest