Xcode cannot find label 'error' for PortAudio - c++

I'm attempting to initialise portaudio as described in the Initialising PortAudio tutorial.
It says to check if there was an error during initialisation like so:
PaError err = Pa_Initialize();
if (err != paNoError) goto error;
Which is the exact code I'm using.
I'm running this on OS X Mojave 10.14.4, using Xcode 10.1 with the 10.12 OS X SDK.
I've attempted to find where the error label in the PortAudio docs to no avail, and there are no variables in the file named error.
The full program so far is:
# include <iostream>
# include "portaudio.h"
using namespace std;
// Typedef and demo callbacks here.
int main(int argc, const char * argv[])
{
PaError err = Pa_Initialize();
if (err != paNoError) goto error;
// Nothing here yet.
err = Pa_Terminate();
if (err != paNoError)
{
printf("Port audio error terminating: %s", Pa_GetErrorText(err));
}
return 0;
}
As far as I can tell in the tutorial, this should be a valid statement, but Xcode shows the syntax error:
Use of undeclared label 'error'

Inspecting the c++ reference for goto statements an example program for PortAudio, the problem came from assuming that goto can access things defined in the portaudio.h file, which is not the case.
If you're having this issue, I assume you're also unfamiliar with goto statements.
The tutorial assumes there's a section of the main function dedicated to resolving errors. In order to resolve this issue, we need to define an error label in our main function that takes care of responding to errors.
For example:
int main(void) {
PaError err;
// Checking for errors like in the question code, including goto statement.
return 1; // If everything above this goes well, we return success.
error: // Tells the program where to go in the goto statement.
Pa_Terminate(); // Stop port audio. Important!
fprintf( stderr, "We got an error: %s/n", Pa_GetErrorMessage(err));
return err;
}

Related

RTMP_Init “Segmentation fault”

