unable to add element to std:map when variable is in a library

I have this code:

https://pastebin.com/5NfEveih

compiled with this command:

g++ -fPIC -shared -o libjson.so libjson.cp

and this code:

https://pastebin.com/4z6g9dPd

compiled with this command:

g++ main.cpp -o main -Wl,-rpath './' -L ./ -ljson

If I try run the executable, I am getting this error:

1
2
Fatal glibc error: malloc.c:2593 (sysmalloc): assertion failed: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
Aborted (core dumped)


but if i hsve both codes in one file, and compile it with the command:

g++ json2.cpp -o json

everything works as expected, with no errors.

Anyone can tell me what's wrong here? I need compile this as a library.
Last edited on
Not sure, but you pasted the wrong thing for the first "compiled with this command" part.
the correct command is: g++ -fPIC -shared -o libjson.so libjson.cpp
Which file is libjson.hpp?

Chances are there's one part in the code where memory is being corrupted. Not immediately sure which part.
this is libjson.hpp: https://pastebin.com/ABsUDSJ3
I solve the issue. I just realize now that in the header included in the file with the main function, the declaration of the classes were not correct (missing the class private/protected atributes)
I solve the issue.

But wouldn't that have been a compiler issue? Not solving the actual runtime issue?

Anyway, I wrote the following before seeing your latest post:

_____________________________________________

You're doing some weird copy-pasting, decreasing the chances of us being able to build the same thing you're building. You accidentally left out the protected/private fields of other classes. e.g.
protected:
    Type type;
part in that latest link.

You're defining the following in your .cpp:
1
2
3
std::map<const std::string, const Value*> JSONObject::getValues() {
    return this->values;
}

But in the hpp it's:
std::map<std::string, const Value*> getValues();
(One has a const the other one doesn't. It's kind of dumb that this matters, but it makes my compiler barf.)

I just removed the const from the std::string part of the template type to make it compile.

_____________________________________________

Now, that compiling is out of the way...

You have some... potentially undefined behavior.
I say "potentially" because I didn't bother examining every possibility, but there are warnings like:

libjson.cpp:201:17: warning: 'elementValue' may be used uninitialized in this function [-Wmaybe-uninitialized]
                 if (elementValue) {
                 ^~


Interestingly, these warnings only show up when I build with optimizations enabled.

See, the problem is, it might theoretically be possible that none of the if statements between lines 181 to 197 are entered.
If that's the case, then elementValue is not initialized correctly.

Which eventually makes it possible that you're deleting or manipulating a junk pointer.
Last edited on
Topic archived. No new replies allowed.