Skip to main content
gamedev.net
🔒 Locked

Map loading problem

Started by RS232 3 replies 550+ views
Original Post
RS232
RS232
Hi, this is my first post. I hope this isn't at the wrong section, because I don't know whether it's an OpenGL problem or simply C. I am trying to create a very simple text based map loader. I have made it so that it should be able to load an undefined number of textures, but the problem is, whenever I try to load more than 4 textures, the loader segfaults. I have no clue why it does so. Anyway, here is the code, along with a sample map. kenogl_map.c kenogl_map.h map.txt kenogl.c kenogl.h Thanks ahead, RS232 [Edited by - RS232 on August 7, 2006 7:08:28 PM]
Samith
Samith
What's kgl_texture look like? And is there any reason why you're using malloc and free instead of new and delete? That and glBindTexture takes an unsigned int as the second parameter, not a kgl_texture. Other than that I can't really see what else might be wrong. Where exactly do you get the error?
RS232
RS232
Sorry about that, kgl_texture is an unsigned integer, I just typedef'd it to be able to identify it easier. I don't use new/delete because I write in C. I have trouble getting used to C++. Anyway, the program segfaults here, when it tries to load the final texture:

	if(map->textures > 0) {		for(i = 0; i < map->textures; i++) {			fscanf(pfile, "-%s\n", texture_path);			printf("%s\n", texture_path);			if((texture = kgl_loadimage(texture_path)) != NULL) {				map->texture = kgl_createtexture(texture);				SDL_FreeSurface(texture);			}		}	}

Edit: I should probably include the kenogl.c/kenogl.h-files too.
Samith
Samith
It looks like kgl_createtexture is freeing the SDL_Surface 'image', then, right after you're done calling kgl_createtexture you call SDL_FreeSurface with 'texture', which you just passed to kgl_createtexture and thus freed in doing so. 'texture' is no longer a valid pointer, and I have trouble seeing how your program would work with even one texture.

As a rule of thumb, try to keep memory allocation and unallocation within the same scope. It gives a sense of symmetry and prevents you from trying to use a pointer that was freed in a deeper scope.
RS232
RS232
Incredible! I have been trying to figure out this one since thursday. Thanks very much.

RS232

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.