I am using librtmp to stream video.I install librtmp in ubuntu 16.04 with "sudo apt-get librtmp-dev".I successfully compiled the code,but I got a "Segmentation fault" in RTMP_Init().Here is my rtmp_test code:
main function:
int main(int argc,char *argv[]){
printf("11111\n");
if (RTMP264_Connect("rtmp://**********") == TRUE){
printf("connect successful");
}
printf("22222222\n");
return 0;
}
RTMP264_Connect function:
int RTMP264_Connect(const char * url){
printf("~~~~~~~1\n");
RTMP_Init(m_Rtmp);
printf("~~~~~~~2\n");
if(RTMP_SetupURL(m_Rtmp,(char*)url) == FALSE){
RTMP_Free(m_Rtmp);
return FALSE;
}
printf("~~~~~~~3\n");
RTMP_EnableWrite(m_Rtmp);
if(RTMP_Connect(m_Rtmp,NULL)==FALSE){
RTMP_Free(m_Rtmp);
return FALSE;
}
When I run the code,I got this error:
11111
aaaa
~~~~~~~1
Segmentation fault
So I'm sure the problem is in RTMP_init function.I saw someone on the Internet saying that the reason may be that socket is not initialized. I found a socket initialization code:
int InitSockets()
{
#ifdef WIN32
WORD version;
WSADATA wsaData;
version = MAKEWORD(1, 1);
return (WSAStartup(version, &wsaData) == 0);
#endif
}
void CleanupSockets()
{
#ifdef WIN32
WSACleanup();
#endif
}
But this is for Windows system. If I run on Ubuntu, I will report an error. And I don't know if my problem has something to do with socket not being initialized.Could someone tell me how to fix my problem,thanks!
According to the documentation:
The basic interaction is as follows. A session handle is created using RTMP_Alloc() and initialized using RTMP_Init().
As you haven't called RTMP_Alloc this is presumably the cause of the segfault

Libzip - Error: Error while opening the archive : no error

I'm trying to find out the solution to solve a problem;
In fact, i'm writing my own tool to make saves using libzip in C++ to compress the files.
Absolutly not finished but i wanted to make some tests, then i do and obtain a "funny" error from the log.
Here's my function:
void save(std::vector<std::string> filepath, std::string savepath){
int err;
savepath += time(NULL);
zip* saveArchive = zip_open(savepath.c_str(), ZIP_CREATE , &err);
if(err != ZIP_ER_OK) throw xif::sys_error("Error while opening the archive", zip_strerror(saveArchive));
for(int i = 0; i < filepath.size(); i++){
if(filepath[i].find("/") == std::string::npos){}
if(filepath[i].find(".cfg") == std::string::npos){
err = (int) zip_file_add(saveArchive, filepath[i].c_str(), NULL, NULL);
if(err == -1) throw xif::sys_error("Error while adding the files", zip_strerror(saveArchive));
}
}
if(zip_close(saveArchive) == -1) throw xif::sys_error("Error while closing the archive", zip_strerror(saveArchive));
}
I get a => Error : Error while opening the archive : No error
And, of course, i didn't have any .zip written.
If you could help me, thanks to you !
The documentation for zip_open says that it only sets *errorp if the open fails. Either test for saveArchive == nullptr or initialize err to
ZIP_ER_OK.
P.S. The search for '/' does nothing. Did you mean to put a continue in that block?
The other problematic line is:
savepath += time(NULL);
If that is the standard time function, that returns a time in seconds since the epoch. That will probably get truncated to a char, and then that char appended to the file name. That will cause strange characters to appear in the filename! I suggest using std::chrono to convert to text.

SymInitialize despite returning true is setting GetLastError to 2 what all this means?

I use this compiler: http://sourceforge.net/projects/mingwbuilds/files/host-windows/releases/4.7.2/32-bit/threads-posix/sjlj/x32-4.7.2-release-posix-sjlj-rev7.7z
code:
#include <windows.h>
#include <iostream>
#include <imagehlp.h>
int main() {
HANDLE process = GetCurrentProcess();
if (GetLastError()) {
printf("GetCurrentProcess failed: %d\n", GetLastError());
return 1;
}
if (!SymInitialize(process, NULL, TRUE)) {
printf("SymInitialize failed: %d\n", GetLastError());
return 1;
} else if (GetLastError()) {
printf("SymInitialize returned true but failed nonetheless: %d\n", GetLastError());
return 1;
}
}
It returns: SymInitialize returned true but failed nonetheless: 2
error no 2 means "The system cannot find the file specified." What file, I can't put it in any context here. I understand from documentation that if this function succeeds GetLastError must be zero.
It is very common that functions DON'T reset the error when successful. And it's entirely possible that "SymInitialize" internally calls some function that "Look for a file at dir1, look for a file at dir2, ...", and the error from "I couldn't find it in dir1" is 2. Now, you have to actually call SetLastError(0) to clear the error-code, and it may be an oversight or intentionally that the code in SymInitialize doesn't set that. You need to check the result of SymInitialize to tell if it was a success or not. If it's NOT a success, you look at the error code. But GetLastError() is just that - the last error that happened, not "did my last function succeed!"

luaL_dofile Error

Error at line "luaL_dofile" and debugger doesn't show anything about error.
I can use command "luaL_dostring" but I don't know why I can't dofile.
My code is following:
const char* file = "/app_home/data/minigames/mg_hint_machine_2.lua";
ret = luaL_dofile(LS, file);
if(ret != 0){
PRINTF("Error occurs when calling luaL_dofile() Hint Machine 0x%x\n",ret);
}
else PRINT("\nDOFILE SUCCESS");
and debugger shows error in this line and "ret" still not get returned value from dofile.
If you want to see about error in debugger
02C2D304 7C21016A stdux r1,r1,r0 03 (02C2D300) REG PIPE LSU
Debugger points in this line and I can't understand it.
As an elaboration on superzilla's answer (upvote that answer rather than this one),
to get the error message your code needs to look like this:
const char* file = "/app_home/data/minigames/mg_hint_machine_2.lua";
ret = luaL_dofile(LS, file);
if(ret != 0){
PRINTF("Error occurs when calling luaL_dofile() Hint Machine 0x%x\n",ret);
PRINTF("Error: %s", lua_tostring(LS,-1));
}
else PRINT("\nDOFILE SUCCESS");
Your change (in the comments) changed the luaL_dofile to a luaL_dostring, which is why you're getting unexpected error message ( as mentioned here ).
Putting this in the body of your if statement will help us narrow down the problem:
printf("%s\\n",lua_tostring(LS,-1));
It'll tell us what Lua is reporting when it crashes.

How to read the failure log message displayed when a system call failed in C++?

I have a C++ code that calls a test. I am doing a system call to execute this test. When this test fails, it will display something like this " ERROR: One or more devices of following component type(s) could not be discovered:"
I have a C++ code that runs on Linux redhat and it is capable of detecting if the system call pass or failed. But it can not capture the error message (ERROR: One or more devices of following component type(s) could not be discovered:) and append into the log file or print it.
Can someone please tell me how to capture the error message (ERROR: One or more devices of following component type(s) could not be discovered:)?
PS: I am an intern, any help would be really nice.
#include<iostream.h>
int main ()
{
int i;
if (system(NULL))
puts ("Ok");
else
exit (1);
i=system("hpsp --discover -verbose --user Admin --oapasswd password");
printf ("The value returned was: %d.\n",i);
return false;
}
Instead of using system() use popen(). This will open a pipe capturing the standard output of the test program so that your program can read it via the pipe.
Example EDITED:
#define _BSD_SOURCE 1
#define BUFFSIZE 400
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *cmd = "hpsp --discover -verbose --user Admin --oapasswd password";
char buf[BUFFSIZE];
char* searchResult;
int testPassed = 0;
FILE *ptr;
if ((ptr = popen(cmd, "r")) != NULL)
while (fgets(buf, BUFFSIZE, ptr) != NULL)
{
if ((searchResult = strstr(buf, "The test passed")) != NULL )
{
testPassed = 1;
break;
}
}
if (testPassed)
printf("yea!!\n");
else
printf("boo!!\n");
pclose(ptr);
return 0;
}
You can use dup and dup2 to backup/store the stderr file descriptor to redirect to your log file. Well, I'm guessing that errors go to stderr anyways.
Here's an example if you just want to write to a log file.
//open log file, choose whatever flags you need
int logfd = open("whateveryourlogfileis", O_APPEND);
//back up stderr file descriptor
int stderr_copy = dup(STDERR_FILENO);
//redirect stderr to your opened log file
dup2(logfd, STDERR_FILENO);
//close the original file descriptor for the log file
close(logfd);
//system call here
//restore stderr
dup2(stderr_copy, STDERR_FILENO);
//close stderr copy
close(stderr_copy);
Note: dup2 closes the target file descriptor before dup2ing to it. dup just duplicates the file descriptor and returns to you the new file descriptor.