CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: Memory Leak

  1. #1
    Join Date
    Oct 2006
    Posts
    13

    Memory Leak

    I wrote a program using VC++.
    I use the library files (vld.h) to detect the memory leak problem.
    I face memory leak problem.
    Here is the sample.

    char * a;

    UCHAR b[100];
    .
    .
    .

    a = (char *) b;

    std::string c = std::string(a);

    Can anyone help me?

    Thanks.

  2. #2
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Memory Leak

    What is happening in between those lines ( the .... s )

  3. #3
    Join Date
    Oct 2006
    Posts
    13

    Re: Memory Leak

    In between the lines, I actually access mysql such as select statement or update statement.

    Like
    - SQLBindCol(hStmt, 1, SQL_C_CHAR, ....

    Thanks

  4. #4
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Memory Leak

    When Ever use a Pointer in your Program try to use new to Allocate Memory to your pointer and after Finishing your Work use Delete to free allocated memory.
    Thanx

  5. #5
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: Memory Leak

    Quote Originally Posted by thl
    I wrote a program using VC++.
    I use the library files (vld.h) to detect the memory leak problem.
    I face memory leak problem.
    Here is the sample.

    char * a;

    UCHAR b[100];
    .
    .
    .

    a = (char *) b;

    std::string c = std::string(a);

    Can anyone help me?

    Thanks.
    You dont have memory allocations done using new or malloc, so there is no leak in this code.
    Regards,
    Ramkrishna Pawar

  6. #6
    Join Date
    Oct 2006
    Posts
    13

    Re: Memory Leak

    Thanks everyone.

    I would like to show you all that the line that has been complained is
    std::string c = std::string( a );

    that means the last line that I posted.

    Hope to hear more from you all soon....

    Thanks

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Memory Leak

    Quote Originally Posted by thl
    Thanks everyone.

    I would like to show you all that the line that has been complained is
    std::string c = std::string( a );

    that means the last line that I posted.
    There is no memory leak there.

    Temporarily get rid of the SQL stuff, post a complete but small program that demonstrates the problem (please, no "..." -- make sure it is a complete example), and post it. Otherwise you do not have information for anyone to tell you what is going on.

    Regards.

    Paul McKenzie

  8. #8
    Join Date
    Oct 2006
    Posts
    13

    Re: Memory Leak

    The program is like this,

    int main(int argc, char* envp[])
    {
    int nRetCode = 0;
    char * b;
    UCHAR a[100] = "hehehe";

    b = (char *) a;

    string c = string(b);

    return nRetCode;
    }

    There is no memory leak for this, but in my actual applications, after running some lines of the SQL stuffs, it claims memory leak.

    Thanks

  9. #9
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: Memory Leak

    Then it could be in SQL code.
    Regards,
    Ramkrishna Pawar

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Memory Leak

    Quote Originally Posted by thl
    There is no memory leak for this, but in my actual applications, after running some lines of the SQL stuffs, it claims memory leak.
    So given this, what is your conclusion? The SQL is the thing you should be investigating.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537

    Re: Memory Leak

    It's 'heh heh'

    What claims a memory leak? vld.h? Visual leak detector?

    http://www.codeproject.com/tools/vis...select=1236065

    Look, unless you know what you are doing, you will get false positives from the CRT. Can you post a complete project. Your code has no 'leak' so I'm gonna assume they call the leak check before the CRT clears.

  12. #12
    Join Date
    Oct 2006
    Posts
    13

    Re: Memory Leak

    What does it mean by call leak before CRT clear?

    Thanks
    Last edited by thl; November 8th, 2006 at 04:54 AM.

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Memory Leak

    You may also consider using different technology to connect to the mySql database. Consider using the ATL DB Consumer classes or ADO for db interaction. The old direct ODBC approach that you are using (SQLBindCol(hStmt, 1, SQL_C_CHAR, ....)), requires that you explicitly allocate memory for the column data. As such, you must remember to delete any allocated memory or you'll have leaks.

    The ATL DB Consumer or ADO approaches take care of any memory allocation/cleanup for you and let you get on to programming other parts of your application.

  14. #14
    Join Date
    Oct 2006
    Posts
    13

    Re: Memory Leak

    Arjay, thanks a lot. There is a new way for me to move forward.

    Just to confirm again, like

    ****************************
    UCHAR ucField[50];
    SDWORD sdField;

    SQLBindCol(hStmt, 1, SQL_C_CHAR, ucField, sizeof(ucField), &sdField);
    ****************************

    may cause the memory leak?

    For what I have in my project, I just do as what I showed above.

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured