Page 2 of 2

Re: Purebasic interface to Box2D / Chipmunk2D [solved]

Posted: Fri Nov 27, 2015 5:52 am
by Lunasole
And again there are problems making me stuck. Don't want to make another dll-import-problem thread so leaving it here.

Why does this code leads to data corruption (or stack corruption, not sure) and how it is possible to handle returning structure (not pointer) correctly in PB?

Code: Select all

; Create an empty space.
Define *space.cpSpace = cpSpaceNew()	; cpSpaceNew returns a pointer to structure stored inside external lib

; it is all OK, the *space data is valid
Define *testG.cpVect = cpSpaceGetGravity (*space) ; cpSpaceGetGravity returns a whole cpVect structure, not just a pointer
; now the first fields of *space data structure are corrupted [seems one first field which size is 4 bytes, as pointer size]
The called C function:

Code: Select all

typedef double cpFloat;
typedef struct cpVect{cpFloat x,y;} cpVect; 

cpVect
cpSpaceGetGravity(const cpSpace *space)
{
	return space->gravity; 
}
That's how it is declared in PB:

Code: Select all

Macro cpFloat			: d : EndMacro
Structure cpVect ; Align not needed, cause lib temporary re-compiled with 1 byte align
	X.cpFloat
	Y.cpFloat
EndStructure

cpSpaceGetGravity.i   (*space.cpSpace) As "_cpSpaceGetGravity";  = cpVect

Re: Purebasic interface to Box2D / Chipmunk2D

Posted: Fri Nov 27, 2015 8:00 am
by infratec
Hi,

I'm not really the specialist for such cases, but in my opinion:

It returns a pointer, but you have to copy the structure immediately to a 'real' structure.
Maybe it is later no longer available.

Bernd

Re: Purebasic interface to Box2D / Chipmunk2D

Posted: Fri Nov 27, 2015 3:08 pm
by Lunasole
infratec wrote:Hi,

I'm not really the specialist for such cases, but in my opinion:

It returns a pointer, but you have to copy the structure immediately to a 'real' structure.
Maybe it is later no longer available.

Bernd
Just tried something like this, all the same:
Image

Re: Purebasic interface to Box2D / Chipmunk2D

Posted: Fri Nov 27, 2015 3:43 pm
by Lunasole
It seems to be another annoying PB limitation. And I am not familiar with ASM to fix it.
If I haven't access to a lib sources it would be total damn disaster. But fortunately I have, so modified called function this way and it works fine:

Code: Select all

void
cpSpaceGetGravity(const cpSpace *space, cpVect *lp_out)
{
	*lp_out = space->gravity;
}
Now calling this way

Code: Select all

Define Gout.cpVect
cpSpaceGetGravity (*space, Gout)

Re: Purebasic interface to Box2D / Chipmunk2D

Posted: Fri Nov 27, 2015 5:28 pm
by DontTalkToMe
infratec wrote: It returns a pointer, but you have to copy the structure immediately to a 'real' structure.
Unfortunately (for us using PB) in C you can return structures by value and I think (not sure) the way to do so it's not specified in the language.

EDIT
http://stackoverflow.com/questions/2155 ... structures
http://stackoverflow.com/questions/9653 ... ction-in-c

Re: Purebasic interface to Box2D / Chipmunk2D

Posted: Sat Nov 28, 2015 12:10 am
by Lunasole
DontTalkToMe wrote:
infratec wrote: It returns a pointer, but you have to copy the structure immediately to a 'real' structure.
Unfortunately (for us using PB) in C you can return structures by value and I think (not sure) the way to do so it's not specified in the language.

EDIT
http://stackoverflow.com/questions/2155 ... structures
http://stackoverflow.com/questions/9653 ... ction-in-c
> The address of the caller's Data return value is actually passed as a hidden argument to the function, and the createData function simply writes into the caller's stack frame.

Sounds interesting. So technically there is no difference between

Code: Select all

Define ReturnStruct
someFunct (@ReturnStruct)
and returning structure byval in C/C++ compilers style. And no arguments against using such return.

I'd like to experiment trying passing that "hidden pointer" (it might be possible without using ASM) but it is too late, I've already changed all 41 problematic functions to void and using *lp_out as return