Recent

Author Topic: FreeMem question  (Read 10422 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
FreeMem question
« on: June 27, 2010, 11:24:15 pm »
I own a string in memory allocated by a DLL written in C and assigned to a PChar the ShowMessage () I can see correct values. The only thing I can do is to free the memory area occupied. So I decided to use the passing FreeMem PChar but I SIGSERV returns an error. Does anyone know how to address the problem?  :)
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4698
  • I like bugs.
Re: FreeMem question
« Reply #1 on: June 28, 2010, 12:04:18 am »
You can't free the memory allocated by a DLL. The DLL must do it.

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: FreeMem question
« Reply #2 on: June 28, 2010, 12:14:10 am »
You can't free the memory allocated by a DLL. The DLL must do it.

Juha

Ok, I had thought about but I can not get it, I PChar the revision to a hypothetical function that is called

void LiberaStringa(char * variabile)
{
    free(variabile);
}

But it works!
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4698
  • I like bugs.
Re: FreeMem question
« Reply #3 on: June 28, 2010, 01:57:33 am »
void LiberaStringa(char * variabile)
{
    free(variabile);
}

If the function is in the DLL, then it works. Yes. It has a different memory manager and address space than your program.

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: FreeMem question
« Reply #4 on: June 28, 2010, 02:59:22 am »
void LiberaStringa(char * variabile)
{
    free(variabile);
}

If the function is in the DLL, then it works. Yes. It has a different memory manager and address space than your program.

Juha

I can not understand, but does not work, it is a dll that I'm writing with devcpp. And your last message is not clear!
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4698
  • I like bugs.
Re: FreeMem question
« Reply #5 on: June 28, 2010, 04:00:55 am »
I can not understand, but does not work, it is a dll that I'm writing with devcpp. And your last message is not clear!

Ok. From the previous message I understood it works.
Anyway, if I understand right, the memory allocated by a DLL must be freed by the same DLL. The memory allocated by your main program must be freed by your main program. You can't mix memory management between them.

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: FreeMem question
« Reply #6 on: June 28, 2010, 04:07:23 am »
Okay and I understand that I do not understand why but goes SIGSERV error when trying to deallocate!
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1222
    • Burdjia
Re: FreeMem question
« Reply #7 on: June 28, 2010, 05:16:35 am »
I don't understand that you understand that you don't understand... Oh, wait!

Seriously, Juha was very clear: Memory allocated by the DLL must be deallocated by the DLL same. Each DLL has it's own memory frame. This is what it is.
« Last Edit: June 28, 2010, 05:20:00 am by Ñuño_Martínez »
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

José Mejuto

  • Full Member
  • ***
  • Posts: 136
Re: FreeMem question
« Reply #8 on: June 29, 2010, 06:36:40 am »
I own a string in memory allocated by a DLL written in C and assigned to a PChar the ShowMessage () I can see correct values. The only thing I can do is to free the memory area occupied. So I decided to use the passing FreeMem PChar but I SIGSERV returns an error. Does anyone know how to address the problem?  :)

Hello,

Most DLLs written in C (in Windows) are being linked against msvcrt.dll which handles the memory management. You can try (if the memory allocated in such strings is high along the program life) to dynamic load that DLL and call the "free" method in this DLL. Load time link could work also, but I think it is better a dynamic approach.

type
  Tmsvcrt_free=procedure (P: pointer);

var
  c_free: Tmsvcrt_free;
begin
  h:=LoadLibrary('msvcrt.dll');
  c_free:=Tmsvcrt_free(GetProcAddress(h,'free'));
end;

c_free(mypcharvar);

I had not tested this, use at your own risk :)

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2674
Re: FreeMem question
« Reply #9 on: June 30, 2010, 12:43:03 am »
dlls requiring a string to be freed should export a procedure to do so. Never use your own memory manager for this. The method posted by JoshyFun may work, but in general you shouldn't rely on that.

@xinyiman:
Why do you think you need to free the string ?
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: FreeMem question
« Reply #10 on: June 30, 2010, 01:00:35 am »
because I allocated with malloc in dll.

We say that the problem does not exist anymore because I abandoned that way!
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: FreeMem question
« Reply #11 on: June 30, 2010, 10:00:06 pm »
Doesn't dll have finalization function? You can free your strings there (of course, you have to maintain the references)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12721
  • FPC developer.
Re: FreeMem question
« Reply #12 on: July 05, 2010, 11:37:40 am »
(sidenote: on *nix one can use cmem.free() to deallocate C allocated memory. Beware, using this unit will change your memory manager though)

 

TinyPortal © 2005-2018