Re: Freeing a record which contains a string field leads to a memory/string leak ?!

From: Skybuck Flying (nospam_at_hotmail.com)
Date: 02/21/04


Date: Sat, 21 Feb 2004 03:06:15 +0100


> > But but but but but this raising a somewhat little interesting
> > question if you guys also new a 'legal' trick to keep the string
> > alive but so that delphi does destroy the string when the pointer
> > goes dead or is reassigned something else whatever.
>
> There is no legal solution to the problem you posed. You're giving P a
> pointer to a stack variable. As soon as the procedure returns, that
> pointer will be invalid. The fact that P is pointing to a *string*
> variable is totally irrelevant. If you want the pointer to remain valid
> after the procedure returns, then you need to make it point to memory
> allocated on the heap, not on the stack.

P is not on the stack, P is outside the procedure. I think you mean the
string should be allocated on the heap.

How does one allocate a string on the heap ?



Relevant Pages

  • Re: String to byte[] reloaded
    ... Indeed, most of them don't allocate, but relies on you to allocate. ... I am using .NET framework v1.1 and I need to compress my string. ... With a one million character string, there is no change in the pointer, and the AddrOfPinnedObject call still includes the correct offset. ... Probably small objects get moved to the Large Object Heap in order to pin them. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: This is getting really weird.
    ... I thought 4 bytes for reference count and 4 for string length. ... > There should be no memory allocation for that line. ... > manager may allocate more space than requested for its own efficiency. ... > that New returned with a pointer to the string constant. ...
    (alt.comp.lang.borland-delphi)
  • Re: Python Memory Manager
    ... heap, the current heap size, and the beginning of the free memory. ... When you need to 'allocate' more room, just return a pointer to some ... which typically involves copying live data. ...
    (comp.lang.python)
  • Re: String to byte[] reloaded
    ... Indeed, most of them don't allocate, but relies on you to allocate. ... I am using .NET framework v1.1 and I need to compress my string. ... Note that the Large Object Heap is a heap, ... I don't know why the offset isn't needed when you use a pinning pointer. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: alloca / _alloca / dynamic stack memory
    ... that will be copied and set to the internal char*. ... By allocating an instance of String class using new ... of const char* to be stored in our internal char* pointer. ... If a heap has been allocated on a per-thread basis, ...
    (microsoft.public.vc.language)

Loading