smallbin double linked list corrupted

748 views
Skip to first unread message

Manigandan C

unread,
Jan 17, 2017, 9:45:37 AM1/17/17
to Jansson users
Hi All,

I am facing a memory corruption issue.

My scenario:
1) I have written an application (let's call it App1) which links to jansson as dynamic library
2) I have a master application (let's call is App2) which forks App1
3) The commands to both the applications are in JSON format
4) Via App2 I spawn 1000 instances of App1
5) After a few command exchanges with App2 I get the following error from App2:
"smallbin double linked list corrupted"
and the application is closed with the signal
SIGSEGV - 11 - Invalid memory segment access

Any help would be much appreciated? [And it is a bit urgent ;)]

Thanks in Advance,
CManigandan

Graeme Smecher

unread,
Jan 17, 2017, 10:05:35 AM1/17/17
to jansso...@googlegroups.com
Hi Manigandan,

Try running your application with valgrind. Otherwise there's not much we can do to help -- unless you can send us enough code or details to understand what's going on.

Best,
Graeme

--
--
Jansson users mailing list
jansso...@googlegroups.com
http://groups.google.com/group/jansson-users
---
You received this message because you are subscribed to the Google Groups "Jansson users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jansson-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Manigandan C

unread,
Jan 17, 2017, 7:55:10 PM1/17/17
to Jansson users
Hello Graeme,

Thanks for the tip. I ran Valgrind as you've suggested and found this:

1) I have a global variable "json_t * jReply"
2) Inside a function, I do "jReply = json_array ()" if certain conditions are satisfied. Else I leave it without initializing
3) In another function I am doing this,
         if (jReply != NULL) json_decref (jReply)
4) I get a memory error when json_decref is called with jReply not initialized

I assumed that json_decref will set the json pointer to NULL after decreasing reference.
Apparently this is not the case. Is there a way to check if the reference count of the json pointer is > 0 and then call json_decref?
I did not find anything the API reference directly doing this.

Thanks & Cheers,
CManigandan

Graeme Smecher

unread,
Jan 17, 2017, 8:15:40 PM1/17/17
to jansso...@googlegroups.com
Hi Manigandan,

Valgrind is a magic tool, and I'm always happy to introduce people to it.

Comments inline. It sounds like you figured out your problem, so all I
can add is a little commentary.

On 17/01/17 04:55 PM, Manigandan C wrote:
> Hello Graeme,
>
> Thanks for the tip. I ran Valgrind as you've suggested and found this:
>
> 1) I have a global variable "json_t * jReply"

It's probably a good idea to explicitly initialize this variable:

json_t *jReply = NULL;

> 2) Inside a function, I do "jReply = json_array ()" if certain
> conditions are satisfied. Else I leave it without initializing
> 3) In another function I am doing this,
> if (jReply != NULL) json_decref (jReply)
> 4) I get a memory error when json_decref is called with jReply not
> initialized
>
> I assumed that json_decref will set the json pointer to NULL after
> decreasing reference.

The line of code

json_decref(jReply)

can't set jReply to null -- it's a pointer passed by value, so there's
no way for the json_decref function to null it. This is a C restriction
and there's not much Jansson can do about it.

> Apparently this is not the case. Is there a way to check if the
> reference count of the json pointer is > 0 and then call json_decref?
> I did not find anything the API reference directly doing this.

As I understand it, json_decref() ignores NULL pointers, so you can call
json_decref(jReply) safely even if the pointer is already nulled. You
can see the code here:

https://github.com/akheron/jansson/blob/master/src/jansson.h#L108

...this is perhaps not a good idea, though, since it relies on the
implementation of json_decref() in a non-obvious way. Ultimately, it's
up to you whether this is a good convention or not.

cheers,
Graeme

>
> Thanks & Cheers,
> CManigandan
>
> On Tuesday, January 17, 2017 at 8:35:35 PM UTC+5:30, Graeme Smecher wrote:
>
> Hi Manigandan,
>
> Try running your application with valgrind. Otherwise there's not
> much we can do to help -- unless you can send us enough code or
> details to understand what's going on.
>
> Best,
> Graeme
>
> On Jan 17, 2017 6:45 AM, Manigandan C <cmaniga...@gmail.com
> <javascript:>> wrote:
>
> Hi All,
>
> I am facing a memory corruption issue.
>
> My scenario:
> 1) I have written an application (let's call it App1) which
> links to jansson as dynamic library
> 2) I have a master application (let's call is App2) which forks App1
> 3) The commands to both the applications are in JSON format
> 4) Via App2 I spawn 1000 instances of App1
> 5) After a few command exchanges with App2 I get the following
> error from App2:
> *"smallbin double linked list corrupted"*
> and the application is closed with the signal
> SIGSEGV - 11 - *Invalid memory segment access*
>
>
> Any help would be much appreciated? [And it is a bit urgent ;)]
>
> Thanks in Advance,
> CManigandan
>
> --
> --
> Jansson users mailing list
> jansso...@googlegroups.com <javascript:>
> http://groups.google.com/group/jansson-users
> <http://groups.google.com/group/jansson-users>
> ---
> You received this message because you are subscribed to the
> Google Groups "Jansson users" group.
> To unsubscribe from this group and stop receiving emails from
> it, send an email to jansson-user...@googlegroups.com <javascript:>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
> --
> --
> Jansson users mailing list
> jansso...@googlegroups.com
> http://groups.google.com/group/jansson-users
> ---
> You received this message because you are subscribed to the Google
> Groups "Jansson users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to jansson-user...@googlegroups.com
> <mailto:jansson-user...@googlegroups.com>.

Manigandan C

unread,
Jan 19, 2017, 1:15:18 AM1/19/17
to Jansson users
Hi Graeme,

Thanks for the replies. Embarassed at how I missed the pass by value part :(
Will follow your suggestions.

Cheers,
CManigandan
Reply all
Reply to author
Forward
0 new messages