Freeing NULL pointers...

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Post Reply
User avatar
stevenh
TTDPatch Developer
TTDPatch Developer
Posts: 759
Joined: 24 Jul 2005 05:07
Location: Canberra, Australia
Contact:

Freeing NULL pointers...

Post by stevenh »

To the devs,
I've just been playing around with the source in an archaic cross-compile environment and it turns out that my compiler/libraries do not like to free NULL pointers.
I've started a find/replace to replace any calls to free to my own safe_free which checks for a NULL pointer first.

Would this change be acceptable in the trunk? I would be angry if I had to start using safe_free instead of free.. but for this current port it is the easiest method to get around the current problem; unless someone knows how to inline a free() func that has the same name as the free() func that you then want to call :)

Thanks all.
peter1138
OpenTTD Developer
OpenTTD Developer
Posts: 1732
Joined: 30 Mar 2005 09:43

Re: Freeing NULL pointers...

Post by peter1138 »

Hmm, even K&R's C book states that free(NULL) should just do nothing... Where do these implementors get their ideas from...
He's like, some kind of OpenTTD developer.
PhilSophus
Chairman
Chairman
Posts: 776
Joined: 20 Jan 2007 12:08
Location: Germany

Re: Freeing NULL pointers...

Post by PhilSophus »

stevenh wrote:unless someone knows how to inline a free() func that has the same name as the free() func that you then want to call :)
I guess your archaic environment doesn't support dynamic linking, does it? Otherwise, you could maybe provide your own implementation before the default libc (or however this is called in your environment) is loaded. E.g. on Linux this is used by memory leak checkers (LD_PRELOAD is the keyword here).

You may also just try to provide your own free implementation and hope that it does not try to take the one from the libraries then. However, I don't see how you could access the original implementation then. And writing your own memory management is probably nothing you would like to do :D

As an easy but dirty solution you could try this line in a header that is included anywhere you use free:

Code: Select all

#define free(x) safe_free(x)
"The bigger the island of our knowledge, the longer the shore of our ignorance" - John A. Wheeler, Physicist, 1911-2008
peter1138
OpenTTD Developer
OpenTTD Developer
Posts: 1732
Joined: 30 Mar 2005 09:43

Re: Freeing NULL pointers...

Post by peter1138 »

in your OS specific bit in stdafx.h or something...

Code: Select all

static inline safe_free(x)
{
   if (x != null) free(x);
}

#define free(x) safe_free(x)
maybe?
He's like, some kind of OpenTTD developer.
DaleStan
TTDPatch Developer
TTDPatch Developer
Posts: 10285
Joined: 18 Feb 2004 03:06
Contact:

Re: Freeing NULL pointers...

Post by DaleStan »

peter1138 wrote:Hmm, even K&R's C book states that free(NULL) should just do nothing.
Indeed. free(NULL);, delete NULL;, and delete[] NULL; are all no-ops.
Complain to whoever wrote your C library (or upgrade); it's non-conformant.

And while you're waiting for a fix for that, please go fix aRV refitting in Patch? It still only refits the first vehicle.
To get a good answer, ask a Smart Question. Similarly, if you want a bug fixed, write a Useful Bug Report. No TTDPatch crashlog? Then follow directions.
Projects: NFORenum (download) | PlaneSet (Website) | grfcodec (download) | grfdebug.log parser
User avatar
stevenh
TTDPatch Developer
TTDPatch Developer
Posts: 759
Joined: 24 Jul 2005 05:07
Location: Canberra, Australia
Contact:

Re: Freeing NULL pointers...

Post by stevenh »

I was just about to write "Isn't that classed as an infinite loop...." until I realised that the definition of the new 'free' is after the usage of the old 'free'....

Quite interesting... I might just do this... Thank all...

Now to work out endian issues... opntitle.dat wont work unless I have TO_LE32X in the Savegame formats which means that the 'direct' hdr[0] Uint32 that it's pulling out is being read in the "wrong" direction...

I have a mouse cursor showing though :D
User avatar
prissi
Chief Executive
Chief Executive
Posts: 647
Joined: 15 Nov 2004 19:46
Location: Berlin, Germany
Contact:

Re: Freeing NULL pointers...

Post by prissi »

