m0nkeymafia

Another little tale of woes from work.

We store a list of classes, by value, in a stl list.  We copy it in by value into the list.
Been using the code for ages, job done, easy stuff.

We have some custom built data class templates inside the classes [verrrrry nice]
Anyway I upped the data limit in some of them and the program started crashing

After some digging it turns out we were hitting the stack limit [its a good thing to check for]
So we went through and put everything on the heap.
Now we start to get some very odd random crashes.

Turns out we hadnt implemented the copy constructor on the class, so when it was on teh stack, the compiler just copied it all easily, when we started using pointers to the memory on the heap, the compiler only copied the pointer over, so we got errors!

It was an hour or so out my day chasing ghosts, but a nice lesson anyway.
So if you ever get random crashes, think about memorrryyyy

perkiset

I love and hate pointers. That is such a classic type of bug it's not funny.

Glad you squashed it, random stuff is monstrously frustrating.

thedarkness

quote author=m0nkeymafia link=topic=731.msg5097#msg5097 date=1201202464


After some digging it turns out we were hitting the stack limit [its a good thing to check for]



Dude, very interested. What was the stack limit and how would you test for that? Is the stack limit

mac

 hine specific or is it fairly standard?

Cheers,
td

thedarkness

quote author=m0nkeymafia link=topic=731.msg5097#msg5097 date=1201202464


Turns out we hadnt implemented the copy constructor on the class, so when it was on teh stack, the compiler just copied it all easily, when we started using pointers to the memory on the heap, the compiler only copied the pointer over, so we got errors!



Watch out for the same sort of problems with the assignment operator as well, the default assignment operator will just do a bitwise copy of the pointers so in the case of "a = b;" internal pointers in "b" will now point to the same location in memory as the equivalent pointer in "a" and the memory "b"s pointer was pointing to is now lost for good (memory leak). The copy constructor won't leak but the assignment operator will. I guess you had the issue of trying to delete memory that had already been deleted as well huh? Prolly not what you want. Scott Meyers "Effective C++" has a great section on this, I have the second edition and there is now a third edition so I hope I'm still up to date lmao...... It's a great book and the more I read it the more  I understand (understood almost nothing the first time i read it).

quote author=thedarkness link=topic=731.msg5102#msg5102 date=1201230202

quote author=m0nkeymafia link=topic=731.msg5097#msg5097 date=1201202464

What was the stack limit and how would you test for that? Is the stack limit

mac

 hine specific or is it fairly standard?



To answer my own question "ulimit -s" will tell you the stack size 10MB in my case, seems to be fairly standard for l8 model

linux

 .

Still don't know how to test for it though.... monk?

Cheers,
td

m0nkeymafia

Hah yeah it's easy to forget stuf like that, even when you've solved it for an almost identical problem.

On PC the stack is about 1mb ish.

Its a hard one to test for because VS2005 [my IDE... may be different in others] throws an exception when you hit the stack limit, but doesnt catch it so you don't know you've hit it... you need to go into the options and actually tell it to catch all exceptions! Odd, and annoying, but ce la vie!!

thedarkness

Cool, understood. I do a lot of stuff in VS as well, and lots in kdevelop, etc. on

Linux

 .

I know I can look at the stack usage under

linux

  through /proc/xxx/status or "top -H -p xxx" where xxx = pid.

Windows I'm not sure, don't grok Windows at that level as much (not OS you see :-) )

Cheers,
td


Perkiset's Place Home   Politics @ Perkiset's