Addictgamer Posted November 11, 2009 Share Posted November 11, 2009 This code keeps returning an access violation during runtime. void text_object::set_txt(SDL_Surface *_text, char _text_to_display[1000]) { //load the correct text into the text surface _text= TTF_RenderText_Solid( font, _text_to_display, allstars_color ); } Obviously i am using sdl, and sdl_ttf. For some reason, doesn't occur when i put it in the main function, minus the calls and such. In fact, sdl doesn't let me load anything anywhere other than the main function in every sdl app i make, except for one that i somehow fixed. What am i doing wrong? Link to comment Share on other sites More sharing options...
Cyrem Posted November 11, 2009 Share Posted November 11, 2009 A segmentation fault (often shortened to segfault) is a particular error condition that can occur during the operation of computer software. A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system). Segmentation is one approach to memory management and protection in the operating system. It has been superseded by paging for most purposes, but much of the terminology of segmentation is still used, "segmentation fault" being an example. Some operating systems still have segmentation at some logical level although paging is used as the main memory management policy. On Unix-like operating systems, a process that accesses an invalid memory address receives the SIGSEGV signal. On Microsoft Windows, a process that accesses invalid memory receives the STATUS_ACCESS_VIOLATION exception. A few causes of segmentation fault can be summarized as follows, 1. attempt to execute a program that does not compile correctly. Note that most compilers will not output a binary given a compile-time error. 2. buffer overruns 3. using uninitialized pointers 4. dereference NULL pointers 5. attempt to address memory the program does not own. 6. failure to check for data validity before using it. 7. failure to check for file open success before attempting to use the file pointer. Generally, segmentation faults occur because: a pointer is either NULL, or points to random memory (probably never initialized to anything), or points to memory that was deleted. I think that answers the possible causes well. Link to comment Share on other sites More sharing options...
Addictgamer Posted November 11, 2009 Author Share Posted November 11, 2009 Problem is i tried google already, all the way until around page result 50. Nothing worked. Microsoft.com didn't help either. I had figured out from google that it was not accessing correctly and such... Now, what exactly is the fix? Using _text = *TTF_RenderText_Solid( font, _text_to_display, allstars_color ); may fix it? As that is what i understand from your quotes. (no, i had not checked Wikipedia when i was searching for solutions.) And yes, i had tried something similar to this before. Link to comment Share on other sites More sharing options...
Anonymouse Posted November 11, 2009 Share Posted November 11, 2009 Maybe try using a reference rather than a pointer: void text_object::set_txt(SDL_Surface &_text, char _text_to_display[1000]) { //load the correct text into the text surface _text= TTF_RenderText_Solid( font, _text_to_display, allstars_color ); } As opposed to: void text_object::set_txt(SDL_Surface *_text, char _text_to_display[1000]) { //load the correct text into the text surface _text= TTF_RenderText_Solid( font, _text_to_display, allstars_color ); } If that doesn't work, try adding another & like so: void text_object::set_txt(SDL_Surface &_text, char _text_to_display[1000]) { //load the correct text into the text surface &_text= TTF_RenderText_Solid( font, _text_to_display, allstars_color ); } Link to comment Share on other sites More sharing options...
Addictgamer Posted November 11, 2009 Author Share Posted November 11, 2009 I haven't tried that, i'll give it a go. error: 'text_object::set_txt' : cannot convert parameter 1 from 'SDL_Surface *' to 'SDL_Surface &' Thats what i get both times. Alright... Here's a pic during runtime btw, using the same code i ran at the beggining post //load the correct text into the text surface _text= TTF_RenderText_Solid(font_allstars, _text_to_display, allstars_color); Fixed it. It turns out that font was out of the scope, so it was loading nothing. Nvm, the text doesn't display at all anymore. But at least i get no errors. Link to comment Share on other sites More sharing options...
sciguy Posted January 11, 2010 Share Posted January 11, 2010 Hm, wierd. Have you tried using a different font? Or a different size? Link to comment Share on other sites More sharing options...
Addictgamer Posted January 11, 2010 Author Share Posted January 11, 2010 Tried eveyrthing. IDK why it always does this to me, and only when I do it outside of main.cpp and main.h Link to comment Share on other sites More sharing options...
sciguy Posted January 14, 2010 Share Posted January 14, 2010 Hm, the files could be ina n area of the hard drive where access is not authorized for the prog, or there could be a problem with the call. Link to comment Share on other sites More sharing options...
Anonymouse Posted January 14, 2010 Share Posted January 14, 2010 Hm, the files could be ina n area of the hard drive where access is not authorized for the prog, or there could be a problem with the call. Memory, not drive. That is, RAM. But yes, that's the problem. Link to comment Share on other sites More sharing options...
sciguy Posted January 14, 2010 Share Posted January 14, 2010 Ram is only used for information that is being accessed, but it would make sense that only certain areas are used Link to comment Share on other sites More sharing options...
Addictgamer Posted January 14, 2010 Author Share Posted January 14, 2010 Why does it happen on EVERY pc I run, even on linux and xp, and using different compilers and different settings? It is something with how I program it. THERE ARE NO BOOKS OR TUTORIALS THAT MAKE A REAL GAME IN SDL THAT USES MULTIPLE FILES FOR VERY GOOD ORGANIZATION. They just say, "do this or do that" Or they give an example, but still crunch many stuff into a small file. It just drives me crazy. Id anyone can help... I would like a simple, real game, example that is very OO, and uses separate files and classes and functions, etc... As if it was in a real, complex game. Or maybe a tutorial or book that does these. Link to comment Share on other sites More sharing options...
Addictgamer Posted January 17, 2010 Author Share Posted January 17, 2010 Maybe using the this operator may fix the problem... Link to comment Share on other sites More sharing options...
Recommended Posts