ToCString and FromCString and Memory Flushing

BlitzMax Forums/BlitzMax Beginners Area/ToCString and FromCString and Memory Flushing

Gabriel(Posted 2006) [#1]
I'm sure I've asked this before and received vague/contradictory replies. Now that FlushMem has been deprecated, what needs to be done with ToCString and FromCString?

I assumed ( apparently wrongly ) that FromCString needed no action and that ToCString needed a MemFree call, but MemFree seems to mess up ( causes subsequent program execution not to occur even though the program doesn't stop ) when used with a Byte Ptr returned from a string's ToCString() method.


Chris C(Posted 2006) [#2]
you need to assign straight away to a max string with FromCString


Perturbatio(Posted 2006) [#3]
You could always manage memory yourself and use GCCollect


Difference(Posted 2006) [#4]
You need to free the ToCString().

Read this topic: http://www.blitzbasic.com/Community/posts.php?topic=53269

Hi,

Here's the story:

String.ToCString() and String.ToWString() return pointers that you must manually free yourself using MemFree.

The $Z data type can only be used with function parameters, and automagically handles the ToCString/MemFree stuff for you - it does not actually use GC at all.

The $Z setup assumes that the receiving c function does NOT 'hold onto' or otherwise use the c string after the function returns. In other words, $Z can be thought of as 'const char *'.

I did briefly consider getting rid of $Z altogether (and forcing everyone to use ToCString/MemFree) but realized it would require quite a module overhaul to achieve.

I'll probably add $W at a later date too.

Bye,
Mark Sibly
WinXP, DX9.0b, P4/3G, 512M, Radeon 9800
MacOS10.3, G5/1.8G, 768M, GeForce 6800 Ultra



Gabriel(Posted 2006) [#5]
That's exactly what I was looking for, thanks. I knew I'd seen it somewhere.