Need help correcting Access Violation with FFmpeg DLL - c++

I'm trying to use the FFmpeg dll's using Visual Studio 2012 and I'm getting a run time access violation when I call avcodec_find_encoder. Here is the code:
// TestFFmpeg.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
extern "C" {
#include "libavcodec\avcodec.h"
#include "libavformat\avformat.h"
}
#define INBUF_SIZE 4096
int _tmain(int argc, _TCHAR* argv[])
{
AVCodec *codec;
const char *videoFilename = "C:\\Users\\Public\\Videos\\Sample Videos\\Wildlife.wmv";
av_register_all(); // This works; however, no parameters or return values.
codec = avcodec_find_encoder(CODEC_ID_WMV3); // Run time Access Violation HERE
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}
return 0;
}
Here is the error message:
Unhandled exception at 0x75C18B60 (msvcrt.dll) in TestFFmpeg.exe: 0xC0000005: Access violation reading location 0x00000049.
The stack trace is:
msvcrt.dll!_strcmp() Unknown
avcodec-54.dll!6a56caac() Unknown
[Frames below may be incorrect and/or missing, no symbols loaded for avcodec-54.dll]
> TestFFmpeg.exe!wmain(int argc, wchar_t * * argv) Line 23 C++
TestFFmpeg.exe!__tmainCRTStartup() Line 533 C
TestFFmpeg.exe!wmainCRTStartup() Line 377 C
kernel32.dll!#BaseThreadInitThunk#12() Unknown
ntdll.dll!___RtlUserThreadStart#8() Unknown
ntdll.dll!__RtlUserThreadStart#8() Unknown
I'm guessing there's a problem with returning the codec pointer, but I'm new to C++ and have no idea how to correct it. I tried the cdecl, stdcall, and fastcall calling conventions -- none corrected the issue. I'm using the latest 32-bit DLL from Zeranoe. Any suggestions?
EDIT:
I've called other functions in the DLL and they work. For example, avformat_open_input works properly. I can pass parameters and the function returns a successful return value (0) and populates the format context structure. av_find_stream_info works as well. I still can't figure out why avcodec_find_decoder creates an access violation.

Finally, fixed it. I did two steps and I'm not sure which one worked (heh):
Added the '.lib' files as linker input dependencies
I picked up the Sept 7 build and made sure I have all the same build-date for the dll's, libraries, and include files.
Everything appears to work fine now.

Related

E0167 error : C++ argument of type * is incompatible with parameter of type "FILE**"

i am very new to C and while working on examples in the book, i keep getting "C++ argument of type * is incompatible with parameter of type **" Error. I am using visual studio 2019 C++. Here is where i get the error when i use fopen_s:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *inFile;
inFile = fopen_s("prices.dot","r"); /*Here is the line with an error*/
if (inFile == NULL)
{
printf("\nThis file does not existL");
printf("\nPlease make sure that this file currently exist");
exit(1);
}
printf("\nThe file has been succfully open for reading.");
return(0);
}
when i use "fopen" i get a warning which tells me to use fopen_s and when i do use fopen_s, i get the other error. I was wondering if i could get any help with this issue. Thank you!
If you google "MSDN fopen_s" and read the documentation on the Microsoft Developer Network, you will find that the function prototype is not identical to fopen():
errno_t fopen_s(
FILE** pFile,
const char *filename,
const char *mode
);
This means your code in that area would change to look like:
FILE *inFile;
errno_t errcode;
errcode = fopen_s(&inFile, "prices.dot","r");
if (errcode != 0) { /* do error handling, perhaps quit */ }
Alternately, you could continue to write old-style C code, and just use "fopen()" and at the top of your file, to shut MSVC compiler up, add the following #define:
#define _CRT_SECURE_NO_WARNINGS
Note, fopen_s() was added to the C 2011 standard, and is described in section K.3.5.2.1 -- meaning this is still portable C code on any modern C compiler.
Your book is probably older than 2011, and this function wasn't part of C at that time.

Application is unable to get command argument

