Ticket #14 (closed defect: fixed)

Opened 1 year ago

Last modified 5 months ago

.dup return strings?

Reported by: keinfarbton Assigned to: Pse
Priority: major Milestone: Pre-release 9
Component: gtkd - classes Version: TRUNK
Keywords: strings text entry corruption Cc:

Description

If some strings are requested from the GUI and stored, they need to be copied.

char[] str1 = entry1.getText();
char[] str2 = entry2.getText();

This can/will result in corrupted data in str1, because GTK seems to reuse the buffer from the first line. I wonder if the getText() method shall .dup the string by default?

Perhaps it would make sense to offer an additional parameter, but default behaviour does the copy. So both is possible, maximum performance, but by default, the save copy is made.

char[] getText( bool makeCopy = true ){
   char[] res = ...;
   return makeCopy ? res.dup : res;
}

What you mean?

Change History

12/30/07 00:38:20 changed by Pse

  • owner changed from JJR to Pse.

12/30/07 00:38:31 changed by Pse

  • status changed from new to assigned.

12/30/07 00:39:24 changed by Pse

  • keywords set to strings text entry corruption.
  • milestone set to Release 1.0.

12/30/07 00:40:09 changed by Pse

  • type changed from enhancement to defect.

12/30/07 01:47:57 changed by Pse

This is GTK+'s documented behaviour. Not really safe, though. We could do better.

I don't think adding an additional parameter is the right way of doing this. Instead, I think we should create two methods for each get function that returns a character array: one with the default name, which returns a dup of the original string (i.e. Entry.getText()); and another one, which returns the original GTK+ string (i.e. Entry.getTextNoDup()).

I'll try to get to this in the following days.

(follow-up: ↓ 9 ) 12/30/07 14:05:15 changed by Pse

In r353 strings are automatically dupped before returning them (wrapper only, will be mirrored in the next rewrap). Todo: set the wrapper to create sister methods that return the original string.

01/11/08 11:56:19 changed by Pse

  • status changed from assigned to new.

01/11/08 17:07:00 changed by Mike Wey

I've looked a bit more at the GTK docs, it looks like functions witch return a const gchar* return only a reference to the internaly used string. While the functions which return a normal gchar* return a string you are allowed to change. Since D1 doesn't have const we could return a dup for the functions with const gchar* as the return type, while returning the string from GTK for the functions with gchar* as the return type.

(in reply to: ↑ 6 ; follow-up: ↓ 10 ) 01/11/08 17:15:09 changed by Mike Wey

Replying to Pse:

Todo: set the wrapper to create sister methods that return the original string.

Does that mean there would be a duplicate of every function that return a sting, with the only difference would be dup or no dup and probably the method name. I don't like that option much i would rather go with a sensible default. or is there some case where you actually need access to the string directly ?

(in reply to: ↑ 9 ) 01/12/08 22:01:53 changed by Pse

Replying to Mike Wey:

Replying to Pse:

Todo: set the wrapper to create sister methods that return the original string.

Does that mean there would be a duplicate of every function that return a sting, with the only difference would be dup or no dup and probably the method name. I don't like that option much i would rather go with a sensible default. or is there some case where you actually need access to the string directly ?

I guess the sole reason would be performance. I'd be pushed hard to think of an actual usage case where that kind of performance would be needed, though. Since dups are already implemented by default, I'm still waiting for any actual complaints. Given your recent discoveries, I guess we could set up dups the way you mentioned. If anyone really needs that kind of performance, we could always implement undupped sister methods at a later date.

06/28/08 11:08:46 changed by okibi

  • status changed from new to closed.
  • version changed from Pre-release 7 to TRUNK.
  • resolution set to fixed.
  • milestone changed from Release 1.0 to Pre-release 9.

Strings are .dupped by default. We haven't received any complaints on this issue. If anyone feels like we need to return to this ticket and review, let us know.