Memory leak in mdlText_getElementDescr?

I convert a lot of text with mdlText_getElementDescr:

status=mdlText_getElementDescr (&edPText, &edP->el.text_2d, NULL);

if (status == SUCCESS)

{

// do nothing
   mdlElmdscr_freeAll(&edPText);

}

Used Memory grows over the time. Is there really a bug in this function? How to free the Memory? Is there any Workaround?

Parents Reply Children
  • Unknown said:
    It is from model scan.But why it should not freed?

    Whether it should or should not be freed depends on how you write your scan.  If you explicitly read the element descriptor in your scan code, then the descriptor is yours to free.  If, on the other hand, you use an element descriptor provided by a scan callback function, then the descriptor is not yours to free.

    Example (from MDL help) of reading an element descriptor yourself:

    do
    {
      scanSize = sizeof (scanBuf) / sizeof (short);
      scanStatus = mdlScanCriteria_scan (oscP,scanBuf,&scanSize,&scanPosition);
      for (lP = scanBuf; lP < scanBuf + scanSize / 2; ++lP)
      {
        MSElementDescr  *edP;
        if (mdlElmdscr_readToMaster (&edP, *lP, mdlModelRef_getActive(), FALSE, NULL))
        {
          ...
          mdlElmdscr_freeAll (&edP);
        }
    
      }
    } while (scanStatus == BUFF_FULL);

    Example of scan callback:

    //  In your scan function
    mdlScanCriteria_setElmDscrCallback (pScanCrit, (PFScanElemDscrCallback)writeWorkFunc, (void*)cpArgms);
    mdlScanCriteria_scan (pScanCrit, NULL, NULL,NULL);
    

    The scan callback function:

    int             writeWorkFunc
    (
    MSElementDescr* edP,     /*  =>  This descriptor is not yours (should be MSElementDescr const* edP) */
    void*           args,
    ScanCriteria*   scP
    )
    {
        MSElementDescr  *newDescrP = NULL;
        /*  Here's how to obtain a descriptor that belongs to you */
        mdlElmdscr_duplicate (&newDescrP,edP);
        /*  Do something with duplicate descriptor */
        mdlElmdscr_freeAll (&newDescrP);
        /*  But don't free edP */
        return  SUCCESS;
    }

     
    Regards, Jon Summers
    LA Solutions

  • OK - nice. I used the first variant in the last 20 years. So ists not the reason for the problem. But thanx.
    Regards Tom
  • Hi Thomas,

    to hunt memory leaks is always disagreeable job. It's not clear what is "around" the code you posted and I am not fully convinced the problem is in these functions. Do you think it's possible to extract a bit more code including part where you manipulate with elements?

    Another questions is what doeas it mean exactly "Used Memory grows over the time."? If you modified a lot of elements, I guess some memory usage grow is because of undo buffer.

    For curiosity only, not directly related to discussed issue (but native code can behave differently): Why do you use old pseudocode MDL that has been marked as obsolete and "don't use it' about 14 years ago? Is it because of some very old MicroStation version?

    With regards,

    Jan
  • Hi Jan,
    ok, I will build a minimum example. Let me a couple of minutes.

    I started with mdl over 20 years ago and there ist a lot of technology to use...and it runs

    With regards
    Tom
  • Hi Jan,
    here is the code, directly called from main. I've placed 7000 texts 'hallo tom'. The effect is only with true type text.

    scP = mdlScanCriteria_create ();
    mdlScanCriteria_setModel (scP,ref);
    mdlScanCriteria_setReturnType (scP,MSSCANCRIT_RETURN_FILEPOS ,FALSE,TRUE);


    do
    {
    scanWords = sizeof(elmAdr)/sizeof(short);
    status = mdlScanCriteria_scan (scP,elmAdr, &scanWords,&filePos);

    numAdr = scanWords/sizeof(short);

    for (i=0;i<numAdr ;i++ )
    {
    filepos = elmAdr[i];
    startFilepos = filepos;
    mdlElmdscr_read(&edP, elmAdr[i], ref, FALSE, NULL);
    type=mdlElement_getType(&edP->el);
    if (type == TEXT_ELM)
    {
    mdlText_getElementDescr(&edPText, &edP->el.text_2d, NULL);
    mdlElmdscr_freeAll(&edP);
    mdlElmdscr_freeAll(&edPText);
    }
    }
    } while (status == BUFF_FULL);
    mdlScanCriteria_free (scP);


    With regards
    Tom
  • Hi Tom,

    Unknown said:
    here is the code

    Thanks! Right now I have no idea what is wrong, but let's hope somebody else will have an idea.

    Can you please edit your post and to use Syntaxhighlighter (yellow pencil icon in Advanced Editor mode) and set the language to C++, so you code will be displayed not as ugly plain text but as the real code?

    Unknown said:
    The effect is only with true type text.

    This is weird.

    But some information still missing:

    • What version of MicroStation do you use? BTW It would be nice to follow Programmin Forum best practices and to mention main branch in Question subject and exact MicroStation build in the first post.
    • Is memory decreased (freed) after the design file is closed or it remains allocated until MicroStation is closed?

    Unknown said:
    I started with mdl over 20 years ago

    I understand, but it's not good. In such case I would still use assembler for 8bit processors and Pascal ;-)

    Unknown said:
    ...and it runs

    Will not anymore pretty soon. MicroStation CONNECT Edition doesn't support pseudocode applications, so you will have to migrate your code to native MDL anyway and it's for sure better to migrate pseudocode app to native code at first in MicroStation V8i and in the next step to migrate working native code to CONNECT Edition than to try to solve both tasks at the same time ... in such case maybe it's easier to write the application from scratch.

    And even in V8i it's not only recommended, but also better to use native C/C++ code, because of more standard programming environment and also an access to new MicroStationAPI that provides some extra features compring to C MDL API.

    With regards,

      Jan

  • Hi Jan,
    OK - thank you for help / hints and that you spend your time for me!

    Best regards
    Tom
  • Hi Tom,
    just to be accurate:

    if (mdlText_getElementDescr(&edPText, &edP->el.text_2d, NULL)==SUCCESS) {
    mdlElmdscr_freeAll(&edPText);
    }
    mdlElmdscr_freeAll(&edP);

    Did not compile your example.
    Is your demo code leaking, too?

    Best, Stefan.
  • Hi Stefan,
    yes, thats better. But the code is leaking, too - unfortunatily

    Regards, Tom
  • Sry Tom,
    not leaking for me. Zero K for 20.000 text elements.
    Can you send your file, or at least a sample containing malicious text?
    Best, Stefan.