Main Restorations Software Audio/Jukebox/MP3 Everything Else Buy/Sell/Trade
Project Announcements Monitor/Video GroovyMAME Merit/JVL Touchscreen Meet Up Retail Vendors
Driving & Racing Woodworking Software Support Forums Consoles Project Arcade Reviews
Automated Projects Artwork Frontend Support Forums Pinball Forum Discussion Old Boards
Raspberry Pi & Dev Board controls.dat Linux Miscellaneous Arcade Wiki Discussion Old Archives
Lightguns Arcade1Up Try the site in https mode Site News

Unread posts | New Replies | Recent posts | Rules | Chatroom | Wiki | File Repository | RSS | Submit news

  

Author Topic: ledutil.c memory leak?  (Read 2369 times)

0 Members and 1 Guest are viewing this topic.

headkaze

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2943
  • Last login:August 14, 2023, 02:00:48 am
  • 0x2b|~0x2b?
ledutil.c memory leak?
« on: June 29, 2008, 08:46:47 pm »
I think there is a memory leak in the Mame.dll file which is used in a few applications developed by people on here (including LEDBlinky, CPWizard, Mame Hooker and PluginLCD). Mame.dll is based on the ledutil.c code in Mame. I just need someone to confirm it for me. Someone with some C experience obviously.

Take a look in ledutil.c (located in src\osd\windows) you will see in the function handle_copydata() the following two lines

Code: [Select]
// allocate memory
entry = malloc(sizeof(*entry));
string = malloc(strlen(data->string) + 1);

The "string" pointer is later assigned to entry->name and entry is added to a linked list. When Mame closes, this linked list is cleaned up in reset_id_to_outname_cache() like so

Code: [Select]
while (idmaplist != NULL)
{
id_map_entry *temp = idmaplist;
idmaplist = temp->next;
free(temp);
}

Above free(temp) there needs to be a free(entry->name) right?
« Last Edit: June 29, 2008, 08:53:50 pm by headkaze »

fatfingers

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 966
  • Last login:April 17, 2025, 05:26:08 pm
  • Got UltraStiks?™
Re: ledutil.c memory leak?
« Reply #1 on: June 29, 2008, 09:00:23 pm »


Yes, if that is the only code-path for cleanup, then there is a memory leak there.  Note, however, if this is only ever executed by MAME shutdown code, then it's probably not a big deal as the OS will 'free' the memory when the application (MAME) exits.

My DK low scores
-------------------
1) 180700
2) 165000
3) 162900
4) 162600
5) 158500


W.W.P.M.D.?                                       I'm here to help ... I just don't do it. ™

headkaze

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2943
  • Last login:August 14, 2023, 02:00:48 am
  • 0x2b|~0x2b?
Re: ledutil.c memory leak?
« Reply #2 on: June 29, 2008, 09:23:34 pm »
Yes, if that is the only code-path for cleanup, then there is a memory leak there.  Note, however, if this is only ever executed by MAME shutdown code, then it's probably not a big deal as the OS will 'free' the memory when the application (MAME) exits.

Yeah I thought so, and the thing is this code is designed to run all the time in applications like CPWizard and LEDBlinky so it is a problem because Mame can be launched hundreds of times while this code is still running, and each time the name string is allocating more memory that isn't free'd. So yes this is definately a problem. Thanks for confirming this, I was pretty sure it was a leak but was reluctant to second guess Aaron's code lol

TheShanMan

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 1912
  • Last login:October 22, 2024, 11:51:12 am
    • My Arcade (updated 1/30/13)
Re: ledutil.c memory leak?
« Reply #3 on: June 29, 2008, 11:03:08 pm »
I just looked at ledutil.c as well. Definitely a leak. Good catch. Be sure to let Aaron or whoever know so it gets fixed in future releases.
My Collection: Mame cab, 38 dedicated vids, pin, skeeball, coin op air hockey table, Ice Cold Beer, Megatouch, 2 token machines, and payphone (VAPS, pics at Arcade Crusade)

Add Ambience to your mame cab setup

Cakemeister

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 1002
  • Last login:May 31, 2024, 06:23:16 pm
  • I'm a llama!
Re: ledutil.c memory leak?
« Reply #4 on: June 30, 2008, 09:22:11 am »
Old, but not obsolete.

headkaze

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2943
  • Last login:August 14, 2023, 02:00:48 am
  • 0x2b|~0x2b?
