Solved

Array allocations from within templates

Posted on 2000-03-21
6
187 Views
Last Modified: 2013-11-20
I have a DLL which contains a class, and one of its members is a template class instantiation. The template class allocates and subsequently deletes an array of an arbitrary type.

The problem occurs when the DLL is used from a MFC program in debug mode. The DLL is a regular Win32 DLL - not MFC.

When the array is destructed I get an assertion failure in dbgheap.c at line 1076. If the assertion is ignored everything works fine.

Hopefully the following code example will clarify this:

template<class T>
class CMyTempl<T> {
private:
    T *m_pT;
public:
    CMyTempl(int x) {
        m_pT = new T[x];
    }
    ~CMyTempl() {
        // This is where it all goes TU
        delete [] m_pT;
    }
};

struct SMyStruct {
    int a;
    short b;
    string c;
};

class CMyClass {
public:
    CMyTempl<SMyStruct> m_T;

    CMyClass() : m_T(123) {};
};

I figure the problem must be some clash between CRT memory checking and MFC memory checking. Hope you guys can help...
0
Comment
Question by:andymurd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 

Expert Comment

by:Subramanya
ID: 2641635

it is delete problem.
Remove the [] and make it like below.
~CMyTempl() {
        // This is where it all goes TU
        delete  m_pT;
    }
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2642592
WHAT!!

If you allocate with new T[..]; then you MUST deallocate with delete []p.

Thems the (C++) rules .. suggesting that you remove the [] from the delete is WRONG.

But what IS worth checking is that in your REAL code, you do actuallyallocate an array with new T[..] and didn't mistakenly do a new T(...).


0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2642599
It could well be that something ELSE is stuffing up and corrupting the memory.  And you don't find out until the (perfectly valid) delete happens.

It may even be something happening to another allocated lump of memory altogether (not even directly in the array in question).

Put some call to check the heap throughout your code and see .. or (if you have it) use something like BoundsChecker to find out who is stuffing up.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:andymurd
ID: 2643794
Subramanya, as RONSLOW says I must deallocate with the delete[] operator to ensure that the destructors are called for object in the array.

I am definitely allocating with the new[] operator. Unfortunately I don't have Boundschecker I'll see if CrtCheckMemory will make any sense.
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 200 total points
ID: 2643878
Hi andymurd?

Do both (the MFC app and the DLL) use the same C run-time library? Check this in 'Project Settings' tab 'C++' category 'code generation' combo box 'Use run-time library'.

ZOPPO
0
 
LVL 1

Author Comment

by:andymurd
ID: 2644048
Excellent answer Zoppo - have 200 points. Any ideas why the CRT DLL and static libraries are incompatible?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Complete beginner needs help making a cron job 9 133
How to know only "File created" with EventLog 3 35
Unix Command -- Challenging  question 7 106
Work with App store 7 121
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question