I'm unable to get command line argument with int main(int argc, char* argv[]), No errors occurs but when further manipulation want to happen i see that argv[ ] and other related variables dont have any value and says Error reading characters of string then an Acces violation reading location error happens.
This is the sample code of my issue, I had to downsize it to make it readable:
#include "CommonHeaders.h"
void Start(char *input)
{
lstrcpyA(host, input);
// In this point i see in my Debugger "Locals/Autos" that nothing
// is passed to function then a "Access violation ... " happens.
// ...
}
int main(int argc, char *argv[])
{
Start(argv[1]);
return 0;
}
I always use this int main(int argc, char* argv[]) and pass command arg with ProjectProperties->Debugging->Command Argument and works perfect everytime. Is it possible that proper headers aren't included or any changes in project configuration could make a conflict?
Be sure to set the subsystem to console Linker -> SubSytem -> Console (/SUBSYSTEM:CONSOLE) and dont set an Entry point set the Whole Program Optimization to No Whole Program Optimization and turn SDL check to off.

cannot find -lC:\SQLAPI\lib\sqlapi.lib in omnet++ IDE

I am running my simple C++ program in OMNET ++ IDE
My code is as follows
**#include <stdio.h> // for printf
#include <string.h>
#include <SQLAPI.h> // main SQLAPI++ header
//#include <asaAPI.h>
int main(int argc, char* argv[])
{
SAConnection con;
con.setOption( "UseAPI" ) = "DB-Library";
con.setClient( SA_SQLServer_Client );
try
{
con.Connect(
"paper2"
"NADRA",
"",
SA_SQLServer_Client);
printf("We are connected!\n");
// Disconnect is optional
// autodisconnect will occur in destructor if needed
//con.Disconnect();
printf("We are disconnected!\n");
}
catch(SAException &x)
{
// SAConnection::Rollback()
// can also throw an exception
// (if a network error for example),
// we will be ready
try
{
// on error rollback changes
//con.Rollback();
}
catch(SAException &)
{
}
// print error message
printf("%s\n", (const char*)x.ErrText());
}
return 0;
}**
I have already linked all the files but the error that i am getting is as follow
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lC:\SQLAPI\lib\sqlapi.lib
collect2.exe: error: ld returned 1 exit status
Where as the file sqlapi.lib is in the same folder but linker is not able to find it. Can someone tells me about the issue that why compiler is not able to link it .I am using MINGW as a C++ compiler. The screen shot is attached by with the question about the linked filesenter image description here
If you are using -l, then it should be followed by the library name only, so something like:
-lsqlapi
If you want to specify a search path, then:
-lsqlapi -LC:\SQLAPI\lib\
(Usually the path is in Linux mode, so `C:/SQLAPI/lib though).
Then if this doesn't work, you can always force the library to be linked by just using it as another object:
C:/SQLAPI/lib/sqlapi.lib
Note though that gcc doesn't link against Visual Studio static libraries, which sqlapi might (because of the extension being .lib and not .a, but then this may be the export library for a dll).

First-chance exception (ntdll.dll)

Any ideas why I am getting this error?
This is the full error:
First-chance exception at 0x77202282 (ntdll.dll) in Test.exe 0xC0000005: Access violation writing location 0x0000004
Unhandle exception at 0x77202282 (ntdll.dll) in Test.exe 0xC0000005: Access violation writing location 0x0000004
Debugging breaks at the line Grid::Grid().
Root.hpp contains a private member static Grid grid;.
Root.cpp initializes this Grid Root::Grid;
I do have SFML linked statically, which has caused me a bit of trouble.
This is a small example which exhibits this error:
#include <fstream>
#include <sstream>
#include <Windows.h>
#include "Root.hpp"
Grid Root::grid;
int main (uchar argc, char **argv) {
Root root;
Root::Prepare(root);
return Root::GetStatus();
}
The constructor as follows:
Grid::Grid() {
Width = 100;
Height = 100;
}
The call stack:
ntdll.dll!772022b2() Unknown
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]
Test.exe!sf::priv::MutexImpl::lock(void) Unknown
Test.exe!sf::Mutex::lock(void) Unknown
Test.exe!sf::Lock::Lock(class sf::Mutex &) Unknown
Test.exe!sf::GlResource::GlResource(void) Unknown
Test.exe!sf::Texture::Texture(void) Unknown
> Test.exe!Grid::Grid() Line 5 C++
Test.exe!`dynamic initializer for 'Root::grid''() Line 11 C++
msvcr110d.dll!_initterm(void (void) * * pfbegin, void (void) * * pfend) Line 894 C
Test.exe!__tmainCRTStartup() Line 460 C
Test.exe!mainCRTStartup() Line 377 C
kernel32.dll!767533aa() Unknown
ntdll.dll!77219ef2() Unknown
ntdll.dll!77219ec5() Unknown
Even without having your full source code, the exception you're getting, and the stack you show, look suspiciously similar to another solved Stack Overflow posting:
"Unhandled exception at 0x777122D2 (ntdll.dll) in ArticxEngine.exe: 0xC0000005: Access violation writing location 0x00000004"
Your stack shows that Grid() is derived from SFML's Texture(), which is derived in turn from GlResource(). The problem is that your grid is a static variable which is getting constructed before GlResource()'s internal mutex is.
The solution is the same as the other Stack Overflow posting: make sure your Grid object is non-static and is constructed after your main() begins running.

Function Call Stack in C++

I have tried the following links, from StackOverflow and other sites,[I tried, but it didn't helped me, so i can't avoid duplicating]
StackWalk64 on Windows - Get symbol name
How do you make StackWalk64() work successfully on x64?
http://www.codeproject.com/KB/threads/StackWalker.aspx
http://jpassing.com/2008/03/12/walking-the-stack-of-the-current-thread/
How to Log Stack Frames with Windows x64
...
But none of the Code worked for me.I'm new to Windows C++ environment and i can't get any of the above code to work.
I'm looking for a call stack format like,
FUNCTION_NAME_DEPTH_1 : _LINE_NUM__
FUNCTION_NAME_DEPTH_1 : _LINE_NUM__
FUNCTION_NAME_DEPTH_1 : _LINE_NUM__ ...
Just function name and line numbers.
My Environment:
Visual Studio 2010
SDK : v7.1
Windows 7 Pro SP1
It would be a lot simple if anyone post a header file,[there seems to be few available,but not working] which we can include in our cpp file and print the call stack with a call like 'PrintFunctionCallStack();' . BTW in Linux/Mac, it was a whole lot easier,i was able to get the call stack from backtrace and it was so simple that i did it myself in few mins. In Windows i've have been trying past two days, but no surprise at all.
Linux/Mac Stack Trace Code, i haven't yet demangled the symbol names.
#ifndef _STACKTRACE_H_
#define _STACKTRACE_H_
#include <stdio.h>
#include <stdlib.h>
#include <execinfo.h>
#include <cxxabi.h>
#include <iostream>
static inline void PrintStackTrace()
{
cout<<"##############################################\n";
unsigned int maxStackCount = 63;
void* addressList[maxStackCount+1];
int addrLen = backtrace(addressList, sizeof(addressList) / sizeof(void*));
if (addrLen == 0) {
cout<<"Empty Stack, Probably Corrupted it seems ###\n";
return;
}
char** symbolList = backtrace_symbols(addressList, addrLen);
for (int i = 1; i < addrLen; i++) // Skipped First, 'i' begins with '1'
{
cout<<"###: "<<symbolList[i]<<":###\n";
}
free(symbolList);
cout<<"##############################################\n";
}
#endif
If your environment is Visual Studio, you can insert a Tracepoint and input
$CALLSTACK
in its edit box, after checking Print a message.
To do it, right-click on the line you want and select Breakpoint > Insert Breakpoint (or alternatively, insert a breakpoint clicking on the left of the editor line you want, then select When Hit).
Then you will see a detailed report in the Output window, having file name, line number and function name. It served me to successfully discovered some memory leaks.