Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1936 connectés 

  FORUM HardWare.fr
  Programmation
  C++

  [C] Problème de libération mémoire \Visual C++

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[C] Problème de libération mémoire \Visual C++

n°230815
Pschitt
Posté le 19-10-2002 à 16:14:47  profilanswer
 

Bonjour,
 
Quand je déroule mon prg en mode déboguage sous vc++ j'obtiens l'erreur suivante à la destruction d'une pile par l'intermédiare d'1 fct de dll importée. Cette même pile ayant été alloué par une autre fct de cette même dll :
 
User breakpoint called from code at 0x102124af
Vc++ s'arrête dans le if de la biblio. dbgheap.c - Debug CRT Heap Functions, après appel de free():
 

Code :
  1. /* forced failure */
  2.         if (!(*_pfnAllocHook)(_HOOK_FREE, pUserData, 0, nBlockUse, 0L, NULL, 0))
  3.         {
  4.             _RPT0(_CRT_WARN, "Client hook free failure.\n" );
  5.             return;
  6.         }
  7.         /*
  8.          * If this ASSERT fails, a bad pointer has been passed in. It may be
  9.          * totally bogus, or it may have been allocated from another heap.
  10.          * The pointer MUST come from the 'local' heap.
  11.          */


 
Une idée ?

mood
Publicité
Posté le 19-10-2002 à 16:14:47  profilanswer
 

n°231078
Musaran
Cerveaulté
Posté le 20-10-2002 à 04:33:15  profilanswer
 

[devin]
Tu as corrompu la pile avec un débordement mémoire dans ton programme.
[/devin]


---------------
Bricocheap: Montage de ventilo sur paté de mastic silicone
n°231102
Pschitt
Posté le 20-10-2002 à 11:15:49  profilanswer
 

Pourtant mon code semble bavure :
 
main.c :
 

Code :
  1. #include"sstack.h"
  2. sstack_t *ppile = static_stack_create(5);
  3. sstack_destroy(ppile);


C'est à l'appel de sstack_destroy(ppile) que VC++ me retourne l'erreur citée + haut.  
Au travers de quelques autres forums, j'ai trouvé d'anciens posts qui traitent du pb.  
1) Cela peut s'agir d'1 pointeur corrompu
2) free() refuse la libération du bloc mémoire car celui-ci à été alloué sur un autre tas.  
Je ne vois mon pb ni dans 1) ni dans 2) ???
La mémoire est bien alloué dans la même dll qui tente de la libérer donc dans le même tas.
???
 
Dll
sstack.h

Code :
  1. ...
  2. /* Library stack */
  3. struct static_stack_s;
  4. typedef struct static_stack_s sstack_t;
  5. ...
  6. /* Constructor */
  7. /*------------------------------------------------------------------*/
  8. /* Function : Create and Initializate a stack
  9. * arg : Size of stack  
  10. * exit: Pointer to the new stack
  11. * exit: NULL if allocation failed or size == 0
  12. /*------------------------------------------------------------------*/
  13. EXPORT sstack_t *static_stack_create(unsigned long size);
  14. ...
  15. /*------------------------------------------------------------------*/
  16. /* Function : Destroy a stack  
  17. * arg : stack   
  18. * exit: NULL -> OK
  19. /*------------------------------------------------------------------*/
  20. EXPORT sstack_t *sstack_destroy(sstack_t *pstack);


 
sstack.c

Code :
  1. #include<windows.h> /* DllMain() for dll creation */
  2. #include"sstack.h"
  3. ...
  4. /*
  5. * List Structure
  6. */
  7. struct static_stack_s {
  8. wdata_t *pawdata;   /* pointer to array of wrappers */
  9. unsigned long size;   /* size of wrappers array  */
  10. unsigned long count;  /* count of occupied wrapper */ 
  11. unsigned long itop;   /* index of top's wrapper  */ 
  12. };
  13. ...
  14. /*------------------------------------------------------------------*/
  15. /* Function : Create and Initializate a stack
  16. * arg : Size of stack  
  17. * exit: Pointer to the new stack
  18. * exit: NULL if allocation failed or size == 0
  19. /*------------------------------------------------------------------*/
  20. EXPORT sstack_t *static_stack_create(unsigned long size)
  21. {
  22. sstack_t *pstack = NULL;
  23. /* For stack initialization */
  24. static const struct static_stack_s sstack0 = {0};
  25.  
  26. if(size)
  27. {  
  28.  /* Stack allocation */
  29.  pstack = malloc(sizeof *pstack);
  30.  if(pstack)
  31.  {  
  32.   /* Initialization */
  33.   *pstack = sstack0;
  34.   /* ++size because stack starts at pstack->pawdata[1] */
  35.   pstack->size = size;  
  36.    
  37.   /* Array of wrappers allocation */
  38.   pstack->pawdata =  
  39.    malloc((++pstack->size) * sizeof *pstack->pawdata);
  40.   if(!pstack->pawdata)
  41.    /* Allocation failed */
  42.    pstack = NULL;
  43.  }
  44. }
  45. return pstack;
  46. }
  47. ...
  48. /*------------------------------------------------------------------*/
  49. /* Function : Destroy a stack  
  50. * arg : stack   
  51. * exit: NULL -> OK
  52. /*------------------------------------------------------------------*/
  53. EXPORT sstack_t *sstack_destroy(sstack_t *pstack)
  54. {
  55. sstack_t *ret = NULL;  
  56.  
  57. if(pstack)
  58. {
  59.  free(pstack->pawdata);
  60.  free(pstack);
  61. }
  62. return ret;
  63. }


 
 


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C++

  [C] Problème de libération mémoire \Visual C++

 

Sujets relatifs
Différence entre C++ et Visual C++Quelqu'un connait ou utilise Visual Adélia ???
Problème d'adaptation d'un div par rapport à un autre div en absolutRecherche CD2 Visual Studio 6 contenat le MSDN pour VB6........
Visual C++ ou Visual Basic ? Problème avec site hébergé en local...
[W3C] Problème de validationProbleme delicat
Probléme avec ma base de donnée !problème unix-windows (résolu)
Plus de sujets relatifs à : [C] Problème de libération mémoire \Visual C++


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR