extending a C++ class with GC ?

About Monkey 2 Forums Monkey 2 Programming Help extending a C++ class with GC ?

This topic contains 7 replies, has 3 voices, and was last updated by  codifies 2 years, 5 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #5205

    codifies
    Participant

    I’m trying to extend a C++ class (without modifying it) so it can be GC’d like a “normal” MX2 object…

    Now to be honest C++ has to be my least favourite language, and thus not something I’m particularly strong in, so its entirely possible I’m doing something wrong…

    I’m wrapping the Rectangle class into RectangleMX

    and (finally) how I’m “testing” it…

    Currently I’m stumped as it seems to be getting GC’d immediately on creating, and I’d have thought that the rectangleMX destructor would have been called to – but then that shows you what I know about c++

    Attachments:
    1. cppWrap.zip
    #5212

    Mark Sibly
    Keymaster

    Close!

    The problem is with the ‘disambiguation’ bit in RectangleMX’s ctor – your code actually creates a temporary Rectangle on the stack that is immediately destroy when RectangleMX’s ctor exits.

    To call a super class ctor from a ctor you need to use this special syntax:

    #5215

    codifies
    Participant

    cool, thanks mark 🙂 (just why I dislike c++ 😉 )

    while it works, the Rectangle destructor is never called ?

    I’ve tried assigning multiple instances to the same local in a function called 1000’s of times while calling GCCollect() in the loop calling the function assigning the classes….

    #5216

    Mark Sibly
    Keymaster

    Try this to create the rectangle:

    This is mx2’s special ‘new’ to create GC aware objects – it must be used with classes that extend bbObject.

    This makes this approach unsuitable for libraries that use ‘factory’ methods to produce objects, but it should work for bullet?

    #5217

    codifies
    Participant

    ding ding ding, we have a winner, thanks Mark 😀

    I wasn’t actually thinking of bullet (as anyhow you promised it soon ;-p – still tempted to throw a very limited temporary thing together mind…)

    Very good point about factory methods but you could always call the factory place the pointer in a field of a GC’d class that you *then* return from the wrapper… :-/ !

    I’m thinking about putting together a blog post about wrapping for MX2 and including this idea as well, you wouldn’t happen to have any other wrapping secret sauce, that you think would be good to go in such a blog post would you?

    #5219

    Mark Sibly
    Keymaster

    but you could always call the factory place the pointer in a field of a GC’d class that you *then* return from the wrapper… :-/ !

    Yes, and this is what I thought you were thinking of in the first place re: bullet. But I like this abuse of multiple inheritance much more (in fact, the litehtml modules uses this approach) – separate wrapper objects get messy as you need to foward ALL methods and allowing users to override virtual methods is tricky.

    #5224

    abakobo
    Participant

    Could you post the entire rectangle example codes? I have difficulties with the gcnew in cpp side and how it becomes a simple new in mx2. (Or you can’t ‘new’ it in mx2?)
    Thx

    #5225

    codifies
    Participant

    I’m tidying the code and just writing it all up now, I’ll post the link before long…

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.