Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2005-07

C++ > C wrapper callback > Pascal Continues UPDATE

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Urbansound

See bottom post

Earlier message 110 lines
----- Original Message ----- From: "Urbansound" To: Sent: Wednesday, July 27, 2005 11:05 PM Subject: C++ > C wrapper callback > Pascal Continues > > Reply Peter and Tomas, is below, each... Thread pruned. > >> 2. Re: C++ > C wrapper callback > Pascal Continues (Peter Vreman) >> 3. Re: C++ > C wrapper callback > Pascal Continues (Tomas Hajny) > >> ---------------------------------------------------------------------- >> Message: 2 >> Date: Wed, 27 Jul 2005 14:26:57 +0200 (CEST) > >> It is an internal function implicitly called by the compiler. > > The s call in the message line was converted to a "pointer(s)". I found > the FPC_INITIALIZEUNITS at Main, in the ASM output and understand now that > the compiler handles this fully and that FPC_INITIALIZEUNITS could not be > the issue. Perhaps this thread will help others new to FPC to know this > too on the chance they may research it. Thank you. Present code is at > bottom to thin down the thread. > > Tomas below, may be onto it, as a threading issue. Thanks still for your > help. Mike > >> >> Well, maybe I misunderstood the original post, but my understanding was >> that it's about a _Pascal_ program calling some C function (which is in >> fact just a wrapper for C++, but that shouldn't matter) and passing some >> other Pascal function as a callback. If this is the case and the callback >> function runs within the same thread as the block calling the C wrapper, >> Pascal units should be initialized since the main Pascal program startup, >> shouldn't they? If a new thread is started by the C++ program, you need >> to >> perform (Pascal) initialization of the thread (probably on the first >> entry >> to the callback function). Could you clarify this, please (link to the >> complete sources or preferrably their simplified but still compilable >> version would probably serve the best as already pointed out by Peter)? >> >> Tomas > > Thanks Tomas. > > 1) Current function is below, due to pruning the thread. > 1) Clearly I was off course on FPC_INITIALIZEUNITS > 2) You're understanding is correct C++api > C+wrpr > Pascal as callback > The wrapper is actually C code in a .cpp, for functionality > 3) The API is multi-threaded, having dl'd a thread monitor, showing as > many as 5 threads spawened to the API and accompanied executable. As > well, a call back occurs by default from the API, at runtime, suggesting > it fires off a single dummy callback event. > 4) Can you suggest an approach on the Pasacl Initialization to the thread > and/or some of the areas I should be looking up toward doing the work? > Google is my friend. ;-) > > Thank you again. Mike > > //------- Current function model below here > > function L1_Callback(quote : pointer) : integer; stdcall; export; > var > bid_price : single = 0.0; > s : ansistring = ''; > begin > SendMessage(listbox, LB_ADDSTRING, 0, longint(Pchar('L1 Callback'))); > // -- either commented below will cause crash > //SendMessage(listbox, LB_ADDSTRING, 0, longint(Get_secsym(quote))); > //bid_price := Get_l1_BidPrice(quote); > s := FloatToStr(bid_price); > SendMessage(ListBox,LB_ADDSTRING,0,Longint(pointer(s))) > end; > Update: Per Tomas suggestions: Among our C equivelent of the Pascal program, we did the following, which confirms that the C+ callback and Pascal, are on the same, single thread, which was confirmed with a thread monitoring tool during operation. (See comment at bottom). //---------Thread compare void ShowThreadInfo(char* id) { char s[80]; sprintf(s,"%s Thread ID: %08X Thread Handle: %08X",id,GetCurrentThreadId(),GetCurrentThread()); SendMessage(listbox, LB_ADDSTRING, 0, (LPARAM)s); } int _stdcall L1_Callback(void* quote) { //double bid_price; char s[100]; ShowThreadInfo("Callback"); //------------------------------------------ Given that an equivelent C program, (built by modifying the Pascal program to suit C syntax/method), runs without a problem and yet there is no evident reason code-wise for the problem, what is the next step in resolving the matter. I can use GDB, though I am new to it, as of this issue. If someone is willing to give me a guide on what command/data gdb can afford us, I'd certainly make the effort here, unless there are other suggestions? This is fpc 2.00, so I'm current w/stable.
Tomas Hajny
to Urbansound

From: "Urbansound"
To:
Sent: Wednesday, July 27, 2005 11:05 PM
Subject: C++ > C wrapper callback > Pascal Continues

.
.

> 3) The API is multi-threaded, having dl'd a thread monitor, showing as > many as 5 threads spawened to the API and accompanied executable. As > well, a call back occurs by default from the API, at runtime, suggesting > it fires off a single dummy callback event.

OK. As long as the callback is only called in one thread (the same one which passed the callback address as confirmed in your message below), number of other threads spawned by the C++ code shouldn't really matter.

.
.

Show quoted text 14 lines
> //------- Current function model below here > > function L1_Callback(quote : pointer) : integer; stdcall; export; > var > bid_price : single = 0.0; > s : ansistring = ''; > begin > SendMessage(listbox, LB_ADDSTRING, 0, longint(Pchar('L1 Callback'))); > // -- either commented below will cause crash > //SendMessage(listbox, LB_ADDSTRING, 0, longint(Get_secsym(quote))); > //bid_price := Get_l1_BidPrice(quote); > s := FloatToStr(bid_price); > SendMessage(ListBox,LB_ADDSTRING,0,Longint(pointer(s))) > end;

.
.

Among our C equivelent of the Pascal program, we did the following, which confirms that the C+ callback and Pascal, are on the same, single thread, which was confirmed with a thread monitoring tool during operation. (See comment at bottom).

.
.

Show quoted text 10 lines
Given that an equivelent C program, (built by modifying the Pascal program to suit C syntax/method), runs without a problem and yet there is no evident reason code-wise for the problem, what is the next step in resolving the matter. I can use GDB, though I am new to it, as of this issue. If someone is willing to give me a guide on what command/data gdb can afford us, I'd certainly make the effort here, unless there are other suggestions? This is fpc 2.00, so I'm current w/stable.

I'm afraid I'm not the right one to teach GDB commands. ;-) However, I believe that setting up a HW breakpoint to the address of the Pascal callback ("hbreak L1_CALLBACK"?) should indeed allow you to check why it crashed (e.g. "bt" command in GDB should show you where exactly the crash happens which in turn should help to find the reason). You should compile your program with debugging enabled RTL, though. Based on the particular error, I'd suggest to watch the stack while tracing through the code, because my guess is that a thrashed stack directs you to an improper address.

Tomas