new/delete operators

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
ProphetOfDoom
User
User
Posts: 84
Joined: Mon Jun 30, 2008 4:36 pm
Location: UK

new/delete operators

Post by ProphetOfDoom »

Hiya,

Recently I was converting a program from Blitz BASIC and I (a bit naively I admit) replaced all the

Code: Select all

p = New Thing
...
Delete p
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.
milan1612
Addict
Addict
Posts: 894
Joined: Thu Apr 05, 2007 12:15 am
Location: Nuremberg, Germany
Contact:

Post 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
Windows 7 & PureBasic 4.4
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Post by Little John »

I think that should be mentioned in PB's help!

Regards, Little John
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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?
oh... and have a nice day.
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post 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
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

@Wolf:
?

... what kind of structure is IcString? :?
oh... and have a nice day.
ProphetOfDoom
User
User
Posts: 84
Joined: Mon Jun 30, 2008 4:36 pm
Location: UK

Post 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*
inc.
Enthusiast
Enthusiast
Posts: 406
Joined: Thu May 06, 2004 4:28 pm
Location: Cologne/GER

Post 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.
Check out OOP support for PB here!
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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....
oh... and have a nice day.
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post by Hroudtwolf »

@Kaeru

Do you feel offence 'cause you don't understood my post?
So, sorry.
I couldn't suspected it.

Best regards

Wolf
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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?
oh... and have a nice day.
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post 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
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Post 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
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post 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?
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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... ;)
oh... and have a nice day.
Post Reply