English Amiga Board

English Amiga Board (http://eab.abime.net/index.php)
-   Coders. C/C++ (http://eab.abime.net/forumdisplay.php?f=118)
-   -   Why printf in hook function freezes everything? (http://eab.abime.net/showthread.php?t=102912)

Sim085 29 June 2020 13:42

Why printf in hook function freezes everything?
 
I have another question with regards to hooks. I have added a hook to a STRING_KIND gadget. Everything works fine if I handle the input from as IDCMP_GADGETUP message. However if I put a printf in the hook function everything will freeze up (even mouse pointer, need to restart). Why does this happen? Do I need to use some command before and after the printf so that this does not happen.

S0ulA55a551n 29 June 2020 13:50

what would it be printing too ?

Sim085 29 June 2020 14:05

In CLI (shell).

Just to give an example, if I put printf in hook function as follows every freezes;

Code:

ULONG editfield(struct Hook *hook, struct SGWork *obj, ULONG *msg)
{
  printf("something");
}

But the following works ok;

Code:

while(run){
  ...
  if(msgClass == IDCMP_GADGETUP){
      printf("something");

  }
  ...

(I removed code to simplify example).

Quote:

Originally Posted by S0ulA55a551n (Post 1410617)
what would it be printing too ?


mark_k 29 June 2020 14:12

Maybe the hook function is called by input.device, not your task? Try having the hook function signal your task (which is a Process and so can call DOS functions) and do the printf from there.

Sim085 29 June 2020 14:21

From the examples I found on the internet the hook is simply used to send messages back through IDCMP. Like that it works.

So is the hook function for STRING_KIND only to determine when to raise IDCMP messages back to solution?

For example I also tried to edit another gadget, but this also freezes the moment I try to refer to some function I have written.

Quote:

Originally Posted by mark_k (Post 1410622)
Maybe the hook function is called by input.device, not your task? Try having the hook function signal your task (which is a Process and so can call DOS functions) and do the printf from there.

Edit: Just to clarify, I can do everything through IDCMP. The reason I am asking the question is that I did not understand why it freeze when I was trying to do same things directly from the hook. From what I understand (now), this will fail as the hook code is not running as part of my process and therefore everything breaks when I think I am modify memory dedicated to my process when in reality this would not be the memory of my process. I suppose this is where the hook h_Data comes in.

Thomas Richter 29 June 2020 19:39

Quote:

Originally Posted by Sim085 (Post 1410616)
I have another question with regards to hooks. I have added a hook to a STRING_KIND gadget. Everything works fine if I handle the input from as IDCMP_GADGETUP message. However if I put a printf in the hook function everything will freeze up (even mouse pointer, need to restart). Why does this happen? Do I need to use some command before and after the printf so that this does not happen.


No, this cannot work. The best you can do is to print a string over the serial port. Or use a low-level debugger like COP.


The string hook is called from intuition when receiving input events, and thus within the intuition input handler, which keeps many intuition resources locked. Printing requires (through many indirection layers) locking the same resources, so this blocks forever.


All times are GMT +2. The time now is 09:48.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, vBulletin Solutions Inc.

Page generated in 0.04573 seconds with 11 queries