[Mono-devel-list] reading char * from extern function

Marcus mathpup at mylinuxisp.com
Fri May 23 20:28:23 EDT 2003

When calling into existing libraries, it is not feasible to alter the source 
code to change their memory-management policies.

If I create "QObject* obj", then "name = obj->callName()" will return a "const 
char*".  At this point, it is not permissible to call "free(name)".

If I compile and run the following, valgrind reports errors:

#include <qobject.h>
#include <iostream>

using namespace std;

int main()
    QObject* obj = new QObject( 0, "myObject" );
    char* name = (char*) obj-> className();

==1494== Invalid free() / delete / delete[]
==1494==    at 0x4004083A: free (vg_clientfuncs.c:180)
==1494==    by 0x804879E: main (in /home/marcus/a.out)
==1494==    by 0x40A7D991: __libc_start_main (in /lib/libc.so.6)
==1494==    by 0x8048661: _Znwj@@GLIBCPP_3.2 (in /home/marcus/a.out)
==1494==    Address 0x409154C8 is not stack'd, malloc'd or free'd
==1494== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 1 from 1)
==1494== malloc/free: in use at exit: 5764 bytes in 379 blocks.
==1494== malloc/free: 397 allocs, 20 frees, 6169 bytes allocated.

On Friday 23 May 2003 1:53 am, you wrote:

> Well, thats true. Functions should simply duplicate the string before
> they return if needed. Else you have a memory leak which is impossible
> to avoid.

More information about the Mono-devel-list mailing list