data types

if in a console program i were to declare a int and closed the program with return 0, everything is fine but when it comes to SDL_Surface. u need to clean the memory before closing the program. why? arent both of them just data types? what is the difference?
EDIT: maybe i m wrong about sdl_surface being a data type.

EDIT: well i have found out it is a structure. but my questin still stands. why SDL_Surface types has to be cleaned before closing
Last edited on
Well (AFAIK) if you are declaring a pointer to either you would need to clean up when you are done with it.

AFAIK the following is true:
1
2
3
4
5
int main() {
	int* i= new int();	//memory leak as it is pointing to an int on the heap that has not been desturcted
	SDL_Surface s();	//no leak as the destructor should be called at the end of the scope
	return(0);
}// SDL_Surface destructor called and memory from it is free 


EDIT: I would look into smart pointers, as they generally solve most memory leaks
Last edited on
SDL_Surface may allocate system resources that are not automatically reclaimed by the system on program exit. Or the kernel may not be able to effectively restore the system to the state before the program began. The OS kernel manages many of the resources that an application uses, but not necessarily all of them. For those that it manages, the kernel can automatically reclaim the resources. Those that it cannot reclaim and re-initialize must be managed by the application. Also, not all resources are application-specific resources. An example of this are SysV IPC resources such as shared memory or semaphores. These are designed to be shared across multiple applications and must be cooperatively allocated and freed.
@Script Coder: Isn't SDL a C library, meaning no destructors?
@Script Coder:

SDL_Surface s(); //no leak as the destructor should be called at the end of the scope

That's actually no leak because it's a function prototype and not actually a SDL_Surface ;P


@ Ceset:

if in a console program i were to declare a int and closed the program with return 0, everything is fine but when it comes to SDL_Surface. u need to clean the memory before closing the program. why? arent both of them just data types? what is the difference?


You do not need to clean up the SDL_Surface. You need to clean up the SDL_CreateSurface function call. There's a subtle difference.

The actual surface pointer you're using is automatically cleaned up for you. However the data it points to is not because it's created dynamically by the above mentioned function.

So your comparison of int<->SDL_Surface is correct... neither needs to be cleaned up.

But what does need to be cleaned up is SDL_CreateSurface<->SDL_DestroySurface


(note I might have the function names wrong... it's been forever since I've looked at SDL)
Last edited on
@LB and Disch Sorry for my ignorance, I now know understand, thank you.
thx all for your helpful answers
Topic archived. No new replies allowed.