Author Topic: Segmentation fault using Beep()  (Read 12114 times)

Offline tom0769

  • Multiple posting newcomer
  • *
  • Posts: 42
Segmentation fault using Beep()
« on: January 30, 2007, 09:50:43 am »
Hi,

on my notebook (win XP home edition sp2) I get always "Segmentation fault" when calling winapi function "Beep".
MessageBeep() works fine.
On my desktop PC (win XP prof. sp2) and my older notebook (win 2k sp4) the same projects run without any problems.
On all systems C::B svn3545 is installed.
I investigated the file versions/dates of comctl32.dll and they are (of course) different.
Could this be the problem, or is this a problem of the sound settings in the windows control panel?

thanx
Tom

Offline Biplab

  • Developer
  • Lives here!
  • *****
  • Posts: 1874
    • Biplab's Blog
Re: Segmentation fault using Beep()
« Reply #1 on: January 30, 2007, 10:21:45 am »
Beep() is defined in Kernel32.dll (link lib Kernel32.lib) whereas MessageBeep() is defined in user32.dll (link lib user32.lib).

Beep() api description:
Quote
http://msdn2.microsoft.com/en-us/library/ms679277.aspx

MessageBeep() api description:
Quote
http://msdn2.microsoft.com/en-us/library/ms680356.aspx

So comctl32.dll is not the problem.  :)
Be a part of the solution, not a part of the problem.

Offline tom0769

  • Multiple posting newcomer
  • *
  • Posts: 42
Re: Segmentation fault using Beep()
« Reply #2 on: January 30, 2007, 10:49:42 am »
Thanx for reply,
so what could be the cause for the segmentation fault:
- the Kernel32.dll?
- the Kernel32.lib?
I do not have a clue!  :?

Offline Biplab

  • Developer
  • Lives here!
  • *****
  • Posts: 1874
    • Biplab's Blog
Re: Segmentation fault using Beep()
« Reply #3 on: January 30, 2007, 11:08:19 am »
Sorry tom0769, I am also not sure about this.  :(

Just guessing, if your kernel32.dll version in both the system is same, then kernel32.lib may be the cause of problem. Are you using same compiler and SDK in both the PCs?
Be a part of the solution, not a part of the problem.

Offline tom0769

  • Multiple posting newcomer
  • *
  • Posts: 42
Re: Segmentation fault using Beep()
« Reply #4 on: January 31, 2007, 08:38:07 am »
Hi Biplab, thanx for the hint!
Since I'm using the same MinGW compiler version on both PCs, the compiler libs are exactly the same.
I did a binary file compare of the versions of kernel32.dll and they are also equal.
Running the program in release mode, there is no program crash when calling "Beep(2000, 1000);" .
I measured the runtime of the function call with "GetTickCount" and it's almost exactly 1sec,
but there is nothing to hear.
« Last Edit: January 31, 2007, 10:16:36 am by tom0769 »

Offline Biplab

  • Developer
  • Lives here!
  • *****
  • Posts: 1874
    • Biplab's Blog
Re: Segmentation fault using Beep()
« Reply #5 on: January 31, 2007, 12:55:43 pm »
Did you check the return value you are getting? It should return Non-zero values on Successful completion.
Be a part of the solution, not a part of the problem.

Offline tom0769

  • Multiple posting newcomer
  • *
  • Posts: 42
Re: Segmentation fault using Beep()
« Reply #6 on: February 11, 2007, 08:12:09 pm »
Hi,

I used the following code to track the error:

    int ret = Beep(1000,500);
    if (ret != 0)
    {
        char szMsgBuf[MAX_PATH] = "";
        DWORD dwLastError = GetLastError();
        wsprintf(szMsgBuf,"Error #%ld: ", dwLastError);
        FormatMessage(  FORMAT_MESSAGE_FROM_SYSTEM,
                        NULL,
                        dwLastError,
                        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                        &szMsgBuf[lstrlen(szMsgBuf)],
                        MAX_PATH,
                        NULL );
        MessageBox( m_hWnd, szMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION );
    }

The message box shows "Error #6: Invalid handle" (tranlated from german "Error #6: Das Handle ist ungülltig")

Seems a little bit strange to me since the return value of Beep is definetly not zero  :?.

Anybody with a clue?

Thanx
Tom


Offline Biplab

  • Developer
  • Lives here!
  • *****
  • Posts: 1874
    • Biplab's Blog
Re: Segmentation fault using Beep()
« Reply #7 on: February 17, 2007, 11:13:14 am »
Hi tom0769,

I didn't see your reply. :)

Did you check the return value you are getting? It should return Non-zero values on Successful completion.

As I wrote in my last reply that the function will return Non-zero value on successful completion. So you must check for errors only when the ret == 0 but NOT when it is non-zero. So change the following code to-

Code
    int ret = Beep(1000,500);
    if (ret == 0)
    {

In my PC, the function returns 1 after execution which means it's a successful execution. :)

AFAIK, GetLastError() does not take any argument as input, it only reports the last error which may not be, in this case, related to Beep() function. :)
Be a part of the solution, not a part of the problem.

Offline tom0769

  • Multiple posting newcomer
  • *
  • Posts: 42
Re: Segmentation fault using Beep()
« Reply #8 on: February 19, 2007, 07:44:07 am »
Hi Biplap,

Thanks for reply! :)
It is really so, that the Beep() function returns with a value non-zero.
So the conclusion would be that there is something behind the Beep() function that fails
and GetLastError() shows the result of this "something behind Beep()".
Is there a chance to evaluate this?  :?

