|
|
Home » U++ TheIDE » U++ TheIDE: Other Features Wishlist and/or Bugs » bug in latest svn
Re: bug in latest svn [message #15673 is a reply to message #15668] |
Sun, 04 May 2008 03:35 |
Novo
Messages: 1358 Registered: December 2006
|
Ultimate Contributor |
|
|
mdelfede wrote on Sat, 03 May 2008 13:17 |
AFAIK that's a right behavior, as returned temporary string is firs assigned to result and then deleted.
|
IMHO, the way you describe would work if the function looks like below.
String GetCppFile(int i);
Current implementation is similar to code below.
const String* GetCppFile(int i)
{
INTERLOCKED_(cpp_file_mutex) {
return &cpp_file[i];
}
return &String();
}
Does this look correct to you?
Regards,
Novo
[Updated on: Sun, 04 May 2008 03:43] Report message to a moderator
|
|
|
Re: bug in latest svn [message #15679 is a reply to message #15673] |
Sun, 04 May 2008 12:17 |
mdelfede
Messages: 1307 Registered: September 2007
|
Ultimate Contributor |
|
|
Novo wrote on Sun, 04 May 2008 03:35 |
mdelfede wrote on Sat, 03 May 2008 13:17 |
AFAIK that's a right behavior, as returned temporary string is firs assigned to result and then deleted.
|
IMHO, the way you describe would work if the function looks like below.
String GetCppFile(int i);
Current implementation is similar to code below.
const String* GetCppFile(int i)
{
INTERLOCKED_(cpp_file_mutex) {
return &cpp_file[i];
}
return &String();
}
Does this look correct to you?
|
Nope, I guess... here you're returning a reference to a local variable, that can (and usually IS) destroyed BEFORE function returns. So, you could make it static to solve the problem, but then you'd have many other problems, in particular with MT.
Returning a String() value is less efficient, but guarantees that string is not destroyed on function return before the value is taken.
Max
|
|
|
Re: bug in latest svn [message #15681 is a reply to message #15679] |
Sun, 04 May 2008 17:04 |
Novo
Messages: 1358 Registered: December 2006
|
Ultimate Contributor |
|
|
mdelfede wrote on Sun, 04 May 2008 06:17 | Returning a String() value is less efficient, but guarantees that string is not destroyed on function return before the value is taken.
|
Isn't
equal to
String anonymous_value;
return anonymous_value;
?
const String& GetCppFile(int i);
String value1 = GetCppFile(0); // Case A
const String& value2 = GetCppFile(0); // Case B
In case A GetCppFile() will work correctly.
Case B will introduce a bug.
IMHO, returning "const String&" is just not thread-safe. Object can be deleted in transition.
Regards,
Novo
|
|
|
Re: bug in latest svn [message #15682 is a reply to message #15679] |
Sun, 04 May 2008 17:11 |
Novo
Messages: 1358 Registered: December 2006
|
Ultimate Contributor |
|
|
mdelfede wrote on Sun, 04 May 2008 06:17 | Nope, I guess... here you're returning a reference to a local variable, that can (and usually IS) destroyed BEFORE function returns.
|
IMHO, returning reference to a local variable is the same thing as returning pointer to a local variable ...
Regards,
Novo
|
|
|
|
|
|
Re: bug in latest svn [message #15686 is a reply to message #15684] |
Sun, 04 May 2008 18:07 |
Novo
Messages: 1358 Registered: December 2006
|
Ultimate Contributor |
|
|
mdelfede wrote on Sun, 04 May 2008 11:16 |
It's strange that compiler doesn't warn about....
|
Actually, it does ...
Regards,
Novo
|
|
|
Re: bug in latest svn [message #15687 is a reply to message #15686] |
Sun, 04 May 2008 18:12 |
mdelfede
Messages: 1307 Registered: September 2007
|
Ultimate Contributor |
|
|
Indeed, I was wrong before :
This piece of code :
#include "stdio.h"
static long seed = 0;
class LongClass
{
long l;
public :
long Get(void) const { return l; }
LongClass() { l = seed++ ; }
};
const LongClass &test(const LongClass &lClass = LongClass())
{
return lClass;
}
int main(int argc, const char *argv[])
{
const LongClass &a = test();
const LongClass &b = test();
long aa = a.Get();
long bb = b.Get();
return 0;
}
Shows that's perfectly legal to return references to const temporaries... Here aa gets a value of 0 and bb of 1, correctly.
That behaviour allows to initialize default reference arguments with objects created on the fly. I don't know where does compile store the actual value....
Max
[Updated on: Sun, 04 May 2008 18:14] Report message to a moderator
|
|
|
Re: bug in latest svn [message #15689 is a reply to message #15667] |
Sun, 04 May 2008 18:28 |
|
mirek
Messages: 13975 Registered: November 2005
|
Ultimate Member |
|
|
Novo wrote on Sat, 03 May 2008 11:42 |
Novo wrote on Fri, 02 May 2008 16:15 |
Hopefully, I’ll submit a patch at the end of the day.
|
Sorry, couldn't keep my promice.
I probably miss something, but code below always was a problem.
const String& GetCppFile(int i)
{
INTERLOCKED_(cpp_file_mutex) {
return cpp_file[i];
}
return String();
}
Should be something like that:
const String& GetCppFile(int i)
{
INTERLOCKED_(cpp_file_mutex) {
return cpp_file[i];
}
static String value;
return value;
}
I'm not sure about thread-safety of static variable initialization though.
|
Well, second return is never performed. It is there only to shut-up the compiler about missing return.... (which, of course, in this context sounds like void effort
Mirek
P.S.: Other than that, while working in the current IDE, the code should be changed anyway.
|
|
|
|
|
|
|
|
|
Re: bug in latest svn [message #15781 is a reply to message #15763] |
Wed, 07 May 2008 22:07 |
Novo
Messages: 1358 Registered: December 2006
|
Ultimate Contributor |
|
|
luzr wrote on Wed, 07 May 2008 07:59 |
Novo wrote on Mon, 05 May 2008 22:07 |
luzr wrote on Sun, 04 May 2008 12:28 | Well, second return is never performed. It is there only to shut-up the compiler about missing return.... (which, of course, in this context sounds like void effort
|
In this case I have two questions:
1) Why you are not using something like mutex guard for such code? That would eliminate requirement in "return String()".
2) Why this code was left not thread-safe?
Just curios ...
|
Well, INTERLOCKED_ is a mutex guard...
Unfortunately, the compiler is not able to figure out that second return never takes place...
Mirek
|
What is wrong with the code below?
const String& GetCppFile(int i)
{
Mutex::Lock guard(cpp_file_mutex);
return cpp_file[i];
}
What is so special about INTERLOCKED_ ?
Regards,
Novo
[Updated on: Wed, 07 May 2008 22:07] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Fri May 03 01:48:53 CEST 2024
Total time taken to generate the page: 0.05052 seconds
|
|
|