Code Monkey home page Code Monkey logo

Comments (4)

johnezang avatar johnezang commented on September 23, 2024

While there certainly exists the possibility that there is a memory leak inside JSONKit, at this point it has been in heavy use by a very large number of projects at this point, and most of the major bugs have been fixed.

Pragmatically, this means that the most likely cause of the memory leak is a bug in your code, not JSONKit. The leak information you're getting is the stack trace of where the allocation took place, which is not the same thing as where that allocation was leaked.

The most likely explanation is that you have over retained something that JSONKit has given you. Unfortunately, it is simply not possible for me to offer any more assistance than this generic advice. For now, I'm going to close this bug, but if you have additional information which clearly implicates JSONKit, feel free to re-open this bug or open a new one.

from jsonkit.

mast avatar mast commented on September 23, 2024

Thanks for your response. You was right and I found leak in my code in very non-obvious place. Sorry that I was complaining JSONKit!

from jsonkit.

jkubicek avatar jkubicek commented on September 23, 2024

I'm seeing the same leak. See this line of code in jk_cachedObjects:

https://github.com/johnezang/JSONKit/blob/master/JSONKit.m#L2038

switch(parseState->token.value.type) {
    case JKValueTypeString:           parsedAtom = (void *)CFStringCreateWithBytes(NULL, parseState->token.value.ptrRange.ptr, parseState->token.value.ptrRange.length, kCFStringEncodingUTF8, 0); break;
    case JKValueTypeLongLong:         parsedAtom = (void *)CFNumberCreate(NULL, kCFNumberLongLongType, &parseState->token.value.number.longLongValue);                                             break;
    case JKValueTypeUnsignedLongLong:
      if(parseState->token.value.number.unsignedLongLongValue <= LLONG_MAX) { parsedAtom = (void *)CFNumberCreate(NULL, kCFNumberLongLongType, &parseState->token.value.number.unsignedLongLongValue); }
      else { parsedAtom = (void *)parseState->objCImpCache.NSNumberInitWithUnsignedLongLong(parseState->objCImpCache.NSNumberAlloc(parseState->objCImpCache.NSNumberClass, @selector(alloc)), @selector(initWithUnsignedLongLong:), parseState->token.value.number.unsignedLongLongValue); }
      break;
    case JKValueTypeDouble:           parsedAtom = (void *)CFNumberCreate(NULL, kCFNumberDoubleType,   &parseState->token.value.number.doubleValue);                                               break;
    default: jk_error(parseState, @"Internal error: Unknown token value type. %@ line #%ld", [NSString stringWithUTF8String:__FILE__], (long)__LINE__); break;
  }

  if(JK_EXPECT_T(setBucket) && (JK_EXPECT_T(parsedAtom != NULL))) {
    bucket = useableBucket;
    if(JK_EXPECT_T((parseState->cache.items[bucket].object != NULL))) { CFRelease(parseState->cache.items[bucket].object); parseState->cache.items[bucket].object = NULL; }

    if(JK_EXPECT_T((parseState->cache.items[bucket].bytes = (unsigned char *)reallocf(parseState->cache.items[bucket].bytes, parseState->token.value.ptrRange.length)) != NULL)) {
      memcpy(parseState->cache.items[bucket].bytes, parseState->token.value.ptrRange.ptr, parseState->token.value.ptrRange.length);
      parseState->cache.items[bucket].object = (void *)CFRetain(parsedAtom);
      parseState->cache.items[bucket].hash   = parseState->token.value.hash;
      parseState->cache.items[bucket].cfHash = 0UL;
      parseState->cache.items[bucket].size   = parseState->token.value.ptrRange.length;
      parseState->cache.items[bucket].type   = parseState->token.value.type;
      parseState->token.value.cacheItem      = &parseState->cache.items[bucket];
      parseState->cache.age[bucket]          = JK_INIT_CACHE_AGE;
    } else { // The realloc failed, so clear the appropriate fields.
      parseState->cache.items[bucket].hash   = 0UL;
      parseState->cache.items[bucket].cfHash = 0UL;
      parseState->cache.items[bucket].size   = 0UL;
      parseState->cache.items[bucket].type   = 0UL;
    }
  }

  return(parsedAtom);

parsedAtom is created with a create method, yielding an object with +1 retain count. CFRetain is called on the parsedAtom yeilding a +2 retain count. The parsedAtom is not released.

Just making the change to remove the CFRetain isn't sufficient; the parsedAtom is used elsewhere as a key in a JKObjectStack, but not retained when it's added to that stack. I'm not sure what the proper solution is here, but I'm looking into it.

from jsonkit.

johnezang avatar johnezang commented on September 23, 2024

@jkubicek,

Please open a ticket with a concise test case that reproduces the memory leak.

At this point JSONKit is fairly mature and stable, and used in a lot of real world applications, none of which are experiencing memory leaks due to JSONKit.

To the best of my knowledge, every report of JSONKit leaking memory has turned out to be user error- the leak was caused by over retaining an object that was created by JSONKit. The memory leak tools can only show you the call stack that allocated the memory that was (eventually) leaked, NOT where the leak took place.

from jsonkit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.