![](https://secure.gravatar.com/avatar/b47d036b8f12e712f4960ba78404c3b2.jpg?s=120&d=mm&r=g)
2014/1/24 Johan Engelen <jbc.engelen@...2592...>:
On 24-1-2014 19:04, Martin Owens wrote:
Hi devs,
I've tried out the following patch:
http://paste.ubuntu.com/6809633/
And it's not working. The path which used to work well, it now being overwritten with garbage data before the file can be opened. As if the reference has been freed.
I'm sure it's something simple to do with those consts or gchar types. Any ideas?
Example output:
** (inkscape:4946): WARNING **: Can't open file: /home/doctormo/D\x89 \x99 (doesn't exist)
** (inkscape:4946): WARNING **: Can't get document for referenced URI: inner.svg#bar
If you add functions with strings, don't use char* ! Use std::string. The bug in your case is delicate (if it is what I think it is). It's in this function:
+const gchar *URI::Impl::getFullPath(gchar const *base) const {
- const gchar *path = (gchar *)_uri->path;
- if(!path) return NULL;
- // Calculate the absolute path from an available base
- if(base && strncmp(path, "/", 1)!=0) {
path = Glib::build_filename(std::string(base), std::string(path)).c_str();
- }
- // TODO: Check existance of file here
- return path;
+}
The following line:
path = Glib::build_filename(std::string(base), std::string(path)).c_str();
creates a std::string object (build_filename does that) as a "temporary". You then call it's c_str() method, which returns a pointer to its internal C-string array. At the end of that statement, the temporary is deleted. So now, path points to a place in memory that has been freed. Advice: rewrite at least the new functions to use std::string. And be very careful when converting from std::string to char*.
Johan's analysis is correct.
In this case, you can convert the offending line to the following:
path = g_build_filename(base, path, NULL);
If you later need to store the result of c_str() back into a char pointer, do this:
foo = g_strdup(some_string.c_str());
Eventually, all paths should be stored in std::string and all XML content, user-facing strings, and so on in Glib::ustring, but these tricks should help you get by in the meantime.
Regards, Krzysztof