OK - I have an interesting one here. I'm working on a tetris clone (basically to "level-up" my skills). I was trying to refactor my code to get it abstracted the way I wanted it. While it was working just fine before, now I get a segmentation fault before any images can be blitted. I've tried debugging it to no avail.
I have posted my SVN working copy of the project here.
It's just a small project and someone with more knowledge than me and a good debugger will probably figure it out in a snap. The only dependency is SDL. Kudos to the person that can tell me what I'm doing wrong.
Edit: As far as I can tell, what I have now and what I had before are logically the same, so I wouldn't think that what I have now would cause a segmentation fault. Just run an svn revert on the working copy, recompile and you can see that it was working...
Look at line 15 to 18 of Surface.cpp:
surface = SDL_DisplayFormatAlpha( tempSurface );
surface = tempSurface;
}
SDL_FreeSurface( tempSurface );
I assume it segfaults because when you use this surface later, you are actually operating on tempSurface because of this line:
surface = tempSurface;
and not the surface returned by SDL_DisplayFormatAlpha(). Since you free tempSurface, surface is now pointing to invalid memory. To fix, simply remove the second line in the else block.
I don't have SDL installed on my machine, but after looking through the code.
I noticed this in the Output.cpp file:
display = new Surface();
You do nothing. The constructor for this is empty. (surface is not initialized).
Then in Output::initalize() you do:
display->surface = SDL_SetVideoMode( 800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF );
This looks like the issue Surface::surface was never actually initialized. If you haven't found the solution, when i get home i'll digg into it.
As far as I understand, a segmentation fault happens when you are trying to mnaipulate a ponter which is no longer available, or you are trying to change a constant's value.
Related
I've got a weird problem that's suddenly appeared across all projects I'm working on. I'm using C++, SDL2 and OpenGL, and one of the first things that happens in my int main is to create an SDL window with an OpenGL flag like below:
int main( int argc, char* args[] )
{
//Minor stuff here e.g. initialising SDL
mainwindow = SDL_CreateWindow("...", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL);
}
For some reason this has started to cause a segmentation fault. If I change the flag from SDL_WINDOW_OPENGL to anything else, it does create a window but obviously fails shortly afterwards given the lack of an OpenGL context to do anything with. I've gone as far as to strip out all code except for the SDL and OpenGL initialisation stuff, and it still fails with a segfault error.
This issue has started as of today across two projects that share the same basic int main structure. This leads me to believe it's not a code issue (largely because the code hasn't actually changed), but that something with my setup / environment has gone wrong. So far I've tried the following to no avail:
Redownloaded latest SDL library
Redownloaded latest GLEW library
Reinstalled Codeblocks
Any ideas for a) what might be causing this and b) where I should start looking to fix it?
Thanks
Nathan
And like so many other problems in life, the answer turned out to be drivers. A system-wide update of some kind interfered with the graphics' ability to render any kind of OpenGL. A direct download and install of the latest graphic drivers fixed it.
I am making a shoot'em up game and have been coming into a problem where the sprites won't load at all after a certain amount of time and continuously shooting. In my console, an error prints out:
"IMG_LOAD_ERROR : Out of memory" (The "IMG_LOAD_ERROR : " was my own thing)
I also use text (SDL_ttf) to display an ammo count.
The hypothesis I have now is that I always call the IMG_load function in the constructor of every enemy that is spawned, but I'm not even sure if that's what's causing it. There are quite a few more possibilities I can think of.
Also, I do free the textures when they are destroyed (when the enemy is out of health or leaves the screen).
Can someone help me understand this and help me fix it?
It seems that you are not destroying textures/images or renderes that you are not need.
SDL_DestroyTexture(img);
SDL_DestroyRenderer(renderer);
Simple example of drawing image, which i found https://gist.github.com/armornick/3434362.
My advice: You should load all textures or begin of application or when is needed, but it must be once. You must reuse every same image.
It would be nice to see code to find out, what is wrong with code.
I have a simple problem however I'm not sure how it could be solved. I have the following code (c++):
Mat myImage;
for(;;){
imshow("Name", myImage);
}
Lets make the assumption that myImage is already populated with an image here. For some reason this very simple code is causing a memory leak. I read somewhere online that supplying imshow with the same image results in imshow adding it as a seperate axis, increase the memory comsumption.
Basically I dont know how to stop this memory leak. Any ideas?
Thanks.
Edit 1
Here is a screen shot of what I'm seeing when analysing memory usage.
Edit 2
Here is an example xcode project, made from scratch to display this issue. If you cannot run xcode, I've also provided a copy of the code on pastebin here. The project files can be downloaded from here.
I have, thanks to some help, managed to get the program below to compile and run but although it keeps on chugging away I cannot see anything drawn on the Pi's screen.
I don't think that it is a problem unique to the use of openvg and ajstarks code as, during the problem I had compiling the test progam, I tried a different way of writing images (sorry, all I remember was that it was low level and didn't need the includes for openvg). It took a bit of searching and re-writing to get it to compile and when it did the same thing happened.
I persevered for a while, but got no where. There were some references to some sort of limitation with Raspberry Pi and X Windows leading to the same problem. You draw something but it doesn't display. Given that there were several comments suggesting that openvg worked, I went back to that and (thanks to a guy called Ross) eventually worked out why I couldn't compile the code.
So now I am at a point where I can compile code that others have got to run successfully, but it doesn't draw anything on the screen. I know that the code runs - it chews CPU cycles (well the official demo does, mine less so although it's still definitly going) and the code can be quit with
Another method of working with graphics has come across the same no-output-display problem, so I think the problem is somewhere on my Pi but I have drawn a blank on how to address the X Windows (or it might have been X11, wish I had kept the tabs open!) not wanting to draw issue.
Any help greatly appreciated, thanks in advance!
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
extern "C" {
#include "VG/openvg.h"
#include "VG/vgu.h"
#include "fontinfo.h"
#include "shapes.h"
}
using namespace std;
int main (void) {
int width, height;
VGfloat w2, h2, w;
char s[3];
init(&width, &height); // Graphics initialization
w2 = (VGfloat)(width/2);
h2 = (VGfloat)(height/2);
w = (VGfloat)w;
Start(width, height); // Start the picture
Background(0, 0, 0); // Black background
Fill(44, 77, 232, 1); // Big blue marble
Circle(w2, 0, w); // The "world"
Fill(255, 255, 255, 1); // White text
TextMid(w2, h2, "hello, world", SerifTypeface, width/10); // Greetings
End(); // End the picture
fgets(s, 2, stdin); // Pause until RETURN]
finish(); // Graphics cleanup
exit(0);
}
Ok...
Thanks to a stroke of luck I have found the answer and it's odd. To me anyway...
In case anyone else encouters the problem, here is the (partial) solution which leads to another question to be posted shortly.
I am trying to run a programming club in my school and it's not practical to physically connect the Pi's to kb, mouse and monitor so they all auto-run VNC and we connect to the machines using Ultra-VNC. The programs are written in a shared directory and Eclipse C++ runs on the host; therefore all program output is viewed via VNC.
I had been continuing to try to solve the problem and at one point connected a keyboard and mouse and noticed that they seemed to be recognised (laser came on, Caps Lock toggled, etc.) but they didn't do anything when moved/typed on.
Eventually the penny began to teater on the edge as I got increasingly confused as to why no one else was having this problem. Is seemed odd that no one else had the issue and then I began to wonder more about the kb/mouse issue.
I tried plugging the HDMI output into a monitor at home (shool ones are still analogue d-sub!) and lo and behold, the physical kb and mouse worked. Then it got really strange!
Somehow I have 2 desktops running at the same time. The physical keyboard and mouse control one and VNC controls the other. Interestingly, VNC has the title Pi's X Desktop suggesting that the graphics problem might be something to do with X, but I am not sure for the reason below.
If I start a terminal window on 'Physical' desktop, it doesn't show up on 'VNC' desktop and vice versa - they seem to be independent, although that's not quite true.
When I run the test file on 'Physical' desktop, it works fine and can be controlled only using the physical kb. When I run it on 'VNC' desktop, it can be controlled only with the VNC kb but the output displays on the physical screen.
I really don't get this!
So, this answers the original question as the program does run on the Pi.
Off to post (hopefully a final) question on how to either get VNC to show the 'Physical' desktop or how to target the graphics output to the 'correct' desktop.
I need my window to be set to fullscreen (fs). No border, no interaction with anything behind this window, no start bar,etc. Just the program i'll be working in.
I'm guessing that the cursor not going outside of the window is not part of this, but i'll work on that later.
I've seen and tried the NeHe tutorial, but since it was wrote almost 10 years ago, i was wondering if a "less-line" way of putting a program fs exist.
"less-line" way of putting a program fs exist.
Use glut or SDL and initialize them in fullscreen mode. Can't get less lines than that.
Otherwise read this.