Offline Biplab

  • Developer
  • Lives here!
  • *****
  • Posts: 1874
    • Biplab's Blog
Re: Segmentation fault using Beep()
« Reply #9 on: February 19, 2007, 07:58:25 am »
Is your application still crashing after Beep() function? If it's so then you may look backwards or just after your beep function, where that Particular Handle related problems occur. Try using a debugger. :)

A Beep() should not give you so much trouble. ;)
Be a part of the solution, not a part of the problem.

Offline tom0769

  • Multiple posting newcomer
  • *
  • Posts: 42
Re: Segmentation fault using Beep()
« Reply #10 on: February 19, 2007, 01:08:36 pm »
If I do not run the application in DEBUG mode, it does not crash.
Only when debugging the gdb throws a message ("Segmentation fault ...") and stops.
If I set a breakpoint just to the Beep()-instruction, doing a
"Next line", "Next instruction" or "Step into" causes the error.
If I run the application in RELEASE mode (see code snippet), Beep() does not return zero
but GetLastError() returns "Invalid handle" code (#6).
The application pauses for the parameter time given to Beep(), but there is no sound (works like Sleep()).
So I think there must be a problem "behind" Beep(), maybe Beep() takes an action
on sound card, which causes the problem? :?
Is there any possibility to debug the Beep() function?

Since now I tried several sound settings in the control panel and did not succeed.
Should I examine the settings of the services? (may be risky :?)


Offline Biplab

  • Developer
  • Lives here!
  • *****
  • Posts: 1874
    • Biplab's Blog
Re: Segmentation fault using Beep()
« Reply #11 on: February 19, 2007, 06:38:24 pm »
You are quite correct. The release target seems to have problem.

Though in my pc there is NO crash in running Release target but it generates Error# 6 in Release target. In Debug target there is no such error.

I think you should write about your problem to MinGW mailing list. They can help you better. :)
Be a part of the solution, not a part of the problem.

Offline tom0769

  • Multiple posting newcomer
  • *
  • Posts: 42
Re: Segmentation fault using Beep()
« Reply #12 on: February 20, 2007, 12:04:23 pm »
You are quite correct. The release target seems to have problem.

Though in my pc there is NO crash in running Release target but it generates Error# 6 in Release target. In Debug target there is no such error.

I think you should write about your problem to MinGW mailing list. They can help you better. :)

Thank you Biplab! :D
I'll post this to the MinGW forums.
Surely it's the better place to discuss it since it probably has nothing to do with C::B itself.