Re: ledutil.c memory leak?
« Reply #5 on: June 30, 2008, 07:58:29 pm »
I just want to clarify something before I report it as a bug, here is the definition of struct _id_map_entry

Code: [Select]
struct _id_map_entry
{
id_map_entry * next;
const char * name;
WPARAM id;
};

When I add in the free(entry->name) I get the following errors

Quote
src\osd\windows\ledutil.c(416) : error C2220: warning treated as error - no 'object' file generated
src\osd\windows\ledutil.c(416) : warning C4090: 'function' : different 'const' qualifiers
mingw32-make: *** [obj/windows/vmame/osd/windows/ledutil.o] Error 2

Should I be changing that to free((void*)entry->name);? Why is it a "const char *" instead of just a "char *"? Does it make a difference to free()?
« Last Edit: June 30, 2008, 08:18:41 pm by headkaze »

fatfingers

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 966
  • Last login:April 17, 2025, 05:26:08 pm
  • Got UltraStiks?™
Re: ledutil.c memory leak?
« Reply #6 on: June 30, 2008, 08:18:25 pm »
The compiler is just being picky.  Your fix is fine.  The only way the fix might not be fine is if that memory wasn't truly dynamically allocated memory (i.e. it came from the stack somewhere), but from the example you have shown, that isn't the case.


My DK low scores
-------------------
1) 180700
2) 165000
3) 162900
4) 162600
5) 158500


W.W.P.M.D.?                                       I'm here to help ... I just don't do it. ™

headkaze

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2943
  • Last login:August 14, 2023, 02:00:48 am
  • 0x2b|~0x2b?
Re: ledutil.c memory leak?
« Reply #7 on: June 30, 2008, 08:20:36 pm »
Okay thanks fatfingers, but I think the cast to remove the warning is better. free((void*)entry->name); it is.

EDIT: Okay I submitted a diff to mamedev
« Last Edit: June 30, 2008, 08:56:53 pm by headkaze »

TheShanMan

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 1912
  • Last login:October 22, 2024, 11:51:12 am
    • My Arcade (updated 1/30/13)
Re: ledutil.c memory leak?
« Reply #8 on: June 30, 2008, 10:39:07 pm »
Just to confirm what fatfingers said (I've examined the code while I think he's just gone by what you've posted here)... It's const just to make sure that after the string gets put into the structure, nothing else modifies it. The const part is all the compiler is upset about. It can free it as a char* or a void*, and there's absolutely nothing wrong with that. free() doesn't care what kind of pointer it is - only that it was allocated with malloc(). Your solution is the correct one.

And thanks for posting about this - I use the same code in a project I'm working on so I've benefited from your discovery.
My Collection: Mame cab, 38 dedicated vids, pin, skeeball, coin op air hockey table, Ice Cold Beer, Megatouch, 2 token machines, and payphone (VAPS, pics at Arcade Crusade)

Add Ambience to your mame cab setup

headkaze

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2943
  • Last login:August 14, 2023, 02:00:48 am
  • 0x2b|~0x2b?
Re: ledutil.c memory leak?
« Reply #9 on: June 30, 2008, 11:31:23 pm »
I've never had the need to use a const modifier for pointers before so that's what threw me. Mamedev specifically ask that there be no warnings for the code included in the diff's, so wanted to clarify it.

Appreciate the feedback (didn't manage to get any help at mameworld)  :cheers:
« Last Edit: June 30, 2008, 11:33:20 pm by headkaze »

fatfingers

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 966
  • Last login:April 17, 2025, 05:26:08 pm
  • Got UltraStiks?™
Re: ledutil.c memory leak?
« Reply #10 on: July 01, 2008, 08:10:07 am »

I concur with TheShanMan's comments.

My DK low scores
-------------------
1) 180700
2) 165000
3) 162900
4) 162600
5) 158500


W.W.P.M.D.?                                       I'm here to help ... I just don't do it. ™

headkaze

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2943
  • Last login:August 14, 2023, 02:00:48 am
  • 0x2b|~0x2b?
Re: ledutil.c memory leak?
« Reply #11 on: July 03, 2008, 07:32:46 pm »
This has been fixed in MAME 0.125u9 :)

brian_hoffman

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 131
  • Last login:July 02, 2011, 09:02:20 pm
Re: ledutil.c memory leak?
« Reply #12 on: July 03, 2008, 07:40:03 pm »
Yes it was.. I also see your name in the credits in (whats new)
Good job  :cheers: