Ot - memory leaks

sorry for being off topic again, this is the only progrming list is subscribe to

i’m not exactly a proffesionally trained c programmer, but my code is getting
pretty tight.

the only thing letting it down now is memory leaks, ive been poking around a bit
on google, but all articles i can find on the subject are a bit vauge… if
someone can point me to a good resource about what will cause a memory leak and
what needs doing where and when to prevent them i would appreciate it.

Run valgrind on your application (http://valgrind.org/).
However, I always (even on almost empty application) have some SDL-related
valgrind errors.
Like the one when setting video mode.On 8/2/07, neil at cloudsprinter.com wrote:

the only thing letting it down now is memory leaks, ive been poking around
a bit
on google, but all articles i can find on the subject are a bit vauge… if
someone can point me to a good resource about what will cause a memory
leak and
what needs doing where and when to prevent them i would appreciate it.


Free Software - find interesting programs and change them
NetHack - meet interesting creatures, kill them and eat their bodies
Usenet - meet interesting people from all over the world and flame them
Decopter - unrealistic helicopter simulator, get it from
http://decopter.sf.net

2007/8/2, Jacek Poplawski :

the only thing letting it down now is memory leaks, ive been poking
around a bit
on google, but all articles i can find on the subject are a bit vauge…
if
someone can point me to a good resource about what will cause a memory
leak and
what needs doing where and when to prevent them i would appreciate it.

Run valgrind on your application ( http://valgrind.org/).
However, I always (even on almost empty application) have some SDL-related
valgrind errors.
Like the one when setting video mode.

what about Windows programmers ?> On 8/2/07, neil at cloudsprinter.com wrote:


SkunkGuru.

the only thing letting it down now is memory leaks, ive been poking
around a bit
on google, but all articles i can find on the subject are a bit vauge…
if
someone can point me to a good resource about what will cause a memory
leak and
what needs doing where and when to prevent them i would appreciate it.

Run valgrind on your application ( http://valgrind.org/).
However, I always (even on almost empty application) have some SDL-related
valgrind errors.
Like the one when setting video mode.

what about Windows programmers ?

i compiled my code on linux and ran it through valgrind to discover
just how bas
it was, i did notice some things that looked like it was sdl, so that
helps if i
know sdl may be doing some of the leaks, but all that stuff valgrind
pumps into
the cose… i still dont know exactly what i need to do to sort it or how :wink:
i tried the odd free(); here and there, but i jst got error messages.

as for windows ( i cuurently use windows while i dont bother sorting
linux out (
new monitor arrives soon all will be sweet )) i did poke around for a valgrind
like thing breifly but all i saw were some commercial shoddyness that probably
didnt really do what you wanted.

would there be some kinda valgrind windows cygwin combination or is that just
too wierd and crazy?

i compiled my code on linux and ran it through valgrind to discover
just how bas
it was, i did notice some things that looked like it was sdl

FYI, as far as I know there are no memory leaks in SDL, as long as you
free all your surfaces and shut down cleanly. There are a few memory leaks
in glibc and X11 that show up as SDL since SDL uses them, but there isn’t
anything we can do about that.

On the other hand, occasionally new leaks are introduced, so it never hurts
to take a look at them. :slight_smile:

See ya,
-Sam Lantinga, Lead Software Engineer, Blizzard Entertainment

would there be some kinda valgrind windows cygwin combination or is that just
too wierd and crazy?

Valgrind relies on an understanding of system calls that goes a level
lower than glibc, so you can’t just point it at any Unix-like thing and
have it work, including Cygwin.

–ryan.

Hi neil
as far as memory leaks are concerned, on windows i often open
the task manager and watch the memory usage of the task (game), it
would normally fluctuate but will not steadily increase, if its
increasing without any user interaction, the would narrow it down the
the loop that’s running, obviously the converse narrows it down to the
user functions.

Are you making surfaces like using SDL_TTF or rotozoom? i found they
would eat memory if i did not free the surfaces, also are you making
local variable surfaces in a loop that could be eating away.

To track it,
10 comment one part out and test for the leak.
20 no change THEN un-comment go to 10
30 Fix bug

TrishOn 8/2/07, sdl-request at lists.libsdl.org wrote:

Send SDL mailing list submissions to
sdl at lists.libsdl.org

To subscribe or unsubscribe via the World Wide Web, visit
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
or, via email, send a message with subject or body ‘help’ to
sdl-request at lists.libsdl.org

You can reach the person managing the list at
sdl-owner at lists.libsdl.org

When replying, please edit your Subject line so it is more specific
than “Re: Contents of SDL digest…”

Today’s Topics:

  1. Re: SDL 1.3 - SDL_Texture or SDL_Surface? (David Olofson)
  2. Cannot compile SDL_ttf under Cygwin (L-28C)
  3. Re: SDL 1.3 - SDL_Texture or SDL_Surface? (Torsten Giebl)
  4. Re: SDL 1.3 - SDL_Texture or SDL_Surface? (Torsten Giebl)
  5. Re: SDL 1.3 - SDL_Texture or SDL_Surface? (Jacek Poplawski)
  6. ot - memory leaks (neil at cloudsprinter.com)
  7. Re: ot - memory leaks (Jacek Poplawski)
  8. Re: ot - memory leaks (Skunk Guru)

Message: 1
Date: Thu, 2 Aug 2007 12:27:26 +0200
From: David Olofson
Subject: Re: [SDL] SDL 1.3 - SDL_Texture or SDL_Surface?
To: “A list for developers using the SDL library. (includes
SDL-announce)”
Message-ID: <200708021227.26881.david at olofson.net>
Content-Type: text/plain; charset=“iso-8859-1”

On Thursday 02 August 2007, Sitsofe Wheeler wrote:
[…]

With Vista my understanding was that the minimum out of the box
experience for OpenGL had been raised at the cost of the maximum
performance/capability without specialist drivers (due to OpenGL on
Direct3D emulation). Can anyone confirm that this in indeed the
case?

There were some last minute changes there (the original plan wasn’t
too popular), but I don’t remember the details. You should be able to
find the full story on the 'net.

If so in a decade’s time things may be simultaneously worse and
better for OpenGL but it’s true 99% of the people I know doing 3D on
Windows do it using Direct3D and not OpenGL.

Well, Microsoft will probably try to keep it this way. Windows
(including XBox) is a gaming platform large enough that many consider
it the only platform worth supporting. As long as MS can keep it
expensive to port to other platforms, Windows will remain the
platform of choice for gamers. Other platforms won’t have a chance to
gain enough momentum to become the primary target of a significant
number of developers.

Is OpenGL really only become the 3D API for the 5% of non-Windows
platforms?

I don’t believe it’s that bad. Although the really big market is
causual games (where most of the indie game developers hang around),
AAA titles are big business - and AAA titles are mostly played by
hardcore gamers and other relatively advanced users. Many of these
games are OpenGL only, and that doesn’t seem to be all that much of
an issue.

//David Olofson - Programmer, Composer, Open Source Advocate

.------- http://olofson.net - Games, SDL examples -------.
| http://zeespace.net - 2.5D rendering engine |
| http://audiality.org - Music/audio engine |
| http://eel.olofson.net - Real time scripting |
’-- http://www.reologica.se - Rheology instrumentation --’


Message: 2
Date: Thu, 02 Aug 2007 09:43:19 -0400
From: L-28C
Subject: [SDL] Cannot compile SDL_ttf under Cygwin
To: sdl at libsdl.org
Message-ID: <f8sn1s$fa2$1 at sea.gmane.org>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hello everyone!

Okay, I did a google search. To prove so… :slight_smile:
http://twomix.devolution.com/pipermail/sdl/2003-July/055083.html
http://www.wesnoth.org/forum/viewtopic.php?p=196813&sid=bcf0872127cb71f20774d8a2c903a548

I’m having the same problem as the first link. The linker cannot find
"_setjmp". He got no replies… The second link suggests to compile
everything yourself, which I did.

Maybe I should use an older version of FreeType or something?

Thanks in advance!

P.S, here’s my linker output:

Leo at Rita /cygdrive/c/ttf/sdl_ttf-2.0.9
$ make
if /bin/sh ./libtool --tag=CC --mode=compile gcc -DPACKAGE_NAME=""
-DPACKAGE_T
ARNAME="" -DPACKAGE_VERSION="" -DPACKAGE_STRING=""
-DPACKAGE_BUGREPORT="
" -DPACKAGE=“SDL_ttf” -DVERSION=“2.0.9” -DSTDC_HEADERS=1
-DHAVE_SYS_TYPES_H=
1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1
-DHAVE_MEMORY_H=1 -DHA
VE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1
-DHAVE_DL
FCN_H=1 -I. -I. -g -O2 -I/usr/local/include/freetype2
-I/usr/local/include
-I/usr/local/include/SDL -I/usr/include/mingw -mno-cygwin
-Dmain=SDL_main -DHAVE
_OPENGL -MT SDL_ttf.lo -MD -MP -MF “.deps/SDL_ttf.Tpo” -c -o SDL_ttf.lo
SDL_ttf.
c;
then mv -f “.deps/SDL_ttf.Tpo” “.deps/SDL_ttf.Plo”; else rm -f
".deps/SD
L_ttf.Tpo"; exit 1; fi
mkdir .libs
gcc -DPACKAGE_NAME="" -DPACKAGE_TARNAME="" -DPACKAGE_VERSION=""
-DPACKAGE
_STRING="" -DPACKAGE_BUGREPORT="" -DPACKAGE=“SDL_ttf”
-DVERSION=“2.0.9”
-DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1
-DHAVE_STDLIB_H=1 -DHA
VE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1
-DHAVE_ST
DINT_H=1 -DHAVE_UNISTD_H=1 -DHAVE_DLFCN_H=1 -I. -I. -g -O2
-I/usr/local/include/
freetype2 -I/usr/local/include -I/usr/local/include/SDL
-I/usr/include/mingw -mn
o-cygwin -Dmain=SDL_main -DHAVE_OPENGL -MT SDL_ttf.lo -MD -MP -MF
.deps/SDL_ttf.
Tpo -c SDL_ttf.c -DPIC -o .libs/SDL_ttf.o
gcc -DPACKAGE_NAME="" -DPACKAGE_TARNAME="" -DPACKAGE_VERSION=""
-DPACKAGE
_STRING="" -DPACKAGE_BUGREPORT="" -DPACKAGE=“SDL_ttf”
-DVERSION=“2.0.9”
-DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1
-DHAVE_STDLIB_H=1 -DHA
VE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1
-DHAVE_ST
DINT_H=1 -DHAVE_UNISTD_H=1 -DHAVE_DLFCN_H=1 -I. -I. -g -O2
-I/usr/local/include/
freetype2 -I/usr/local/include -I/usr/local/include/SDL
-I/usr/include/mingw -mn
o-cygwin -Dmain=SDL_main -DHAVE_OPENGL -MT SDL_ttf.lo -MD -MP -MF
.deps/SDL_ttf.
Tpo -c SDL_ttf.c -o SDL_ttf.o >/dev/null 2>&1
windres version.rc version.o
/bin/sh ./libtool --tag=CC --mode=link gcc -g -O2
-I/usr/local/include/freetype
2 -I/usr/local/include -I/usr/local/include/SDL -I/usr/include/mingw
-mno-cygwin
-Dmain=SDL_main -DHAVE_OPENGL -o libSDL_ttf.la -rpath /usr/local/lib
-no-unde
fined -release 2.0 -version-info 6:3:6 -Wl,version.o SDL_ttf.lo
-L/usr/local/li
b -L/usr/local/lib -lfreetype -lz -L/usr/local/lib -lmingw32 -lSDLmain
-lSDL -mn
o-cygwin -mwindows
gcc -shared .libs/SDL_ttf.o -L/usr/local/lib
/usr/local/lib/libfreetype.a -lz
-lmingw32 -lSDLmain /usr/local/lib/libSDL.dll.a -mno-cygwin
-Wl,version.o -mno-
cygwin -mwindows -o .libs/SDL_ttf.dll -Wl,–enable-auto-image-base
-Xlinker --ou
t-implib -Xlinker .libs/libSDL_ttf.dll.a
/usr/local/lib/libfreetype.a(sfnt.o): In function tt_face_build_cmaps': /cygdrive/c/ft/freetype-2.3.5/src/sfnt/ttcmap.c:2309: undefined reference to_s
etjmp’
/usr/local/lib/libfreetype.a(smooth.o): In function
gray_convert_glyph_inner': /cygdrive/c/ft/freetype-2.3.5/src/smooth/ftgrays.c:1619: undefined reference to_setjmp’
Creating library file: .libs/libSDL_ttf.dll.a
collect2: ld returned 1 exit status
make: *** [libSDL_ttf.la] Error 1


Message: 3
Date: Thu, 2 Aug 2007 15:53:20 +0200 (CEST)
From: “Torsten Giebl”
Subject: Re: [SDL] SDL 1.3 - SDL_Texture or SDL_Surface?
To: “A list for developers using the SDL library. (includes
SDL-announce)”
Message-ID:
<1486.141.99.122.11.1186062800.squirrel at mail.syntheticsw.com>
Content-Type: text/plain;charset=iso-8859-1

Hello !

Sorry i really don’t understand where the problem here is.

You cannot go with 2D and OpenGL alone. On one side
thanks to Microsoft trying to kill all standards, that they don’t
created. On the other side for bad drivers from companies, that
only support D3D.

This is a fact i think and everybody here will aggree.

So SDL 1.3 needs a D3D driver that is regularly
updated, an OpenGL and 2D drivers like GDI, X11, …

SDL 1.3 will need to support at least all the things
that SDL 1.2 supported. So old apps can be as easy
as possible ported to it. But adding a lot more
like rotating, zooming would be nice, but it will
bloat SDL.

Look at ALLEGRO, it is good/bad example for trying to put
everything into an API.

David Olofson named something like Advanced2D that
could be easily created as an AddOn Library for a good designed
SDL 1.3. It would need to support a D3D part, an OpenGL and a 2D
part as a fallback.

SDL 1.3 should have basically the same commands
like SDL 1.2 +

SDL_CreateTexture (SDL_Surface)
SDL_RemoveTexture (SDL_Surface)
SDL_UpdateTexture (SDL_Surface)

SDL_BlitSurface will work the same as before,
it only looks maybe on a SDL_Surface flag, that
when there is a texture assoc. with that Surface,
use this for fast blitting.

With the 2D drivers this will be a dummy,
as there is no texture support.
OpenGL and D3D will use their Textures to allow
fast screen blitting.

And once you have Texture support, with an external
library you can do anything with these Textures
you want, use them for rotating blitting, use them for
complicated shader blits or whatever.

What is the current situation in SDL1.3.
I thought there were methods to allow to associate
a Texture to a SDL_Surface.

CU


Message: 4
Date: Thu, 02 Aug 2007 16:15:51 +0200
From: Torsten Giebl
Subject: Re: [SDL] SDL 1.3 - SDL_Texture or SDL_Surface?
To: “A list for developers using the SDL library. (includes
SDL-announce)”
Message-ID: <46B1E717.8000005 at syntheticsw.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hello !

Is there a way to extract, maybe from the header files,
the current SDL 1.3 API without Audio, … only the GFX API
and only the commands that are public to the user
at the end ?

This would allow a more practical discussion.
What parameters are needed, what way SDL 1.3 is
already different to SDL 1.2.

CU


Message: 5
Date: Thu, 2 Aug 2007 16:33:07 +0200
From: “Jacek Poplawski”
Subject: Re: [SDL] SDL 1.3 - SDL_Texture or SDL_Surface?
To: “A list for developers using the SDL library. (includes
SDL-announce)”
Message-ID:
<1f276abf0708020733i3bf75967y28c311766e1b831e at mail.gmail.com>
Content-Type: text/plain; charset=“iso-8859-1”

On 8/2/07, Torsten Giebl wrote:

But adding a lot more
like rotating, zooming would be nice, but it will
bloat SDL.

True.

There is no good support for image formats (SDL_Image can only load, not
save, and doesn’t allow to control quality), so why support image
transformations inside SDL?


Free Software - find interesting programs and change them
NetHack - meet interesting creatures, kill them and eat their bodies
Usenet - meet interesting people from all over the world and flame them
Decopter - unrealistic helicopter simulator, get it from
http://decopter.sf.net
-------------- next part --------------
An HTML attachment was scrubbed…
URL: http://lists.libsdl.org/private.cgi/sdl-libsdl.org/attachments/20070802/2b61cd60/attachment-0001.htm


Message: 6
Date: Thu, 2 Aug 2007 16:00:42 +0100
From: neil at cloudsprinter.com
Subject: [SDL] ot - memory leaks
To: sdl at lists.libsdl.org
Message-ID:
<20070802160042.6324ktcpkck4skgs at webmail.cloudsprinter.com>
Content-Type: text/plain; charset=ISO-8859-1

sorry for being off topic again, this is the only progrming list is subscribe to

i’m not exactly a proffesionally trained c programmer, but my code is getting
pretty tight.

the only thing letting it down now is memory leaks, ive been poking around a bit
on google, but all articles i can find on the subject are a bit vauge… if
someone can point me to a good resource about what will cause a memory leak and
what needs doing where and when to prevent them i would appreciate it.


Message: 7
Date: Thu, 2 Aug 2007 17:12:51 +0200
From: “Jacek Poplawski”
Subject: Re: [SDL] ot - memory leaks
To: “A list for developers using the SDL library. (includes
SDL-announce)”
Message-ID:
<1f276abf0708020812jb55182apb91ef946a9d91b06 at mail.gmail.com>
Content-Type: text/plain; charset=“iso-8859-1”

On 8/2/07, neil at cloudsprinter.com wrote:

the only thing letting it down now is memory leaks, ive been poking around
a bit
on google, but all articles i can find on the subject are a bit vauge… if
someone can point me to a good resource about what will cause a memory
leak and
what needs doing where and when to prevent them i would appreciate it.

Run valgrind on your application (http://valgrind.org/).
However, I always (even on almost empty application) have some SDL-related
valgrind errors.
Like the one when setting video mode.


Free Software - find interesting programs and change them
NetHack - meet interesting creatures, kill them and eat their bodies
Usenet - meet interesting people from all over the world and flame them
Decopter - unrealistic helicopter simulator, get it from
http://decopter.sf.net
-------------- next part --------------
An HTML attachment was scrubbed…
URL: http://lists.libsdl.org/private.cgi/sdl-libsdl.org/attachments/20070802/26b16608/attachment.html


Message: 8
Date: Thu, 2 Aug 2007 17:21:54 +0200
From: “Skunk Guru”
Subject: Re: [SDL] ot - memory leaks
To: “A list for developers using the SDL library. (includes
SDL-announce)”
Message-ID:

Content-Type: text/plain; charset=“iso-8859-1”

2007/8/2, Jacek Poplawski :

On 8/2/07, neil at cloudsprinter.com wrote:

the only thing letting it down now is memory leaks, ive been poking
around a bit
on google, but all articles i can find on the subject are a bit vauge…
if
someone can point me to a good resource about what will cause a memory
leak and
what needs doing where and when to prevent them i would appreciate it.

Run valgrind on your application ( http://valgrind.org/).
However, I always (even on almost empty application) have some SDL-related
valgrind errors.
Like the one when setting video mode.

what about Windows programmers ?


SkunkGuru.
-------------- next part --------------
An HTML attachment was scrubbed…
URL: http://lists.libsdl.org/private.cgi/sdl-libsdl.org/attachments/20070802/430df1ce/attachment.htm



SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

End of SDL Digest, Vol 8, Issue 5



In a time of universal deceit, telling the truth is a revolutionary act.

Trish & Thy also Available on

Skype tranmaithy63,

Quoting Sam Lantinga :

i compiled my code on linux and ran it through valgrind to discover
just how bas
it was, i did notice some things that looked like it was sdl

FYI, as far as I know there are no memory leaks in SDL,

sorry didn’t mean to diss teh SDL :wink:

i still dont really know what is the best way to deal with /aviod memory leaks
tho…

valgrind documentaion seems to tell me how the memory leak occurs but
not how to
plug it!!

there must be something somewhere on the web that tells a dumbass like
me where
and what to do!

Wikipedia is fairly helpful on this,

You basically need to make sure that, whatever you create (directly or
indirectly) you clean up using the appropriate function. Otherwise it
just floats in the ether, using resources which could be used for
something else.

Common pairings:

malloc -> free
new -> delete
new[] -> delete[]
SDL_CreateX -> SDL_FreeX
glGenX -> glDeleteX

And so on.

Smart pointers go quite far in avoiding memory leaks, but require a
decent knowledge of C++ to understand.

In particular, Boost "intrusive_ptr"s can easily wrap DirectX objects
and SDL surfaces, maintaining the reference count for you.

For many objects, you can use STL containers instead of rolling your
own containers.

Hope this helps,

PeterOn 02/08/07, neil at cloudsprinter.com wrote:

Quoting Sam Lantinga :

i compiled my code on linux and ran it through valgrind to discover
just how bas
it was, i did notice some things that looked like it was sdl

FYI, as far as I know there are no memory leaks in SDL,

sorry didn’t mean to diss teh SDL :wink:

i still dont really know what is the best way to deal with /aviod memory leaks
tho…

valgrind documentaion seems to tell me how the memory leak occurs but
not how to
plug it!!

there must be something somewhere on the web that tells a dumbass like
me where
and what to do!


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

You basically need to make sure that, whatever you create (directly or
indirectly) you clean up using the appropriate function. Otherwise it
just floats in the ether, using resources which could be used for
something else.

ok, i get this, one thing i’m not too sure about is, in some of my fucntions i
create surfaces for the game as and when needed, for this the function uses 5
surfaces which i SDL_Surface *surface at the start of the function, i
then free
4 of these but one needs to be returned, what happens then? i cant free the
surface before it returns otherwise it will return nothing. can i free
it after
i’ve returned it? i thought the return was the end of the function…

You should destroy the surface when you stop using it.
It can be end of the program if you can’t find better place.
IMHO it’s better to use C++ and allocate SDL_Surface in constructors and
destroy it in destructors.On 8/3/07, neil at cloudsprinter.com wrote:

ok, i get this, one thing i’m not too sure about is, in some of my
fucntions i
create surfaces for the game as and when needed, for this the function
uses 5
surfaces which i SDL_Surface *surface at the start of the function, i
then free
4 of these but one needs to be returned, what happens then? i cant free
the
surface before it returns otherwise it will return nothing. can i free
it after
i’ve returned it? i thought the return was the end of the function…


Free Software - find interesting programs and change them
NetHack - meet interesting creatures, kill them and eat their bodies
Usenet - meet interesting people from all over the world and flame them
Decopter - unrealistic helicopter simulator, get it from
http://decopter.sf.net

Quoting Jacek Poplawski :> On 8/3/07, neil at cloudsprinter.com wrote:

ok, i get this, one thing i’m not too sure about is, in some of my
fucntions i
create surfaces for the game as and when needed, for this the function
uses 5
surfaces which i SDL_Surface *surface at the start of the function, i
then free
4 of these but one needs to be returned, what happens then? i cant free
the
surface before it returns otherwise it will return nothing. can i free
it after
i’ve returned it? i thought the return was the end of the function…

You should destroy the surface when you stop using it.
It can be end of the program if you can’t find better place.
IMHO it’s better to use C++ and allocate SDL_Surface in constructors and
destroy it in destructors.

but if it’s inside a function i can only access it from that function, my
(probably worng) understanding was that anything created in a function was
killed at the end of that fucntion, or is that java?

If you allocate memory inside function you should free it inside or outside
that function.
That’s why using pointers in dangerous.
I think you should play with C/C++ first. You can find lots of good books
about it.On 8/3/07, neil at cloudsprinter.com wrote:

but if it’s inside a function i can only access it from that function, my
(probably worng) understanding was that anything created in a function was
killed at the end of that fucntion, or is that java?


Free Software - find interesting programs and change them
NetHack - meet interesting creatures, kill them and eat their bodies
Usenet - meet interesting people from all over the world and flame them
Decopter - unrealistic helicopter simulator, get it from
http://decopter.sf.net

Java has a garbage collector, which takes a lot of stuff of your
hands (though not everything, there are some quirks).

In C/C++, everything you allocate, must be de-allocated. :wink:

Ofcourse, there are exceptions…

2007/8/3, Jacek Poplawski :> On 8/3/07, neil at cloudsprinter.com wrote:

but if it’s inside a function i can only access it from that function, my
(probably worng) understanding was that anything created in a function was
killed at the end of that fucntion, or is that java?

If you allocate memory inside function you should free it inside or outside
that function.
That’s why using pointers in dangerous.
I think you should play with C/C++ first. You can find lots of good books
about it.


Free Software - find interesting programs and change them
NetHack - meet interesting creatures, kill them and eat their bodies
Usenet - meet interesting people from all over the world and flame them
Decopter - unrealistic helicopter simulator, get it from
http://decopter.sf.net


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Stefan Hendriks

http://www.fundynamic.nl

There is a memory debugger named Fortify


Beside it can tell you memory leaks it’s also capable to trap many memory
violations. It’s source based so you can use it on either system. Back in bad
old days when I was on DOS I used it a lot and was satisfied with it.

To avoid memory leaks and memory violations you should focus on documenting
and a clean program structure. If a function returns a pointer to memory that
it has allocated, then always mention that in a comment:
/* … returns a pointer to a blah that the caller is responsible for… /
And similarly for members in structs:
struct BAR {
/
Points to a FOO. The memory is owned by this BAR. */
FOO foo;
/
Points to another FOO. The memory is NOT owned by this BAR. */
FOO *foo2;

Also make separate functions to destroy your objects (and here you can make
use of you carefully typed comments):
destroy_bar(BAR *bar)
{

destroy_foo(foo);
free(bar);
}

This way you can track where things come from and who owns them when the
memory debugger tells you that you have a leak.

Moving to C++ you basically have to do the same. Using STL may sometimes help
you, but if you put pointers into the containers you must be aware of the
leak problem anyway. Using references instead of pointers in C++ may also
help you, but it’s not likely that you can live entirely without pointers in
C++.On Thursdayen den 2 August 2007, neil at cloudsprinter.com wrote:

would there be some kinda valgrind windows cygwin combination or is that
just too wierd and crazy?

Christer

I use a simple memory manager for C code to catch memory leaks. Not fancy, but
it fits my needs. I’ve packaged it for anyone who might be interested. You
can download the 16KB zip file from

http://home.pacbell.net/theposts/memmgr.zip

JeffOn Fri August 3 2007 03:15, Christer Sandberg wrote:

There is a memory debugger named Fortify
http://www.geocities.com/SiliconValley/Horizon/8596/fortify.html
Beside it can tell you memory leaks it’s also capable to trap many memory
violations. It’s source based so you can use it on either system. Back in
bad old days when I was on DOS I used it a lot and was satisfied with it.

I use a simple memory manager for C code to catch memory leaks. Not
fancy, but
it fits my needs. I’ve packaged it for anyone who might be interested. You
can download the 16KB zip file from

http://home.pacbell.net/theposts/memmgr.zip

thanks for this, i’ll take a look, but i guess what i really need to do is go
and find a big fat book on C at the library, because i still dont really quite
get using the malloc stuff, so don’t because it scares me :wink:

i have been sttempting to use free() but it all seems very obscure,
someimes it
works, sometimes it dosnt. but i’m sure that’s all my fault :wink:

i might just hunt around for some properly written SDL programs and learn from
other peoples stuff… so if anyone can think of a tight piece of SDL
code i can
peek n poke let me know.

right now i’m just gonna concentrate on getting the game to how i want and go
about memory leaks when i’m finished.

I use a simple memory manager for C code to catch memory leaks. Not

fancy, but
it fits my needs. I’ve packaged it for anyone who might be
interested. You

can download the 16KB zip file from

http://home.pacbell.net/theposts/memmgr.zip

thanks for this, i’ll take a look, but i guess what i really need to
do is
go
and find a big fat book on C at the library, because i still dont
really
quite
get using the malloc stuff, so don’t because it scares me :wink:

I used to be afraid of malloc but when I got into a program that
required it and became comfortable with it.

i have been sttempting to use free() but it all seems very obscure,
someimes it
works, sometimes it dosnt. but i’m sure that’s all my fault :wink:

The one thing to remember about using malloc/free is to keep track of
your pointers’ states. Initialize all pointers to NULL when they’re
declared. Always check before assigning a pointer to malloc’d space.
If it isn’t NULL, you don’t want to assign it. Always check the
returned value when you malloc to make sure that space was assigned.
When you free the memory a pointer is pointing to, always reassign the
pointer to NULL so that you know that the next time you need to use the
pointer you can make sure that it isn’t already pointing to assigned
memory. And always keep track of how much space you requested and never
store outside its bounds.>>> On 8/4/2007 at 2:17 PM, wrote:


Lilith

your pointers’ states. Initialize all pointers to NULL when they’re
declared.
Talking about local variables I would general say in don’t, since it will
prevent the compiler from giving warnings for one of the common error cases:
void foo(int a, int b)
{
char *buf=NULL;
if (a) {
buf = malloc(100);
if (buf==NULL) error();
}
if (b) {
*buf = 0;
}

}
This code crashes randomly depending on the values of a and b. Remove the
initialization of buf in the declaration and you will get a warning about the
mistake, at least from a decent compiler.

Always check before assigning a pointer to malloc’d space.
If it isn’t NULL, you don’t want to assign it. Always check the
returned value when you malloc to make sure that space was assigned.
When you free the memory a pointer is pointing to, always reassign the
pointer to NULL so that you know that the next time you need to use the
pointer you can make sure that it isn’t already pointing to assigned
memory. And always keep track of how much space you requested and never
store outside its bounds.

Here is a macro that often will help help you in making a safer allocation;
#define CALLOC(type, nelem) ((type*)calloc(nelem, sizeof(type)))
used like:
struct MY_TYPE *my_obj;

my_obj = CALLOC(struct MY_TYPE, 1);
The point here is that one reduce the risk of getting the wrong size of the
object (it may easy happen due to copy-paste).
Note that in C there is no requirement to cast the return value when assigning
from a void pointer like in:
struct MY_TYPE *my_obj;
my_obj = (struct MY_TYPE *)calloc(1, sizeof(struct MY_TYPE));
and it is actually quite meaningless to do it because in practice the problem
is usually originating from a copy-paste, so when you get the warning for:
my_obj = (struct ANOTHER_TYPE *)calloc(1, sizeof(struct ANOTHER_TYPE));
you may feel satisfied when correct it to
my_obj = (struct MY_TYPE *)calloc(1, sizeof(struct ANOTHER_TYPE));
because the warning is gone, but the problems will arise in runtime.

However, using the macro above will not give you the opportunity to make a
latter error.

You can make a similar macro wrapping malloc. However, calloc (that
initializes the memory to 0), is often to prefere when using structs. In such
case it is also better to set pointers that are members in the struct to NULL
(i.e. 0) since the current compilers don’t complain about accessing
uninitialized memory returned from malloc.

If you want to ensure to not forget checking the the return value from
malloc/calloc, you can add that as a part of the macro. A general error
handling like e.g. a call to assert(0) is usually enough. That will at least
give you the line number printed in the console. The most common memory
errors are 1:infinite loop or recursion 2: miscalculation of the size of a
dynamic array. In neither of the cases it is likely to figure out any error
recovery anyway, so aborting with a call to assert is good enough.

To ensure not miss checking the return values from SDL functions returning
pointers to dynamic memory, you can wrap also these in macros (but these does
not need the type cast, of course).On Saturdayen den 4 August 2007, Lilith Calbridge wrote:

Christer

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Christer Sandberg wrote:

your pointers’ states. Initialize all pointers to NULL when they’re
declared.
Talking about local variables I would general say in don’t, since it will
prevent the compiler from giving warnings for one of the common error
cases:
void foo(int a, int b)
{
char *buf=NULL;
if (a) {
buf = malloc(100);
if (buf==NULL) error();
}
if (b) {
*buf = 0;
}

}
This code crashes randomly depending on the values of a and b. Remove the
initialization of buf in the declaration and you will get a warning
about the
mistake, at least from a decent compiler.

Hi, just throwing in my 2 cents, and this is by no means the only way
to do this, but I would probably re-write that sample to be something
like:

void foo(int a, int b)
{
char *buf=NULL;
if (a) {
buf = malloc(100);
}

if (buf==NULL) {
error();
}
else if (b) {
*buf = 0;//we know buf has been allocated, so assign it a value;
}

}

Jim
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFGtPu5QuDJiZ/QrH0RAoC/AKDU7/k9f5kNauVj0rTaViiEUpCK+gCgvzpd
KCIgIRUDx1/VhNJ43TOFSm0=
=bnpi
-----END PGP SIGNATURE-----> On Saturdayen den 4 August 2007, Lilith Calbridge wrote: