vtkArrayCalculator - Segmentation fault when accessing the output - c++

I want to use vtkArrayCalculator, for use in a Paraview filter, as described here: ArrayCalculatorExample
vtkSmartPointer<vtkArrayCalculator> calculator = vtkSmartPointer<vtkArrayCalculator>::New();
calculator->SetInputData(input);
calculator->AddScalarArrayName("u");
calculator->SetFunction("u+1");
calculator->SetResultArrayName("wind_velocity");
calculator->Update();
vtkSmartPointer<vtkFloatArray> windVelocity =
vtkFloatArray::SafeDownCast(calculator->GetStructuredGridOutput()->GetPointData()->GetArray("wind_velocity"));
Now when I want to access the data with (or similar commands)
windVelocity->GetValue(0);
I get a "Segmentation fault (core dumped)".
"input" is a vtkStructuredGrid and "u" is a vtkDataArray (that can be downcast to a vtkFloatArray without problem). "u" can be accessed by
input->GetPointData()->GetArray("u");
Every hint to what I am doing wrong is greatly appreciated!
Edit: I already tried the following
vtkSmartPointer<vtkFloatArray> windVelocity = vtkSmartPointer<vtkFloatArray>::New();
windVelocity->DeepCopy(vtkFloatArray::SafeDownCast(calculator->GetStructuredGridOutput()->GetPointData()->GetArray("wind_velocity")));

I'd suggest to split up the long chain of vtkFloatArray::SafeDownCast(calculator->GetStructuredGridOutput()->GetPointData()->GetArray("wind_velocity")) and use a debugger to see what the intermediate results are.
When reading the definition of GetArray, it states that under various conditions the function might return NULL. Check the return value of GetArray; it is very likely that you do not get back what you expect.
vtkDataArray* vtkFieldData::GetArray ( const char * arrayName )
inline
Not recommended for use. Use GetAbstractArray(const char *arrayName) instead.
Return the array with the name given. Returns NULL if array not found.
A NULL is also returned if the array with the given name is not a
vtkDataArray. To access vtkStringArray, vtkUnicodeStringArray, or
vtkVariantArray, use GetAbstractArray(const char *arrayName).

Related

Machine ID Code in C++

I tried this because I want to learn how to grab a machine unique ID.
I need it because I'm gonna code a program that reads the machineid and compares it to a specific one.
I edited the code(in the end, the code that prints the Machine ID) to this:
string MachineID;
MachineID = vtProp.bstrVal;
if (MachineID == vtProp.bstr)
but it only gives me this error.
How would I make my HWID variable be set to the value of vtProp.bstrVal, then compare it with vtProp.bstrVal? I've googled for some hour now, can't seem to get it fixed.
Thanks in advance!
Simple syntax error - you've used =, which is assignment, instead of ==, which is comparison.
Because c++ accepts nonboolean types in if statements, the code if(variable = value) is acceptable to the compiler, which tries to assign MachineID the value of vtProp.bstr, which causes your error due to type mismatch.

Unhandled exception VS2010 C++

I am trying to make a socket on windows to connect to a server.
I am using the code from msdn's website, the winsock client code.
(Link: msdn.microsoft.com/en-us/library/windows/desktop/ms737591(v=vs.100).aspx )
In any case, when I try debugging said code I get the error message:
Unhandled exception at 0x58a714cf (msvcr100d.dll) in Application.exe: 0xC0000005: Access violation reading location 0x00000032.
It asks me if I want to break or continue, if I continue the same error message simply keeps popping up. If I press break it takes me to line 1643 in the file output.c .
Simply put, I have no idea about what to do to make it work, and I'd appreciate any help given.
EDIT:
A reply to all comments given thus far:
The surrounding relevant code in output.c is:
if (flags & (FL_LONG|FL_WIDECHAR)) {
if (text.wz == NULL) /* NULL passed, use special string */
text.wz = __wnullstring;
bufferiswide = 1;
pwch = text.wz;
while ( i-- && *pwch )
++pwch;
textlen = (int)(pwch - text.wz);
/* textlen now contains length in wide chars */
} else {
if (text.sz == NULL) /* NULL passed, use special string */
text.sz = __nullstring;
p = text.sz;
while (i-- && *p) //THIS IS WHERE IT BREAKS
++p;
textlen = (int)(p - text.sz); /* length of the string */
}
This is not code that I have written but innate code that already exists.
EDIT NR 2:
This is a printscreen displaying my call stack.
I do not have 10 reputation so I cannot show the image, so here is a link to the image:
http://tinypic.com/r/5n6ww9/5
On it you can see my call stack
The file output.c has the code that handles the printf family of functions.
The fact that you have an error here is probably due to a malformed printf function call in your code. Maybe you have specified an invalid print format or have not provided enough arguments.
When your program crashes, click Break and look at the call stack in the debugging windows to see where - in your code - the function is called, and with what arguments.
I suspect you are trying to print a NULL string or something. When you have found the printf call (if that's what it is), edit your question to show that section of source code and/or use the debugger to examine the variables used a arguments to the function and make sure they are all correct.
Without seeing the code that you've written, at the location of the crash, it's not possible to give a more precise answer.
Or you may insert a null string to CString.format like below:
CString str;
str.format("%s"); //A null string
where it should be
str.format("%s",mes);
where mes is char mes[20] for example
or any other variable
So the point is there may be some mistake in CString.format or Printf
Good Luck.

Is there a bool for inNull(sqlite3_stmt *, int col) to allow me to check for it?

In my database, there are a lot of options where an entry ISNT required at start, so it is nullable. I was looking through the C++ api for SQLite3 and i didnt find anything from first glance ( http://www.sqlite.org/c3ref/funclist.html) as to how to handle this. The only thing i came across was what is returned when you request a column and the column is null (shown here: http://www.sqlite.org/c3ref/column_blob.html).
The thing is that, might not be throing errors because 0 is a valid solution for a lot of common things (As a programmer we usually start counts at 0).
What i want to do is find a boolean statement if there is one, so that way i can check if null before extracting the data. That way, a proper flag can be returned instead of 0.
Anyone else have issues here?
You can always check the type of the column before getting the data (which will do a conversion). You do it with sqlite3_column_type, which returns SQLITE_NULL for a NULL value:
bool isNull(sqlite3_stmt *stmt, int iCol)
{
return sqlite3_column_type(stmt, iCol) == SQLITE_NULL;
}

IMAGE_EXPORT_DIRECTORY wrong values

I'm having trouble with IMAGE_EXPORT_DIRECTORY values, whenever I'm calculating address of it, it gives me wrong values (like Name, NumberOfNames, AddressOfNames, etc..).
I'm calculating it by the following code.
IMAGE_EXPORT_DIRECTORY *export_directory = (IMAGE_EXPORT_DIRECTORY*)(nt_headers->OptionalHeader.DataDirectory[0].VirtualAddress + (DWORD)dos_header);
Following in debugger, the address is right, there must be a IMAGE_EXPORT_DIRECTORY, but why it gives me wrong results? Can't access any of the elements from it, because it says bad ptr..
Tried and works fine with kernel32.dll, but fails on user32.dll when extracting names from AddressOfNames.
Using Windows 8 x86.

AfxGetAppName() returns garbage characters

I have the following line of code in my application:
CString strAppName = AfxGetAppName();
Sometimes it fills strAppName up with garbage characters, and I can't figure out why.
Anyone have any ideas?
TIA.
That is possible if you change m_pszAppName manually.
At the very beginning of application initialization, AfxWinInit calls CWinApp::SetCurrentHandles, which caches the current value of the m_pszAppName pointer as follows:
pModuleState->m_lpszCurrentAppName = m_pszAppName;
That is, the module state struct holds a copy of the m_pszAppName pointer. If you change m_pszAppName in InitInstance as adviced in MSDN, you still have the old pointer value in pModuleState->m_lpszCurrentAppName. The AfxGetAppName() function returns AfxGetModuleState()->m_lpszCurrentAppName.
You could add data breakpoint on m_lpszCurrentAppName and on m_pszAppName. It is initialized in <...>\Microsoft Visual Studio 9.0\VC\atlmfc\src\mfc\appinit.cpp file. You'll see what is going on with that variable and who's trying to change it.