Ot - memory leaks

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

No, no, no.

If you don’t understand how malloc() and free() work, you should learn
that first. It can be VERY hard to track down and fix memory leaks later.

malloc() is so fundamental to C programming, you either have to learn it
first or not use C. Those are basically your options.

(For what it’s worth, I’ve been saying for years that all programming
job interviews–for C, Basic, Perl, anything–should start with the
question: “what is a pointer?” and if the person can’t answer, that’s
the end of the interview. As strange as that sounds, companies that try
this have told me that more than half of their entry-level candidates
fail here, even for jobs that would explicitly involve C programming. I
think it’s worth knowing what one is, even if you only use scripting
languages. It separates the men from the boys, so to speak.)

code i can peek n poke let me know.

Irony noted without further comment. :slight_smile:

–ryan.

1 Like

Quoting “Ryan C. Gordon” :

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

No, no, no.

looks like i’ve been told then :wink:

( hangs head in shame )

Quoting “Ryan C. Gordon” :

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

No, no, no.

just for the record ( or the ongoing saga it may seem ) i just went into linux
got my program compiled by GCC and at a certain point it crashes, presumbably
due to dodgy use of memory… valgrind spits me 9 printout pages worth of
output for me to stare at blankly.

sometimes gcc lets me get away with certain things, and other times mingw will
not other things, and other times my RISC OS gcc will allow things other
compilers blah blah

basically my code is for want of a more offencive word, borked.

sorry for the spam, but the gf, the four year old and the six year old
just dont
understand my dilemas :wink:

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

neil at cloudsprinter.com wrote:

Quoting “Ryan C. Gordon” :

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

just for the record ( or the ongoing saga it may seem ) i just went
into linux
got my program compiled by GCC and at a certain point it crashes,
presumbably
due to dodgy use of memory… valgrind spits me 9 printout pages worth of
output for me to stare at blankly.

sometimes gcc lets me get away with certain things, and other times
mingw will
not other things, and other times my RISC OS gcc will allow things other
compilers blah blah

basically my code is for want of a more offencive word, borked.

sorry for the spam, but the gf, the four year old and the six year old
just dont
understand my dilemas :wink:

I am wondering if it would really be so bad to use some c++. For
example, if you have a struct Foo, use the c++ “new” to create it.

typedef struct testFoo
{
int x;
}testFoo;

int main(int argc, char argv[])
{
testFoo
myFoo = 0;

//… do some work…

//create an instance of Foo;
if(myFoo)
{
//if myFoo is not null, then delete it.
delete myFoo;
}
myFoo = new testFoo();

//…, do some more work

//always delete Foo before exiting
delete myFoo;
return EXIT_SUCCESS;
}

jim

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

iD8DBQFGti2JQuDJiZ/QrH0RAm0kAJ9vfdAD0cBm9ugKeLa6DTiZShRwYQCbB2OB
vVKfp7UMxwq7kTg52WycxQM=
=UEJ3
-----END PGP SIGNATURE-----

My approach to memory for any game is i don’t create and destroy
memory, the only place i use a malloc is to allocate strings with
pointers for my help and other text, file.

char * StringTable[MAX_STRINGS] ;

read a line

StringTable[ThisString] = (char*)malloc(StringLength);
StringCopy(ReadLine,StringTable[ThisString] ) ;

loop untill all strings are read…

Then at the end of the game i simply

for(int s=0;s<MAX_STRINGS;s++)
{
if(StringTable[s] )
{
free(StringTable[s]);
}
}

For game objects and what have you, i define an Array of max objects

#typedef struct
{
bool Active;
int NextObject;
int PrevObject;
… other stuff
}Shape;

Shape GameShapes[MAXBALLS];

Then when i want to create a new object (Shape) i simply find the
first empty Object in my Array by checking the Active flag with a
simple FOR NEXT LOOP which returns the number of the object in the
array.

Then link it using the Next and Prev ints,

insertBeginning(int newObject);