About the free stuff: At least the Atari ST did not supported free(0), since the runtime (like many other too) give it to the OS free, which choked on it.

Aparently there are different interpretations of K&R delete NULL around too: I know of at least three compiler/development systems, where this would cause a warning or a breakpoint. (Albeit nothing that cannot be fixed by a define ... )

By the way, I think here is a confusion between K&R and ANSI. PDP-7 C (which was directly from K&R more or less) did not allowed free(NULL), it caused an error. At least it did when I did my first EDV courses.
I like to look at great maps and see how things flow. A little like a finished model railway, but it is evolving and actually never finished. http://www.simutrans.com
egladil
OpenTTD Developer
OpenTTD Developer
Posts: 188
Joined: 07 Nov 2005 17:10
Location: Sweden

Re: Freeing NULL pointers...

Post by egladil »

Loading of opntitle.dat currently works fine both on big endian and little endian machines. Most likely the target endian was not detected correctly. You can use the --endian configure parameter to override this.
User avatar
stevenh
TTDPatch Developer
TTDPatch Developer
Posts: 759
Joined: 24 Jul 2005 05:07
Location: Canberra, Australia
Contact:

Re: Freeing NULL pointers...

Post by stevenh »

A small update.... using peter1138's code worked until I attempted to flip endians and recompile everything... aystar.cpp uses a class function 'free' and seems to get this confused with the #define. This makes no sense to me, as the 'free' inside the class should not be overriden... for now I've renamed the class function.

Secondly... it seems that the libraries I am using for my system bring the CPU up as big-endian but the application that creates the romdisk writes the files as little endian... I reversed the readbyte functions in saveload.cpp, but things still seem a little weird... I'll screw around with it more once I get the time.
PhilSophus
Chairman
Chairman
Posts: 776
Joined: 20 Jan 2007 12:08
Location: Germany

Re: Freeing NULL pointers...

Post by PhilSophus »

stevenh wrote:A small update.... using peter1138's code worked until I attempted to flip endians and recompile everything... aystar.cpp uses a class function 'free' and seems to get this confused with the #define. This makes no sense to me, as the 'free' inside the class should not be overriden... for now I've renamed the class function.
That's why it is called a preprocessor. Preprocessor macros are substituted before the "real" compiling stage and thus ignore scoping completely and replace everything that matches. That is why you should avoid using preprocessor macros in C++ wherever possible (i.e. using inline functions or constants). Unfortunately, AFAICS this is one of the rare cases where there is no other way than using preprocessor macros. It's a hack anyway, so renaming the member function seems to be the best you can do about it.
"The bigger the island of our knowledge, the longer the shore of our ignorance" - John A. Wheeler, Physicist, 1911-2008
User avatar
Rainer
Traffic Manager
Traffic Manager
Posts: 240
Joined: 14 Nov 2007 10:01
Location: Wiesbaden, Germany

Re: Freeing NULL pointers...

Post by Rainer »

Hi,
PhilSophus wrote:
stevenh wrote:A small update.... using peter1138's code worked until I attempted to flip endians and recompile everything... aystar.cpp uses a class function 'free' and seems to get this confused with the #define. This makes no sense to me, as the 'free' inside the class should not be overriden... for now I've renamed the class function.
That's why it is called a preprocessor. Preprocessor macros are substituted before the "real" compiling stage and thus ignore scoping completely and replace everything that matches. That is why you should avoid using preprocessor macros in C++ wherever possible (i.e. using inline functions or constants). Unfortunately, AFAICS this is one of the rare cases where there is no other way than using preprocessor macros. It's a hack anyway, so renaming the member function seems to be the best you can do about it.
perhaps a little #undef will do it...

cu
Rainer
PhilSophus
Chairman
Chairman
Posts: 776
Joined: 20 Jan 2007 12:08
Location: Germany

Re: Freeing NULL pointers...

Post by PhilSophus »

Rainer wrote: perhaps a little #undef will do it...
That would soon going to get ugly as not only the definition of this free member is the problem. The calls to the member are problematic as well. You would have to be really careful where the #define should be in effect an where not. I think renaming is the cleaner solution (esp. easier to maintain).
"The bigger the island of our knowledge, the longer the shore of our ignorance" - John A. Wheeler, Physicist, 1911-2008
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: Ahrefs [Bot] and 44 guests