Exection at cvQueryFrame in visual studio 2010 - visual-studio-2010

I'm going to run this really simple cod that will be compiled in visual studio and opencv libraries are used in.
#include <opencv2/opencv.hpp>
int main()
{
CvCapture *capture=cvCaptureFromFile("sample_1.avi");
IplImage *FirstFrame=cvQueryFrame(capture);
cvShowImage("first",FirstFrame);
cvWaitKey();
}
The compile process is OK But when debugger reaches
IplImage *FirstFrame=cvQueryFrame(capture);
the following exception is ocurred :
Unhandled exception at 0x715f6a7e in VideoTest.exe: 0xC0000005: Access violation reading location 0x01bc4000.
How can I fix this problem ?
Thanks!

cvQueryFrame() crashes because cvCaptureFromFile() is probably failing. That happens when it can't open/find the file or when OpenCV doesn't support the container/codec of the video.
Whenever a function returns something is good practice to test the validity of what was returned, in this case since it's a pointer you should do:
CvCapture *capture=cvCaptureFromFile("sample_1.avi");
if (!capture) // same as: if (capture == NULL)
{
// print error message and abort execution
}

Related

Hitting exception when using Google Protobuf Any UnpackTo function in C++

google::protobuf::Any anyResponse = someResponse.response();
ResponseType unpackResp; //ResponseType is a subclass of google::protobuf::Message
if (anyResponse.UnpackTo(&unpackResp))
{
...
}
Running this piece of C++ code and access vialotion exception happens in anyResponse.UnpackTo(&unpackResp). Does someone know how to debug into this function? I checked anyResponse and it looks good.
It went through these files in google::protobuf:
call stack
Anyway I can see these files?

C++/CLI: Access violation when debugging/stepping into code in 32-bit (VS-2015)

I got this strange issue when stepping into code when debugging a 32-bit mixed mode assembly. The stripped down version of the code looks like this:
public ref class FooClass {
public:
FooClass();
};
FooClass::FooClass(){
// Note: doesn't matter what code is in here, as long as it is native
char test[10];
memset((void*)test, 0, sizeof(test));
}
This class is then instantiated in another class:
FooClass^ BarClass::Test() {
FooClass^ addr = gcnew FooClass();
return addr;
}
..which again is instantiated in a C# console app:
class Program
{
static void Main(string[] args)
{
BarClass bar = new BarClass();
FooClass foo = bar.Test();
}
}
When stepping through the code, and into the FooClass constructor, I get an exception
(note: removed argument info for the sake of less mess):
ntdll.dll!_NtTraceEvent#16() Unknown
ntdll.dll!EtwpEventWriteFull() Unknown
ntdll.dll!_EtwEventWrite#20() Unknown
clrjit.dll!Compiler::lvaInitTypeRef() Line 253 C++
clrjit.dll!Compiler::compCompileHelper(...) Line 3489 C++
clrjit.dll!Compiler::compCompile(...) Line 3092 C++
clrjit.dll!jitNativeCode(...) Line 4063 C++
clrjit.dll!CILJit::compileMethod(...) Line 180 C++
[Managed to Native Transition]
> FooBar.dll!FooBar::BarClass::Test() Line 16 C++
ConsoleApp.exe!ConsoleApp.Program.Main(string[] args) Line 15 C#
However, if I just add breakpoints in the constructors and just run to next breakpoint, the code runs fine.
Also, when removing native code, it runs fine.
This issue does not occur in 64-bit mode. I crosschecked for settings, but can't really see anything special.
There are no 3rd party dll's, all the native code is compiled into the assembly.
This is not my first C++/CLI project, but first time I do it in VS2015.
From the comments in my original question, the following was suggested:
Enter Tools -> Options -> Debugging -> General
Enable the "Use Managed Compatibility Mode" checkbox
This fixed the issue.
In my case the issue was an exception in an internal managed component, that apparently did not propagate well to the native layer that invoked it - and manifested as exactly this impossible ETW stack.
To debug I had to attach as managed-only. The exception was staring me right in the face (missing DLL, but that's irrelevant).
Hope this helps someone out there.

When running a Unity HoloLens program from Visual Studio, when an exception is thrown how do I get the line numbers in the stack trace?

Currently when an exception is thrown from within my Unity script while using my HoloLens the Debug Output in Visual Studio shows the stack trace without the line numbers.
How do I get the line numbers along with the stack trace? I'd be fine with it being logged somewhere else other than the Debug Output.
Here's some example output in Visual Studio:
Exception thrown: 'System.NullReferenceException' in Assembly-CSharp.dll
NullReferenceException: Object reference not set to an instance of an object.
at NewBehaviourScript.Update()
at NewBehaviourScript.$Invoke6Update(Int64 instance, Int64* args)
at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method)
(Filename: <Unknown> Line: 0)
And the corresponding Unity script (I made a Cube and attached a NewBehaviourScript component):
public class NewBehaviourScript : MonoBehaviour {
// Update is called once per frame
void Update ()
{
object a = null;
a.GetType();
}
}
I tried changing the build from Release to Debug doesn't give the line numbers.
I tried googling, and it looks like it's not showing the line numbers for others, as well: http://answers.unity3d.com/questions/1315985/null-reference-in-line-0.html
I tried asking on Microsoft's forums, but didn't receive any useful replies.
I don't think you would get the line number since it does not exist anymore. You get it in Unity editor because you are not running a full build of the application so Unity still has access to the non-compiled code. When you run on the device, it sends debug commands to the VS console about the printing and the errors but all the code is binary at that point, so there is no reason nor possibility to provide a line number.
Actually this is not specific to Hololens, but you would get the same in Android or iOS. Once build, the code is no longer the same, it does not even match one to one as the compiler performs optimizations.
What you can do is placed Debug commands to see where it happens.
public class NewBehaviourScript : MonoBehaviour {
// Update is called once per frame
void Update ()
{
object a = null;
#if DEBUG
if(a == null)
{
Debug.Log("[NewBehaviourScript] Running update with null a object");
}
#endif
a.GetType();
Debug.Log("[NewBeahviourScript] if this line prints, method did not crash");
}
}
In this example, you can use the DEBUG macros if you would have code running only for debug purpose. This way you can easily exclude it on export. The second call for Debug is not required in the macro since the build process will discard it when you set the build to Release or Master.

MissingMetadataException when building UWP app with .Net native

I have this UWP app that uses a project (UWP class library) which itself uses EF7 and SQLite.
I tried to build the app in the Release mode using .Net native tool chain, the build completes successfully (after a good long time, and after eating as much memory as it can), but the application crashes just after leaving the splash screen.
After following some advice on SO I tried the .Net native with Debug mode, the build finishes just like in the Release mode, but I get many errors on the output window and it is the same scenario as this one UWP - .NET Native tool chain compilation error
I followed #Matt Whilden advice, and I got rid of those errors, then tried again.
This time I got hit by this famous MissingMetadataException :
The output window shows this :
Exception thrown: 'System.AggregateException' in System.Private.Threading.dll
Exception thrown: 'System.ArgumentException' in System.Linq.Expressions.dll
Exception thrown: 'System.ArgumentException' in System.Linq.Expressions.dll
Exception thrown: 'System.ArgumentException' in System.Linq.Expressions.dll
The thread 0x2a30 has exited with code 0 (0x0).
Exception thrown: 'System.Reflection.MissingMetadataException' in System.Private.Reflection.Core.dll
Additional information: 'Microsoft.Extensions.Caching.Memory.MemoryCacheOptions' is missing
metadata. For more information, please visit
http://go.microsoft.com/fwlink/?LinkID=392859
I tried to follow my code, during execution and I found out that it is caused by the first ever call to a DbSet table from my DbContext
public long GetLastTimeStamp()
{
//-----> Here is the line causing the error
var sortedArticles = DbContext.Articles.OrderByDescending(article => article.ArticlePubDate).ToList();
if (sortedArticles != null && sortedArticles.Count != 0)
{
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local);
TimeSpan elapsedTime = sortedArticles.First().ArticlePubDate - epoch;
return (long)elapsedTime.TotalSeconds;
}
else
{
return 0;
}
}
This method above is called inside an Async method, just to know.
I tried, desperately, to call .ToList() by doing :
var sortedArticles = DbContext.Articles.ToList().OrderByDescending(article => article.ArticlePubDate).ToList();
But still get the same error.
This is really frustrating, I don't know how to solve this problem, not sure what and how I should change the Default.rd.xml, any one can help telling me how to achieve this build correctly ?
Please try to add type 'Microsoft.Extensions.Caching.Memory.MemoryCacheOptions' in Default.rd.xml (already present in your project).
cf. https://blogs.msdn.microsoft.com/dotnet/2014/05/21/net-native-deep-dive-help-i-hit-a-missingmetadataexception/
For example:
<?xml version="1.0" encoding="utf-8"?>
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Application>
<Type Name="Microsoft.Extensions.Caching.Memory.MemoryCacheOptions" Dynamic="Required All" />
</Application>
</Directives>

DirectX Windows 8.1 fstream won't load file

I'm building a Windows 8.1 DirectX app and trying to load in an external to store level data.
The relevant code currently is (this method is called in the AssetHandler constructor):
void AssetHandler::LoadLevelsData()
{
unsigned int i = 0;
std::string lineData;
this->currentFile.open("Assets/reg.txt");
//Below statement here purely to check if the blasted thing is opening
if (this->currentFile.is_open())
{
i++;
}
while (std::getline(this->currentFile, lineData))
{
levels[i] = lineData;
i++;
}
currentFile.close();
}
The problem that i'm having is that the file does not appear to be opening. I have tried:
Using a full path
Opening the file in the initialisation list
A breakpoint shows that it is jumping over the if and while
I found some information saying that DirectX has constraints on working with external files but it did not specify exactly what these were.
The Item Type was set to 'Does not participate in build'. Setting this value to 'Text' solved the problem.

Resources