Thread: Can't delete memory

  1. #16
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Valgrind is giving me something interesting.

    Process terminating with default action of signal 11 (SIGSEGV)
    Access not within mapped region at address 0x0

    It's pointing to this though:
    Code:
    max_disk_queue = atoi(argv[1]);
    This is in a completely different place though and happens way before. Also, it doesn't actually segfault at that line.
    Last edited by jcafaro10; 02-04-2009 at 10:11 AM.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Likely, argv[1] is NULL.
    Check argc to see how many arguments are passed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Have you considered using containers and smart pointers instead? For example, if you need a stack of chars, use a std::stack<char> (which is a container adapter that provides the interface of a stack).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jcafaro10 View Post
    How do I determine if the pointer is valid? GDB+KDevelop tells me the memory address of the pointer. When I allocate the stack the pointer gets a memory address and it keeps that memory address so I don't think that's the problem.

    Something that is a little curious is, on my watch list it says stack, and gives me a hex memory address, and under that is *stack and thats got 0x0. I'm not sure what that means though.
    It means that the stack points at memory address <hex number> and that it's content is 0x0.

    It is very hard to visually determine the validity of an address - with a lot of viewing memory addresses, you can get a feel for what is valid and isn't on a particular system - but essentially, on Linux, anything from 128MB to 3GB is theoretically correct - but you don't get a completely RANDOM value in that range when allocating memory.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Quote Originally Posted by Elysia View Post
    Likely, argv[1] is NULL.
    Check argc to see how many arguments are passed.
    My program wouldn't get very far at all if argv[1] is null. max_disk_queue gets a value from the argument and the program continues. GDB says it's getting the value 3 which is good because thats the argument I passed. Right before that valgrind says "invalid read of size 1" and points to that line as well.

  6. #21
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    If I comment out the delete of stack, my program doesn't seg fault but the weird valgrind error is still there

  7. #22
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Quote Originally Posted by laserlight View Post
    Have you considered using containers and smart pointers instead? For example, if you need a stack of chars, use a std::stack<char> (which is a container adapter that provides the interface of a stack).
    I think I have to do it this way because I'm using ucontext which is a linux thing and requires things to be set up a certain way.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jcafaro10
    I think I have to do it this way because I'm using ucontext which is a linux thing and requires things to be set up a certain way.
    What is the "certain way"? Note that if you need a pointer to the first element of a dynamically allocated array of objects you can use a std::vector<char> and then pass &v[0], where v is the name of the vector.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Doubtful. A stack is a stack, and a pointer is a pointer, and a smart pointer can handle a raw pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jcafaro10 View Post
    If I comment out the delete of stack, my program doesn't seg fault but the weird valgrind error is still there
    Yes, it won't free the memory either, and since (I'm pretty sure) the problem with the stack delete operation is that the administration block of a previous allocation is being overwritten, it obviously won't fail if you don't call the function that tries to USE that bit of memory.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #26
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Hmm well valgrind doesn't seem to be reporting anything that conflicts with the stack allocation. I tagged the lines that valgrind mentioned and the memory addresses are all different than the one for the stack.

  12. #27
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    So I ran the debugger again and the time that I ran it, the stack was allocated to memory address 0xb7c34014, so I tagged 0xb7c34012 and 0xb7c34016 (those are a byte away right? I'm a bit new to this) Neither of the values in those locations were overwritten after the allocation of the stack. The stack is given a new memory address everytime I run the program so I have no idea what memory addresses to check before the stack is allocated.

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you need to check where your stack is actually being overwritten.

    What do you use the stack for?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #29
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    I'm implementing a thread library so I believe the stack is necessary for the operations that each thread performs.

    According to GDB, the stack isn't being overwritten because none of the values in the memory locations surrounding the memory address, change between the allocation and delete of the stack...or at least thats how I interpret it

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is std::stack and std::vector and std::tr1::smart_ptr. Try those before doing manual allocation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  4. Memory handler
    By Dr. Bebop in forum C Programming
    Replies: 7
    Last Post: 09-15-2002, 04:14 PM
  5. multiple indirection...
    By doubleanti in forum C++ Programming
    Replies: 7
    Last Post: 08-31-2001, 10:56 AM