see http://en.wikipedia.org/wiki/Linked_list for info on linked lists.

Then in my loops for drawing and moving the objects i just use

int Ball = FirstLink;
while(Ball!=LAST_LINK)
{
// some code
Ball = GameShapes[Ball].NextLink;
}

Now yes its not pointers and yes its not OO code but it guarantees
that my games don’t have memory leaks because i know how much memory
it needs and is using.

it is also faster as there is no garbage collection and each object is
reused over and over.

I have used this method in almost every one my 29 games and it works.

At the end of the day most of us are writing games, and we are not
working for NASA, we want speed not technically perfect code.

Trish x

I have used this method in almost every one my 29 games and it works.

Any chance of a website where we can see these games? Or are they on
the SDL website?

Incidently, I use a very similar method with my background/object engine
which uses SDL. At first I allocate enough memory to hold all the induvidual
Nodes and initialise the link-pointers. Then when returning the memory
pointer as a pointer to our linked structure, I can access each node just as
I would a regular array, and I do not have to traverse through the linked
list itself.

Cheers,
PeterFrom: patricia.curtis@gmail.com (Patricia Curtis)

Interestingly, I recently got into game programming professionally and this
first project I’m working on has memory setup like this by a guy who
originally made one of the ports for centipede (so he’s been around a
while!) (:> ----- Original Message -----

From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Peter Ketting
Sent: Sunday, August 05, 2007 9:14 PM
To: A list for developers using the SDL library. (includes SDL-announce)
Subject: Re: [SDL] ot - memory leaks

From: patricia.curtis@gmail.com (Patricia Curtis)

I have used this method in almost every one my 29 games and it works.

Any chance of a website where we can see these games? Or are they on
the SDL website?

Incidently, I use a very similar method with my background/object engine
which uses SDL. At first I allocate enough memory to hold all the induvidual
Nodes and initialise the link-pointers. Then when returning the memory
pointer as a pointer to our linked structure, I can access each node just as
I would a regular array, and I do not have to traverse through the linked
list itself.

Cheers,
Peter


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

If you are going to use C++, why bother using naked pointers when you
can use smart pointer objects like std::auto_ptr and
boost::shared_ptr. Along with all the various std::container classes
there is rarely, if ever, a reason to invoke delete or delete [] in
C++.

RAII is the most fun :slight_smile:

If you are just going to be exchanging malloc for new and free for
delete you really don’t buy yourself much.On 05/08/07, James Barrett wrote:

I am wondering if it would really be so bad to use some c++. For
example, if you have a struct Foo, use the c++ “new” to create it.

typedef struct testFoo
{
int x;
}testFoo;

int main(int argc, char argv[])
{
testFoo
myFoo = 0;

//… do some work…

//create an instance of Foo;
if(myFoo)
{
//if myFoo is not null, then delete it.
delete myFoo;
}
myFoo = new testFoo();

//…, do some more work

//always delete Foo before exiting
delete myFoo;
return EXIT_SUCCESS;
}

jim

I am wondering if it would really be so bad to use some c++. For
example, if you have a struct Foo, use the c++ “new” to create it.

typedef struct testFoo
{
int x;
}testFoo;

int main(int argc, char argv[])
{
testFoo
myFoo = 0;

//… do some work…

//create an instance of Foo;
if(myFoo)
{
//if myFoo is not null, then delete it.
delete myFoo;
}
myFoo = new testFoo();

//…, do some more work

//always delete Foo before exiting
delete myFoo;
return EXIT_SUCCESS;
}

jim

If you are just going to be exchanging malloc for new and free for
delete you really don’t buy yourself much.

I was thinking more for the C beginner. It might be easier to learn memory
management by using new and delete instead of learning malloc and free. Once
you get into using smart pointers, and boost, then your talking more
intermediate and advanced programming. But for the beginner, doing top-down
structured style programming, using some c+±isms wouldn’t hurt.

JimOn 05/08/07, James Barrett <@James_Barrett> wrote:
On 8/6/07, Brian <brian.ripoff at gmail.com> wrote:

I don’t know - I really learned malloc and new at about the same time.
malloc doesn’t seem like it would be too hard to learn, really… you just
have to remember to check that it doesn’t return a NULL pointer, and
remember that you have to allocate in bytes, e.g. sizeof(int)*4 instead of 4
for allocating an array of 4 int’s. Learning new instead might even be
counterproductive, since you don’t allocate by the number of bytes.On 8/6/07, James Barrett wrote:

On 05/08/07, James Barrett < xucaen at gmail.com> wrote:

I am wondering if it would really be so bad to use some c++. For
example, if you have a struct Foo, use the c++ “new” to create it.

typedef struct testFoo
{
int x;
}testFoo;

int main(int argc, char argv[])
{
testFoo
myFoo = 0;

//… do some work…

//create an instance of Foo;
if(myFoo)
{
//if myFoo is not null, then delete it.
delete myFoo;
}
myFoo = new testFoo();

//…, do some more work

//always delete Foo before exiting
delete myFoo;
return EXIT_SUCCESS;
}

jim

On 8/6/07, Brian <brian.ripoff at gmail.com > wrote:

If you are just going to be exchanging malloc for new and free for
delete you really don’t buy yourself much.

I was thinking more for the C beginner. It might be easier to learn memory
management by using new and delete instead of learning malloc and free. Once
you get into using smart pointers, and boost, then your talking more
intermediate and advanced programming. But for the beginner, doing top-down
structured style programming, using some c+±isms wouldn’t hurt.

Jim


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


“If you see somebody who you don’t know getting into a crop duster that
doesn’t belong to you, report them.” – President George W. Bush

Hi folks,
I’m very interested in a tool like this “fortify” memory manager… I
haven’t been able to get it working though, with MSVC++2005… Anyone have
any luck at all with this setup? I am on WinXP.
Thanks,
-Dave> ----- Original Message -----

From: christer.sandberg@mdh.se (Christer Sandberg)
To: "A list for developers using the SDL library. (includes SDL-announce)"

Sent: Friday, August 03, 2007 5:15 AM
Subject: Re: [SDL] ot - memory leaks

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?
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.

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++.

Christer


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

I’m not sure if I said it, but I am talking specifically about the "Fortify"
memory manager/tracker linked to earlier in this discussion.

Okay, after perusing the internet a bit more(I had done some previously,
promise!), I actually got it working great with the test files that come
with it. I’ll post any problems I have working it into my already
established project (If such problems arise!)

For anyone interested in how I got it working, I’ll document the steps I
took here:
#1) Create a new empty Win32 Console Project in MSVC++2005 Express Edition
(empty project! I always start with those…)
#2) import the two header files, and the fortify.cxx and test2.cxx (or
test.c)
#3) Go into the Project Properties->Configuration
Properties->c/c+±>Preprocessor Options, and add “FORTIFY” to the list of
preprocessor definitions
#4) Go into the Project Properties->Configuration Properties->Linker->Input
options, and under additional dependencies add “msvcprtd.lib” (leave off
the -d for release version). Then add in the Ignore Specific Library field
"msvcirtd.lib" (again, no final -d for the release version.) Of course, not
the quotes, just what’s inside them…
#5) Compile/Build Solution! Voila!

I tested it with both Debug and release versions, as well as both included
test programs, and they compiled without a hitch… (Well, a few warnings on
converting an signed int to unsigned… but I just went in and explicitly
cast them to unsigned, so the compiler would be quiet!)
Now, we’ll see what happens when I try to intergrate it into an existing
project… Wish me luck! This thing could really help me clean up my code…
-Dave> ----- Original Message -----

From: christer.sandberg@mdh.se (Christer Sandberg)
To: "A list for developers using the SDL library. (includes SDL-announce)"

Sent: Friday, August 03, 2007 5:15 AM
Subject: Re: [SDL] ot - memory leaks

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?
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.

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++.

Christer


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