diff --git a/makefile.vc b/makefile.vc index 7541a0de8..bb9914706 100644 --- a/makefile.vc +++ b/makefile.vc @@ -46,6 +46,11 @@ else LIBS += Alleg.lib endif +ifdef MEMLEAK + CFLAGS += -DMEMLEAK + LIBS += dbghelp.lib +endif + ###################################################################### # General rules diff --git a/src/jinete/jaccel.cpp b/src/jinete/jaccel.cpp index 4af49fdc2..db7a44c61 100644 --- a/src/jinete/jaccel.cpp +++ b/src/jinete/jaccel.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include #include diff --git a/src/jinete/jalert.cpp b/src/jinete/jalert.cpp index 585ef355c..799f3f163 100644 --- a/src/jinete/jalert.cpp +++ b/src/jinete/jalert.cpp @@ -53,6 +53,8 @@ `-----~---------~-------------~-----------' */ +#include "config.h" + #include #include #include diff --git a/src/jinete/jbox.cpp b/src/jinete/jbox.cpp index 7c5e29ed7..45ccbe1a0 100644 --- a/src/jinete/jbox.cpp +++ b/src/jinete/jbox.cpp @@ -31,6 +31,8 @@ /* Based on code from GTK+ 2.1.2 (gtk+/gtk/gtkhbox.c) */ +#include "config.h" + #include "jinete/jlist.h" #include "jinete/jmessage.h" #include "jinete/jrect.h" diff --git a/src/jinete/jbutton.cpp b/src/jinete/jbutton.cpp index 3d4407d15..bfa0dfc3d 100644 --- a/src/jinete/jbutton.cpp +++ b/src/jinete/jbutton.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include #include diff --git a/src/jinete/jclipboard.cpp b/src/jinete/jclipboard.cpp index 53763dee8..d636bcbaa 100644 --- a/src/jinete/jclipboard.cpp +++ b/src/jinete/jclipboard.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include "jinete/jbase.h" static char *clipboard_text = NULL; diff --git a/src/jinete/jcombobox.cpp b/src/jinete/jcombobox.cpp index e65a5cb7b..a4b28788e 100644 --- a/src/jinete/jcombobox.cpp +++ b/src/jinete/jcombobox.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include "jinete/jinete.h" diff --git a/src/jinete/jdraw.cpp b/src/jinete/jdraw.cpp index d05a85a9d..8fa7fbdbf 100644 --- a/src/jinete/jdraw.cpp +++ b/src/jinete/jdraw.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include #include diff --git a/src/jinete/jentry.cpp b/src/jinete/jentry.cpp index f73bf4b50..f218d881d 100644 --- a/src/jinete/jentry.cpp +++ b/src/jinete/jentry.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include #include diff --git a/src/jinete/jfile.cpp b/src/jinete/jfile.cpp index df1d8c4a3..930045505 100644 --- a/src/jinete/jfile.cpp +++ b/src/jinete/jfile.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include #include diff --git a/src/jinete/jfilesel.cpp b/src/jinete/jfilesel.cpp index 09d5fce1a..94734affb 100644 --- a/src/jinete/jfilesel.cpp +++ b/src/jinete/jfilesel.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include #include diff --git a/src/jinete/jfont.cpp b/src/jinete/jfont.cpp index c8038a13e..4d2207028 100644 --- a/src/jinete/jfont.cpp +++ b/src/jinete/jfont.cpp @@ -31,6 +31,8 @@ /* Based on code of AllegroFont, Copyright (c) 2001, 2002 Javier Gonzalez */ +#include "config.h" + #include #include #include diff --git a/src/jinete/jfontbmp.cpp b/src/jinete/jfontbmp.cpp index 630df05b4..5f092df40 100644 --- a/src/jinete/jfontbmp.cpp +++ b/src/jinete/jfontbmp.cpp @@ -34,6 +34,8 @@ allegro/tools/plugins/datfont.c */ +#include "config.h" + #include #include diff --git a/src/jinete/jgrid.cpp b/src/jinete/jgrid.cpp index 1da27acce..c1c3c0744 100644 --- a/src/jinete/jgrid.cpp +++ b/src/jinete/jgrid.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include #include diff --git a/src/jinete/jhook.cpp b/src/jinete/jhook.cpp index c656a6932..83d8e1d3b 100644 --- a/src/jinete/jhook.cpp +++ b/src/jinete/jhook.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include "jinete/jhook.h" /** diff --git a/src/jinete/jimage.cpp b/src/jinete/jimage.cpp index 721097e57..6da17e893 100644 --- a/src/jinete/jimage.cpp +++ b/src/jinete/jimage.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include diff --git a/src/jinete/jintern.cpp b/src/jinete/jintern.cpp index 3459b314a..441004362 100644 --- a/src/jinete/jintern.cpp +++ b/src/jinete/jintern.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include diff --git a/src/jinete/jlabel.cpp b/src/jinete/jlabel.cpp index b5b3f1a84..013482e09 100644 --- a/src/jinete/jlabel.cpp +++ b/src/jinete/jlabel.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include "jinete/jmessage.h" #include "jinete/jtheme.h" #include "jinete/jwidget.h" diff --git a/src/jinete/jlist.cpp b/src/jinete/jlist.cpp index dedb33e16..8ec80cc04 100644 --- a/src/jinete/jlist.cpp +++ b/src/jinete/jlist.cpp @@ -31,6 +31,8 @@ /* #define USE_JUNKLIST */ +#include "config.h" + #include "jinete/jlist.h" #ifdef USE_JUNKLIST /* TODO warning not thread safe */ diff --git a/src/jinete/jlistbox.cpp b/src/jinete/jlistbox.cpp index 7c79f26ce..f22cc06eb 100644 --- a/src/jinete/jlistbox.cpp +++ b/src/jinete/jlistbox.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include "jinete/jlist.h" diff --git a/src/jinete/jmanager.cpp b/src/jinete/jmanager.cpp index d392775a5..7f0eb1234 100644 --- a/src/jinete/jmanager.cpp +++ b/src/jinete/jmanager.cpp @@ -32,6 +32,8 @@ /* #define REPORT_EVENTS */ /* #define LIMIT_DISPATCH_TIME */ +#include "config.h" + #ifdef REPORT_EVENTS #include #endif diff --git a/src/jinete/jmem.cpp b/src/jinete/jmem.cpp index 52461c4c9..71763cc55 100644 --- a/src/jinete/jmem.cpp +++ b/src/jinete/jmem.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include #include @@ -76,44 +78,130 @@ char *jstrdup(const char *string) #else -/**********************************************************************/ -/* with leak detection */ -/**********************************************************************/ +////////////////////////////////////////////////////////////////////// +// With leak detection + +#define BACKTRACE_LEVELS 16 + +#if defined _MSC_VER + +#include +#include + +// This is an implementation of the __builtin_return_address GCC +// extension for the MSVC compiler. +// +// Author: Unknown +// Modified by David Capello to return NULL when the callstack +// is not as high as the specified "level". +// +__declspec (naked) void* __builtin_return_address(int level) +{ + __asm + { + push ebx + + mov eax, ebp + mov ebx, DWORD PTR [esp + 8] // level +__next: + test ebx, ebx + je __break + dec ebx + mov eax, DWORD PTR [eax] + test eax, eax + je __done + jmp __next +__break: + mov eax, DWORD PTR [eax + 4] +__done: + pop ebx + ret + } +} + +#endif typedef struct slot_t { - void* backtrace[4]; + void* backtrace[BACKTRACE_LEVELS]; void* ptr; unsigned long size; struct slot_t* next; } slot_t; +static bool memleak_status = false; static slot_t* headslot; static JMutex mutex; void jmemleak_init() { + assert(!memleak_status); + headslot = NULL; mutex = jmutex_new(); + + memleak_status = true; } void jmemleak_exit() { + assert(memleak_status); + memleak_status = false; + FILE* f = fopen("_ase_memlog.txt", "wt"); slot_t* it; - + if (f != NULL) { - /* memory leaks */ +#ifdef _MSC_VER + struct SYMBOL_INFO_EX { + SYMBOL_INFO header; + char filename[512]; + } si; + si.header.SizeOfStruct = sizeof(SYMBOL_INFO_EX); + + IMAGEHLP_LINE line; + line.SizeOfStruct = sizeof(IMAGEHLP_LINE); + + HANDLE hproc = ::GetCurrentProcess(); + ::SymInitialize(hproc, NULL, FALSE); + + char filename[MAX_PATH]; + ::GetModuleFileName(NULL, filename, sizeof filename); + ::SymLoadModule(hproc, NULL, filename, NULL, 0, 0); + + char path[MAX_PATH]; + strcpy(path, filename); + if (strrchr(path, '\\')) + *strrchr(path, '\\') = 0; + else + *path = 0; + ::SymSetSearchPath(hproc, path); +#endif + + // Memory leaks for (it=headslot; it!=NULL; it=it->next) { - fprintf(f, - "Leak:\n%p\n%p\n%p\n%p\nptr: %p, size: %lu\n", - it->backtrace[0], - it->backtrace[1], - it->backtrace[2], - it->backtrace[3], - it->ptr, it->size); + fprintf(f, "\nLEAK address: %p, size: %lu\n", it->ptr, it->size); + + for (int c=0; cbacktrace[c], &displacement, &line) && + ::SymFromAddr(hproc, (DWORD)it->backtrace[c], NULL, &si.header)) { + fprintf(f, "%p : %s(%lu) [%s]\n", + it->backtrace[c], + line.FileName, line.LineNumber, + si.header.Name); + } + else +#endif + fprintf(f, "%p\n", it->backtrace[c]); + } } fclose(f); + +#ifdef _MSC_VER + ::SymCleanup(hproc); +#endif } jmutex_free(mutex); @@ -121,19 +209,22 @@ void jmemleak_exit() static void addslot(void *ptr, unsigned long size) { + if (!memleak_status) + return; + slot_t* p = reinterpret_cast(malloc(sizeof(slot_t))); assert(ptr != NULL); assert(size != 0); - p->backtrace[0] = __builtin_return_address(4); /* a GCC extension */ - p->backtrace[1] = __builtin_return_address(3); - p->backtrace[2] = __builtin_return_address(2); - p->backtrace[3] = __builtin_return_address(1); + // __builtin_return_address is a GCC extension + for (int c=0; cbacktrace[c] = __builtin_return_address(BACKTRACE_LEVELS-c); + p->ptr = ptr; p->size = size; p->next = headslot; - + jmutex_lock(mutex); headslot = p; jmutex_unlock(mutex); @@ -141,6 +232,9 @@ static void addslot(void *ptr, unsigned long size) static void delslot(void *ptr) { + if (!memleak_status) + return; + slot_t *it, *prev = NULL; assert(ptr != NULL); diff --git a/src/jinete/jmenu.cpp b/src/jinete/jmenu.cpp index a05bc9adb..0ebe99bab 100644 --- a/src/jinete/jmenu.cpp +++ b/src/jinete/jmenu.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include #include diff --git a/src/jinete/jmessage.cpp b/src/jinete/jmessage.cpp index e951a9596..3d529697d 100644 --- a/src/jinete/jmessage.cpp +++ b/src/jinete/jmessage.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include #include diff --git a/src/jinete/jmutex.cpp b/src/jinete/jmutex.cpp index 83a5a98fa..e9fd08126 100644 --- a/src/jinete/jmutex.cpp +++ b/src/jinete/jmutex.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include diff --git a/src/jinete/jpanel.cpp b/src/jinete/jpanel.cpp index 39421afff..c6d451922 100644 --- a/src/jinete/jpanel.cpp +++ b/src/jinete/jpanel.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include "jinete/jlist.h" #include "jinete/jmessage.h" #include "jinete/jrect.h" diff --git a/src/jinete/jquickmenu.cpp b/src/jinete/jquickmenu.cpp index a904eeddc..3b175996d 100644 --- a/src/jinete/jquickmenu.cpp +++ b/src/jinete/jquickmenu.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include "jinete/jaccel.h" #include "jinete/jlist.h" #include "jinete/jmenu.h" diff --git a/src/jinete/jrect.cpp b/src/jinete/jrect.cpp index 67564f6e3..3e75e5682 100644 --- a/src/jinete/jrect.cpp +++ b/src/jinete/jrect.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include "jinete/jrect.h" JRect jrect_new(int x1, int y1, int x2, int y2) diff --git a/src/jinete/jregion.cpp b/src/jinete/jregion.cpp index 32d522189..faa77bb07 100644 --- a/src/jinete/jregion.cpp +++ b/src/jinete/jregion.cpp @@ -1,3 +1,36 @@ +/* Jinete - a GUI library + * Copyright (C) 2003-2009 David Capello. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of the author nor the names of its contributors may + * be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + #include #include diff --git a/src/jinete/jsep.cpp b/src/jinete/jsep.cpp index 9f46bd392..e7dc57bdc 100644 --- a/src/jinete/jsep.cpp +++ b/src/jinete/jsep.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include "jinete/jlist.h" #include "jinete/jmessage.h" #include "jinete/jrect.h" diff --git a/src/jinete/jslider.cpp b/src/jinete/jslider.cpp index 292c46f37..99e915234 100644 --- a/src/jinete/jslider.cpp +++ b/src/jinete/jslider.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include "jinete/jfont.h" diff --git a/src/jinete/jstream.cpp b/src/jinete/jstream.cpp index 6dbfcb952..ee9fee3e8 100644 --- a/src/jinete/jstream.cpp +++ b/src/jinete/jstream.cpp @@ -31,6 +31,8 @@ /* jstream_string is based on streams of HTMLEX */ +#include "config.h" + #include #include #include diff --git a/src/jinete/jstring.cpp b/src/jinete/jstring.cpp index 08295ba63..089cda1be 100644 --- a/src/jinete/jstring.cpp +++ b/src/jinete/jstring.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include #include diff --git a/src/jinete/jsystem.cpp b/src/jinete/jsystem.cpp index e6b61318e..db1fcafe2 100644 --- a/src/jinete/jsystem.cpp +++ b/src/jinete/jsystem.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include #ifdef ALLEGRO_WINDOWS diff --git a/src/jinete/jtextbox.cpp b/src/jinete/jtextbox.cpp index 285758c8c..f34dd4c20 100644 --- a/src/jinete/jtextbox.cpp +++ b/src/jinete/jtextbox.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include diff --git a/src/jinete/jtheme.cpp b/src/jinete/jtheme.cpp index 187c10cb1..2cac89be4 100644 --- a/src/jinete/jtheme.cpp +++ b/src/jinete/jtheme.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include diff --git a/src/jinete/jthread.cpp b/src/jinete/jthread.cpp index 24ac8ada5..b2a536d73 100644 --- a/src/jinete/jthread.cpp +++ b/src/jinete/jthread.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include diff --git a/src/jinete/jtooltips.cpp b/src/jinete/jtooltips.cpp index c18df5f42..8695e9f20 100644 --- a/src/jinete/jtooltips.cpp +++ b/src/jinete/jtooltips.cpp @@ -16,6 +16,8 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include "config.h" + #include #include diff --git a/src/jinete/jview.cpp b/src/jinete/jview.cpp index 927cc4afe..d89f313a5 100644 --- a/src/jinete/jview.cpp +++ b/src/jinete/jview.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include "jinete/jintern.h" #include "jinete/jlist.h" #include "jinete/jmessage.h" diff --git a/src/jinete/jwidget.cpp b/src/jinete/jwidget.cpp index 715166830..5e78af857 100644 --- a/src/jinete/jwidget.cpp +++ b/src/jinete/jwidget.cpp @@ -31,6 +31,8 @@ /* #define REPORT_SIGNALS */ +#include "config.h" + #include #include #include diff --git a/src/jinete/jwindow.cpp b/src/jinete/jwindow.cpp index 1085c4991..a2e64e05a 100644 --- a/src/jinete/jwindow.cpp +++ b/src/jinete/jwindow.cpp @@ -32,6 +32,8 @@ #define REDRAW_MOVEMENT #define MOTION_CURSOR JI_CURSOR_NORMAL +#include "config.h" + #include #include "jinete/jinete.h" diff --git a/src/jinete/jxml.cpp b/src/jinete/jxml.cpp index c76212101..0edb57806 100644 --- a/src/jinete/jxml.cpp +++ b/src/jinete/jxml.cpp @@ -29,6 +29,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "config.h" + #include #include #include diff --git a/src/widgets/editor/editor.cpp b/src/widgets/editor/editor.cpp index a367ffd9a..622f858c1 100644 --- a/src/widgets/editor/editor.cpp +++ b/src/widgets/editor/editor.cpp @@ -908,7 +908,7 @@ static bool editor_msg_proc(JWidget widget, JMessage msg) case JM_DESTROY: jmanager_remove_timer(editor->mask_timer_id); remove_editor(widget); - jfree(editor_data(widget)); + jfree(editor); break; case JM_REQSIZE: