Re: threads and malloc/free on freebsd 8.0
In the last episode (Jun 11), Vikash Badal said: I have a thread socket application that seems to be behaving strangely In a worker thread, I have the following. CODE--- LogMessage(DEBUG_0, allocated %ld, malloc_usable_size(inst)); free(inst); LogMessage(DEBUG_0, after free allocated %ld, malloc_usable_size(inst)); free(inst); return 0; ---/CODE output allocated 2304 output after free allocated 2304 from playing around, this should have segfaulted but it didn't: if I try this from a non threaded, non socket code: CODE-- char *z; z = (char*)malloc(1000); printf(malloc is %ld\n, malloc_usable_size(z)); free(z); printf(after malloc is %ld\n, malloc_usable_size(z)); --/CODE Output malloc is 1024 Output Segmentation fault (core dumped) Can anyone enlighten me ? why did the 2nd free not cause a segmentation fault ? You asked this same question on May 24: http://lists.freebsd.org/pipermail/freebsd-questions/2010-May/216652.html The answer is still the same: You're invoking undefined behaviour here by calling malloc_usable_size on a free'd pointer. The function is free to crash, return useful data, or return useless data, at its discretion :) The fix is to remove your second call to malloc_usable_size(z)). Then neither version will crash. Also, a useful habit to start is to explicitly zero the pointer you just free'd, to prevent it from being used accidentally later. -- Dan Nelson dnel...@allantgroup.com ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
RE: threads and malloc/free on freebsd 8.0
-Original Message- From: owner-freebsd-questi...@freebsd.org [mailto:owner-freebsd- questi...@freebsd.org] On Behalf Of Dan Nelson Sent: 11 June 2010 09:56 PM To: Vikash Badal Cc: freebsd-questions@freebsd.org Subject: Re: threads and malloc/free on freebsd 8.0 The fix is to remove your second call to malloc_usable_size(z)). Then neither version will crash. Also, a useful habit to start is to explicitly zero the pointer you just free'd, to prevent it from being used accidentally later. Made this change: CODE--- LogMessage(DEBUG_0, allocated %ld, malloc_usable_size(inst)); free(inst); free(inst); return 0; ---/CODE Still no seg fault. The reason im am doing this is that from top I can see the memory grow as I connect to this app. When I disconnect, the memory used ( as displayed from top ) does not decrease. I tried: CODE- char *chunk; chunk = (char*) malloc(120); sleep(30); free(chunk) sleep(30); free(inst); free(inst); --/CODE Top show the memory for the chunk section increase and then decrease when freed However, the when I leave the worker thread ( close the connection ), the memory usage does not decrease. The more connections I open and close, the faster the memory grows. -- Dan Nelson dnel...@allantgroup.com ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions- unsubscr...@freebsd.org Please note: This email and its content are subject to the disclaimer as displayed at the following link http://www.is.co.za/legal/E-mail+Confidentiality+Notice+and+Disclaimer.htm. Should you not have Web access, send a mail to disclaim...@is.co.za and a copy will be emailed to you. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: threads and malloc/free on freebsd 8.0
In the last episode (Jun 11), Vikash Badal said: -Original Message- From: owner-freebsd-questi...@freebsd.org [mailto:owner-freebsd- The fix is to remove your second call to malloc_usable_size(z)). Then neither version will crash. Also, a useful habit to start is to explicitly zero the pointer you just free'd, to prevent it from being used accidentally later. Made this change: CODE--- LogMessage(DEBUG_0, allocated %ld, malloc_usable_size(inst)); free(inst); free(inst); return 0; ---/CODE Still no seg fault. The reason im am doing this is that from top I can see the memory grow as I connect to this app. When I disconnect, the memory used ( as displayed from top ) does not decrease. For performance reasons, the malloc library may not immediately release free memory back to the OS. Also, depending on the position of the memory block being freed, it may not be releasable back to the OS (if malloc used sbrk for allocations, it can only free memory at the far end of the address space, for example). Setting the environment variable MALLOC_OPTIONS to dM10f might help (disable use of sbrk(), enable use of mmap(), don't cache any free pages), but you still may not see any change according to top. -- Dan Nelson dnel...@allantgroup.com ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: threads and malloc/free on freebsd 8.0
On Sat, May 22, 2010 at 1:56 AM, Dan Nelson dnel...@allantgroup.com wrote: In the last episode (May 22), Anoop Kumar Narayanan said: I think glibc uses asynchronous free, as in it doesn't free the memory immediately. So even though the memory is free'd its still part of the process's address space but present in the free pool and so it doesn't crash. FreeBSD doesn't use glibc, so that doesn't apply here :) :) Oh yeah, BSD has its own libc -- Dan Nelson dnel...@allantgroup.com ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: threads and malloc/free on freebsd 8.0
In the last episode (May 21), Vikash Badal said: Excuse me if this is a stupid questions. I have a thread socket application that seems to be behaving strangely In a worker thread, I have the following. CODE--- LogMessage(DEBUG_0, allocated %ld, malloc_usable_size(inst)); free(inst); LogMessage(DEBUG_0, after free allocated %ld, malloc_usable_size(inst)); return 0; ---/CODE output allocated 2304 output after free allocated 2304 from playing around, this should have segfaulted but it didn't You're invoking undefined behaviour here by calling malloc_usable_size on a free'd pointer. The function is free to crash, return useful data, or return useless data, at its discretion :) As long as you only call it on pointers that are still valid you will be okay. -- Dan Nelson dnel...@allantgroup.com ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: threads and malloc/free on freebsd 8.0
I think glibc uses asynchronous free, as in it doesn't free the memory immediately. So even though the memory is free'd its still part of the process's address space but present in the free pool and so it doesn't crash. -Anoop On Sat, May 22, 2010 at 12:48 AM, Dan Nelson dnel...@allantgroup.com wrote: In the last episode (May 21), Vikash Badal said: Excuse me if this is a stupid questions. I have a thread socket application that seems to be behaving strangely In a worker thread, I have the following. CODE--- LogMessage(DEBUG_0, allocated %ld, malloc_usable_size(inst)); free(inst); LogMessage(DEBUG_0, after free allocated %ld, malloc_usable_size(inst)); return 0; ---/CODE output allocated 2304 output after free allocated 2304 from playing around, this should have segfaulted but it didn't You're invoking undefined behaviour here by calling malloc_usable_size on a free'd pointer. The function is free to crash, return useful data, or return useless data, at its discretion :) As long as you only call it on pointers that are still valid you will be okay. -- Dan Nelson dnel...@allantgroup.com ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: threads and malloc/free on freebsd 8.0
In the last episode (May 22), Anoop Kumar Narayanan said: I think glibc uses asynchronous free, as in it doesn't free the memory immediately. So even though the memory is free'd its still part of the process's address space but present in the free pool and so it doesn't crash. FreeBSD doesn't use glibc, so that doesn't apply here :) -- Dan Nelson dnel...@allantgroup.com ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org