Strange SIGSEGV when I exit my FB & C++ program

DOS specific questions.
Post Reply
Cpcdos
Posts: 207
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Strange SIGSEGV when I exit my FB & C++ program

Post by Cpcdos »

Hi,

I've a strange problem with my FreeBasic code. I've declared my C++ function "CPC_LOADER" for using this in freebasic, no problem, when I use this function, this works ;) But when I use JUST ONE TIME my c++ function AND I exit my FreeBasic program, i've Segment Violation signal..
My FreeBasic code works properly and exit when I not use 1 C/C++ function (Commented). But he crashs when I use this.. (Not commented)

My C++ code :

Code: Select all

#include <cstring>
#include <sstream>
#include <iostream>

namespace cpinti
{
	int cpinti_core::CPC_LOADER()
	{
		std::cout << " *** C++ execute ***" << std::endl;
		return 0;
	}
}
And my FreeBasic code with results : Image

PS :
- Forget the lines 24 to 36 !
- I've forgot the return "__CPCDOS_INIT_1 = 0" but it's the same problem

Can you explain, why My freeBasic program Crash at the end when I use one time an C++ Function ?

Regards
marcov
Posts: 3454
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Strange SIGSEGV when I exit my FB & C++ program

Post by marcov »

Cpcdos wrote: Can you explain, why My freeBasic program Crash at the end when I use one time an C++ Function ?
If you link the C++ code, but don't call the function it is ok?

Then we can rule out exitcode (DTOR) of C++ library, and obviously somewhat causes the function to overwrite some critical memory, potentially the stack. As to possible causes I can see three cases:

- calling conventions don't match
- the C++ function is faulty.
- the C++ function is generally OK but behaves badly because (part of ?) its runtime library is not initialized (properly?)
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Strange SIGSEGV when I exit my FB & C++ program

Post by dkl »

Are you sure the functions have the same signature and calling convention? Remember that g++ may default to __fastcall for C++ methods, while FB only supports cdecl or stdcall.
Cpcdos
Posts: 207
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Re: Strange SIGSEGV when I exit my FB & C++ program

Post by Cpcdos »

Thank you for your reply :)
If you link the C++ code, but don't call the function it is ok?
Yes, it is ok! :) If i call just one time, it is not ok
- calling conventions don't match
Remember that g++ may default to __fastcall for C++ methods, while FB only supports cdecl or stdcall.
I've tested to compile with -mrtd parameter for compile in stdcall by defaut in my G++ (GPP.exe for dos) compiler, my program crash directly .. I think that my C libs is not compatible.. So it's not a solution.. :(

I must write an __fastcall wrapper with fb asm function ? Push right to left my arguments into stack ? euh.. I've not argument..
I've same problem if I remplace "int"/"integer" by "void"/"sub" for the return
- the C++ function is faulty.
- the C++ function is generally OK but behaves badly because (part of ?) its runtime library is not initialized (properly?)
I don't know :(

I do not know what to do,
Regards
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Strange SIGSEGV when I exit my FB & C++ program

Post by dkl »

FB-dos defaults to cdecl, so maybe it works when specifying

Code: Select all

__cdecl
or

Code: Select all

__attribute__((__cdecl__))
on the C++ (g++) side. To be safer to can also specify cdecl on the FB side.
Cpcdos
Posts: 207
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Re: Strange SIGSEGV when I exit my FB & C++ program

Post by Cpcdos »

I've declared my FB function like

Code: Select all

declare sub CPC_LOADER cdecl ()
And my C++ function

Code: Select all

#define __cdecl __attribute__((__cdecl__))
namespace cpinti
{
	class cpinti_core
	{
		public:
			void __cdecl CPC_LOADER();
	};
}

void __cdecl cpinti_core::CPC_LOADER() 
{
	std::cout << " *** C++ instance ***" << std::endl;
	// return 0;
}
Same problem.. :( Stack corrupted ? Not compatible ?
Cpcdos
Posts: 207
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Re: Strange SIGSEGV when I exit my FB & C++ program

Post by Cpcdos »

My GPP compiler arguments :

Code: Select all

std=c++14 -m32 -O2 -march=i486 -s -funroll-loops -Wall -ffast-math
FreeBasic :

Code: Select all

-O 3 -arch 486 -w all -lang fb -RR -t 8192 -l stdcxx
Post Reply