GC concerns with c++

BlitzMax Forums/BlitzMax Programming/GC concerns with c++

col(Posted 2011) [#1]
Hi all,

I'm playing around with c++ and BMax and I have some concerns with what would be the 'proper' way to allocate objects with concern to the garbage collector working properly and/or possible memory leak issues.

I'm doing things this way.....

I create a new Type in BMax, create a byte pointer ( bmax null ), pass the pointer into c++, which will become an address, then memcopy the data across from the byte ptr to the Bmax object.

Is this the correct way to do it for the GC to work properly?
The address of the BMax object isnt changed at all or the size of the BMax object, only the data within it.
I was doing things so that it changed the address of the object itself but I thought this would be wrong?

So before I go any further with the project, would some kind people let me know what the best/correct way to do it is?

Many thanks


JoshK(Posted 2011) [#2]
BMX:
Extern "C"
Type MyType
Field x:Int,y:Int,z:Int
EndType
Function CreateMyType:MyType()
Function FreeMyType(o:MyType)
EndExtern

local o:MyType

o = CreateMyType()
Print o.x
FreeMyType o

C++:
MyType* CreateMyType()
{
return new MyType;
}

void DeleteMyType(MyType* o)
{
delete o;
}


Last edited 2011


col(Posted 2011) [#3]
As in the Bmax Help example...

Thanks for the info.

Although in your example - slight typo in the c++ code - should be...

MyType* CreateMyType()
{
return new MyType;
}

void FreeMyType(MyType* o)
{
delete o;
}

I know what you meant but others may have got confused

Last edited 2011


Brucey(Posted 2011) [#4]
Of course in this example, you are managing the objects yourself, not through the GC.


col(Posted 2011) [#5]
I've looked through the Bmax DirectX source and it uses Extern types for the interfaces and bmax types for structures that are used with the interfaces.

I assume this is safe to use so I'm using the same style.

The reason behind my original question is that DirectX can be used, and does create new instances of 'types' or structures, and I want to use those same structures in the BMax code. Hence I was creating a new BMax type of the structure and memcopying the data across. I'm also releasing them in c++ and Bmax to make double sure. I just wasnt sure if this is the 'correct' way to do it and would the GC pick up on something that I may miss?