lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Bingo. new/delete and malloc/free do NOT play well together... they
aren't interchangeable in the slightest under most runtime libraries.


On Wed, 20 Oct 2004 03:59:55 +0200, Nils Wogatzky <nils@wogatzky.com> wrote:
> First of all thank you for your quick reply.
> 
> Do I mix malloc with delete ?
> 
> [Code]
> ...
> savObjects = (T**)malloc(Size * sizeof(Objects));
> ...
> delete [] savObjects;
> ...
> [/Code]
> 
> like this ?
> 
> Then I do, yes. I´m not that good in C++ =)
> 
> To your second question ("..Use of objects..") I can reply: I just use function-calls.
> 
> I´ll try to find a memory-debugging tool to fix the problem.
> (After that I´ll read some C++ - Tutorials and find out why I shouldn´t use malloc and delete;-) )
> 
> Thank you
> 
> 
> 
> 
> >Hi,
> >
> >first of all, welcome to Lua.
> >
> >On Tuesday 19 October 2004 09:12 pm, Nils Wogatzky wrote:
> >> I already tried to debug, but in Debug-Mode everything works fine.
> >>
> >> At least when I just cut-out or change the position of some lines in my
> >> script my application works again.
> >> This makes no sense, because it already did work well,
> >> why should i change the position of those Calls ?
> >
> >I feel your pain. All these symptoms are indicative of a buffer overflow
> >somewhere in your program, or some other form of heap corruption (i.e. you
> >write past the end of an array, you free dynamically allocated memory twice,
> >you mix new with free() or malloc() with delete, etc. Unfortunately, the
> >cause and the location of the crash are often completely unrelated, so I am
> >afraid that finding the bug may require a long exercise in patience.
> >
> >The reason why the behavior of your bug changes in debug mode, or when you
> >move around lines in the lua script, is that it changes the memory allocation
> >patterns of your program, and the heap corruption will consequently not
> >manifest, or in a different place. I've been there and done that, and the
> >only advice that I can give you is: Use a memory debugging tool, such as
> >Purify on Windows, Valgrind on Linux, etc. With any luck, it will pinpoint
> >exactly what is going wrong in your program.
> >
> >> Do I have some trouble with LUA or with my use of memory ? ;-)
> >
> >Most likely your use of memory, but it won't hurt to rule out a few things.
> >First, do you store a Lua object past the point when your called C++ function
> >returns? If yes, you need to look at your use of lua_ref(). Second, do you
> >possibly store a reference to a C++ object from within Lua, and it gets
> >deallocated, garbage-collected, or such like?
> >
> >Good luck
> >- Christian
> >
>