GDALDriver::Create GTiff Segmentation Fault - c++

I am new to using GDAL, and am having trouble creating a GDALDataset using the GDALDriver::Create() method. In this case, I am using C++. Ultimately I am trying to create a raster and write values from an array to the raster. A minimal working example of the code that generates the problem of creating a dataset is given here:
#include <iostream>
#include </usr/include/gdal/gdal_priv.h>
int main(){
std::cout << "starting GDAL business\n";
const char *raster_format = "GTiff";
GDALDriver *g_driver;
g_driver = GetGDALDriverManager()->GetDriverByName(raster_format);
GDALDataset *g_dataset;
const char *test_file = "test_file.tif";
char **raster_creation_options = NULL;
std::cout << "raster options created\n";
g_dataset = g_driver->Create(test_file,
100,200,1,GDT_Float32,
raster_creation_options); //<--- seg faults
std::cout << "dataset created\n";
}
The resulting console looks like:
starting GDAL business
raster options created
Segmentation fault (core dumped)
I am just following the basic API tutorial (Link), but am encountering this problem. I am on Ubuntu 14.04, using the repository's libgdal packages.
Can anyone shed some light on this problem?

I know this is an old question and the issue has probably long since been resolved, but I figured I would throw my answer into the ring. After all, I was directed here when I had this problem and it seems like the question has a fair amount of views.
If your code is not working, and you are having trouble loading the driver, I would suggest adding the line GDALAllRegister();. I was following the same tutorial as you, and they mention adding this line at the top of the tutorial, but if you're like my and rushed right to the part you needed, the Create part of the tutorial, you probably overlooked that step.
Hopefully this answer helps someone, if not the original author of the question.

Related

How do I graph data in c++?

I am trying to make a graph using graphics.h in c++. I was following a tutorial on youtube. It seems that either due to the age of the video (perhaps the syntax has changed slightly?) or a problem on my end; I cannot even get a separate window for my graph to open. I am in completely uncharted waters for me as the limit of my coding knowledge is what you would expect to learn from a first-semester coding class. I am using DEV C++ and am compiling using "TDM-GCC 4.9.2 32-bit Release" (because the 64 bit release gives me an error in "Makefile.win" that scares me) and my program exits with a return value of 3221225477. What am i doing wrong?
#include"graphics.h"
#include<math.h>
#include<conio.h>
#include<iostream>
using namespace std;
int main() {
initwindow(800,600);
int x,y;
line(0,300,getmaxx(),300);
line(400,0,400,getmaxy());
float pi=3.14159;
for(int i=-360;i<=360;i++){
x=(int)400+i;
y=(int)300-sin(i*pi/100)*25;
putpixel(x,y,WHITE);
}
getch();
closegraph();
return 0;
}
According to your issue and exit-code, the return value in hex is 0xC0000005 or STATUS_ACCESS_VIOLATION. But most developers didn't even bother to learn out-dated legacy API and I can not help you to find the exact line (use debugger, it shows you the exact line, still not the reason).
But to answer your question in the title, well, according to what free framework one uses (Qt or XWidget), the method differs, for Qt (which I would recommend) simply override paint-event and use QPainter renderer to show your QPath data.
Don't reinvent the wheel (or render-system in this case), your course and/or book may soon introduce you to one of the mentioned frameworks.

String, debugging, and Segmentation Error goes hand in hand

Hi I am doing this simple program
#include <iostream>
#include <string>
using namespace std;
int main (){
string hi("Hi how are you");
for(int i = 0;i<4;i++)
cout<<hi<<endl;
return 0;
}
When I compiled it and run it, there were no issues, but when I try to debug it, every time the IDE program (Code::Block 16.01) steps in or out of string hi("hi how are you") it gives me a Segmentation Fault.
I know SF is when the program tries to access a space of memory that it is not supposed to access, and I know string class is a C-Sytle string that allocates memory dynamically, and deletes them automatically when the program is finished, hence there should be no issue in memory management, so this code shouldn't be a problem.
BUT in this code I don't understand why do I get a SF when I debug it. When I tried debugging it for the first time and stepped out of hi, there were no errors, but when I tried watching hi, it gave me a SF, and when I try to debug it again, and I steped into string hi I get S.F.
Screenshot of the error FYI
When I was search information about this issue I found entry in Code::Blocks forum, but it is quite old.
However, there is possibility of bug in GDB for MiniGW. If you want to be sure, you should look for this issue and its fix.
I know that this isn't full answer, but you should go to this posts and read they, there is some solution:
Code::Blocks forum's posts:
1. Watching std::string in debugger causes segfault?!?
2. Still having seg fault while watching a string....

Eclipse C++ "#include" and "using namespace" errors

My aunt gave me a book about c++ (for beginners). It's nice and so I wanted to test one of those code samples. But I am just getting errors. I didn't find anything that could help me on Google or so.
I am using Eclipse Mars.1 C++ and MinGW.
Code:
/*
* Erstes_Programm.cpp
*
* Created on: 26.12.2015
* Author: Luca
*/
// Erstes Programm
#include <iostream>
using namespace std;
int main() {
cout << "It's just a test!" << endl;
return 0;
}
I am getting those errors:
Using namespace:
Description Resource Path Location Type
expected ';' before ':' token
Erstes_Programm.cpp /Programmieren C++ (Einführung)
line 10
C/C++ Problem
AND
Description Resource Path Location Type
expected unqualified-id before ':' token
Erstes_Programm.cpp /Programmieren C++ (Einführung)
line 10
C/C++ Problem
Hope somebody can help me?
Here is a screenshot:
EDIT: PROBLEM SOLVED!! I don't know WHAT I did, but it's solved I'm not getting any errors and everything works perfectly. Thanks for everybody who wanted to help
The first line in the screenshot doesn't make any sense. Remove it. The rest looks fine. And in the future, post text, not screenshots.
Instead work on X-code rather then eclipse for C++. Visual studio is most recommend.
For Your problem, I think you got to play with the library, something must not be supported, other then that your code look fine.

How to work with Dev C++

I am new in programming. but i know turbo C & i can work with it. But how to work on Dev C++?
i means 1.How to Compile & 2. How to Run. I have Windows so if anyone help me with proper example than it will be great.
Dev c++ is very simple! To do the basic, Open you file, compile and run using the menu Execute!
However, if u are starting something new now, I would suggest you consider using eclipse. It is not so easy in the first days but the long term gain is giant!
I would like to improve my answer... I suggested that you should consider using eclipse. I still think that eclipse is great. Furthermore, I had had some problems with the old version of dev c++. After reading the comment of #Orwell (which provided facts that I didn't know), I checked the new version and the issues that I had had before disappeared. So, the big picture of dev c++ is that it is a program that works well and it is very simple. There is a cost to start using eclipse!
Example:
#include <iostream>
using namespace std;
int fibonacci(int n){
if(n==1)return 0;
if(n==2)return 1;
if(n>=3)return fibonacci(n-1)+fibonacci(n-2);
}
int main(){
//
int n =10;
cout << "The element of the Fibonacci series is " << fibonacci(n) << "\n";
return 0;
}

Segmentation fault when different input is given

I do some image processing work in C++. For this i use CImg.h library which i feel is good for my work.
Here is small piece of code written by me which just reads an image and displays it.
#include "../CImg.h"
#include "iostream"
using namespace std;
using namespace cimg_library;
int main(int argc,char**argv)
{
CImg<unsigned char> img(argv[1]);
img.display();
return 0;
}
When i give lena.pgm as input this code it displays the image. Where as if i give some other image, for example ddnl.pgm which i present in the same directory i get "Segmentation Fault".
When i ran the code using gdb i get the output as follows:
Program received signal SIGSEGV, Segmentation fault.
0x009823a3 in strlen () from /lib/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.9-2.i686 libX11-1.1.4-5.fc10.i386 libXau-1.0.4-1.fc10.i386 libXdmcp-1.0.2-6.fc10.i386 libgcc-4.3.2-7.i386 libstdc++-4.3.2-7.i386 libxcb-1.1.91-5.fc10.i386
Can some one please tell me what the problem is? and how to solve it.
Thank you all
Segfault comes when you are trying to access memrory which you are not allowed to access.
So please check that out in the code.
The code itself looks just fine. I can suggest some ways to go ahead with debugging -
Try removing the display() call. Does the problem still occur? (I'd assume it does).
Try finding out where in the CImg code is the strlen() that causes the segmentation fault (by using a debugger). This may give additional hints.
If it is in the PGM file processing, maybe the provided PGM file is invalid in some way, and the library doesn't do error detection - try opening it in some other viewer, and saving it again (as PGM). If the new one works, comparing the two may reveal something.
Once you have more information, more can be said.
EDIT -
Looking at the extra information you provided, and consulting the code itself, it appears that CImg is failing when trying to check what kind of file you are opening.
The relevant line of code is -
if (!cimg::strcmp(ftype,"pnm")) load_pnm(filename);
This is the first time 'ftype' is used, which brings me to the conclusion that it has an invalid value.
'ftype' is being given a value just a few lines above -
const char *const ftype = cimg::file_type(0,filename);
The file_type() function itself tries to guess what file to open based on its header, probably because opening it based on the extension - failed. There is only one sane way for it to return an invalid value, which would later cause strcmp() to fail - when it fails to identify the file as anything it is familiar with, it returns NULL (0, actually).
So, I reiterate my suggestion that you try to verify that this is indeed a valid file. I can't point you at any tools that are capable of opening/saving PGM files, but I'm guessing a simple Google search would help. Try to open the file and re-save it as PGM.
Another "fun to track down" cause of segmentation faults is compilier mismatches between libraries - this is especially prevalent when using C++ libraries.
Things to check are:
Are you compiling with the same compiler as was used to compile the CImg library?
Are you using the same compiler flags?
Were there any defines that were set when compiling the library that you're not setting now?
Each of these has bitten me in subtle ways before.