Page 1 of 2
new/delete operators
Posted: Mon Aug 11, 2008 12:40 am
by ProphetOfDoom
Hiya,
Recently I was converting a program from Blitz BASIC and I (a bit naively I admit) replaced all the
with:
Code: Select all
*p = AllocateMemory(SizeOf(Thing))
...
FreeMemory(*p)
The problem was the objects had strings (like MyString$) in them, and FreeMemory doesn't free the strings.
It caused a memory leak that took me hours to track down. And I found that in this situation there doesn't seem to be any obvious way to free the strings at all.
In the end I swapped the strings for memory buffers and a lot of confusing peeking and poking.
Could you add some New/Delete operators that take care of strings automatically?
Thanks for reading.
Posted: Mon Aug 11, 2008 8:49 am
by milan1612
For this you can use a dirty little trick Freak posted some time ago:
Code: Select all
;The following function frees the string pointed to by *Address.
;Used for structures which are created using AllocateMemory().
Procedure FreeStructureString(*Address)
Protected String.String
PokeL(@String, *Address)
EndProcedure
You can use OffsetOf() to find the offset to the string field(s) of the structure.
EDIT: See the post by Freak here:
http://www.purebasic.fr/english/viewtop ... 9&start=10
Posted: Mon Aug 11, 2008 11:23 am
by Little John
I think that should be mentioned in PB's help!
Regards, Little John
Posted: Mon Aug 11, 2008 11:50 am
by Kaeru Gaman
I think the reason is the way dynamic strings are handled in PB.
in the object, only the poolpointer is located, the string itself stays in the pool.
one way to avoid this would be to use only fixedstrings in such objects.
on the other hand... wouldn't it work to add some
MyString$ = ""
into the object destructor?
Posted: Mon Aug 11, 2008 6:31 pm
by Hroudtwolf
Use a string class for string members of your class propertytables.
So you have the full control about the memory your strings are using.
Regards
Wolf
Code: Select all
Interface IcYourClass
; ...
EndInterface
Structure cYourClass
*VTABLE
sobjFilename.IcString
sObjPath .IcString
EndStructure
Posted: Mon Aug 11, 2008 6:39 pm
by Kaeru Gaman
@Wolf:
?
... what kind of structure is IcString?

Posted: Mon Aug 11, 2008 8:12 pm
by ProphetOfDoom
milan, thanks. I wish I'd known about that trick before Iwent through 4000 lines of code changing it to use peek and pokes.
Kaeru, I did initially change my program (an expression evaluator) to use fixed strings. It turned out it was a false economy because really an expression evaluator needs unlimited lengths (for string constants etc) and I had to rewrite it. IT would work for some situations though.
Little John, yes! It is BASIC after all. If I was a newbie I would never have thought of checking for memory leaks, and I would still be unaware of the problem now!
*eyes wolf code and looks puzzled*
Posted: Mon Aug 11, 2008 10:01 pm
by inc.
@Kaeru Gaman
He means that he uses a String Class like in C++ which does handle the strings. IcString in there is an encapsulated String Class object.
with methods like ...\get(), ...\Set("...") and so on.
Posted: Mon Aug 11, 2008 10:13 pm
by Kaeru Gaman
when he does not talk about native purebasic, why doesn't he mention?
what would you think about someone who answers to a question with a code consisting of userlib-calls and he does not tell he uses a userlib at all?
this is at least bad style....
Posted: Mon Aug 11, 2008 10:43 pm
by Hroudtwolf
@Kaeru
Do you feel offence 'cause you don't understood my post?
So, sorry.
I couldn't suspected it.
Best regards
Wolf
Posted: Mon Aug 11, 2008 10:54 pm
by Kaeru Gaman
no, I just don't like your "oh look, I do OOP in PB"-behaviour...
when you are using classes, you need includes or libs to use them.
so, name your Libs/Includes, link them, or just don't post any code.
would you like if somebody posts as an answer to your question
code that is full of Userlib-commands, and he does not even mention he uses a userlib?
Posted: Mon Aug 11, 2008 10:57 pm
by Hroudtwolf
@Kaeru
Nuts to you!
@ProphetOfDoom
Inc resolved the mysterium about my code.
Indeed, it represents the using of a string class. Such class is not a part of the purebasic package.
You would have to write those by your self.
More about interface classes in many of rods "tipps&tricks" threads or if you understand german, look here...
http://www.purebasic-lounge.com/viewforum.php?f=95
Best regards
Wolf
Posted: Mon Aug 11, 2008 11:03 pm
by Little John
Kaeru Gaman wrote:no, I just don't like your "oh look, I do OOP in PB"-behaviour...
when you are using classes, you need includes or libs to use them.
so, name your Libs/Includes, link them, or just don't post any code.
would you like if somebody posts as an answer to your question
code that is full of Userlib-commands, and he does not even mention he uses a userlib?
I absolutely agree!
Posting code like Hroudtwolf did is not of any help in cases like this. And I think it's common sense that people should post
working code if possible.
Regards, Little John
Posted: Mon Aug 11, 2008 11:21 pm
by Hroudtwolf
Why, you don't accost myself directly?
Its a fact, that I've expected (or interpreted) more knowledge by the threadfounder about this subject matter.
This is the reason of my "cryptic" post.
Regards
Wolf
PS: Should I pray 100x "Our Father..." for my bad mistake?
Posted: Tue Aug 12, 2008 1:31 am
by Kaeru Gaman
Hroudtwolf wrote:I've expected (or interpreted) more knowledge by the threadfounder about this subject matter.
well ok, then I can understand better why you posted that code...
PS:
no, hundered prayers won't do it, sacrify a rabbit for Odin...
